Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsToMany runs excessive (heavy) queries #6451

Closed
fruitl00p opened this issue Jul 1, 2024 · 3 comments
Closed

BelongsToMany runs excessive (heavy) queries #6451

fruitl00p opened this issue Jul 1, 2024 · 3 comments
Labels
needs more info More information is required stale

Comments

@fruitl00p
Copy link

fruitl00p commented Jul 1, 2024

  • Laravel Version: 11.10
  • Nova Version: 4.34.3
  • PHP Version: 8.3
  • Database Driver & Version: MariaDB 11.2.3

Description:

We have a BelongsToMany relation which seems to do a lot of queries eventhough the original pivot is empty?

We have the following setup:

Model: Compontent

    public function cook_products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class)
            ->wherePivot('direction', ComponentProductDirection::ToProduct)
            ->withPivot(['direction', 'quantity']);
    }

And in Nova we have:

            BelongsToMany::make(__('Cook products'), 'cook_products', Product::class)
                ->searchable()
                ->fields(fn () => [
                    Number::make(__('Quantity'), 'quantity')
                        ->readonly(!$request->user()->isAdmin())
                        ->rules('required'),
                    Hidden::make('direction')
                        ->default(ComponentProductDirection::ToProduct),
                ]),

When hitting the details page this incurs a /nova-api/products?search request with the following filter (shortened):

[
  "search" => ""
  "filters" => "<some hash, removed for brevity>"
  "orderBy" => ""
  "perPage" => "5"
  "trashed" => ""
  "page" => "1"
  "viaResource" => "components"
  "viaResourceId" => "25"
  "viaRelationship" => "cook_products"
  "relationshipType" => "belongsToMany"
]

This results in 14 queries of which 10 are duplicate?

image

(even though there were no relations)

If having 9 actual cook_products relations this balloons to 67 queries of which 7 where unique (!)

I'm not sure if this is a bug, a feature request or missing documentation. (or something else)

@fruitl00p
Copy link
Author

Further inspection reveals that in the XHR request for /nova-api/ I can't alter the construction of the 'component' class to exclude all other relations (i.e. using a without() call in the nova-api/-request. Also It seems that this is where the issue stems from; both Component and 'Product' and the Pivot have even more releations that are N+1 Queried multiple times... Hence the massive increase in (duplicate) queries.

Any way I can hook into this (without sacrificing the '$with = []' in my models?

@crynobone
Copy link
Member

Please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

@crynobone crynobone added the needs more info More information is required label Jul 2, 2024
@crynobone
Copy link
Member

Hey there,

We're closing this issue because it's inactive, already solved, old, or not relevant anymore. Feel free to open up a new issue if you're still experiencing this problem.

@crynobone crynobone closed this as not planned Won't fix, can't repro, duplicate, stale Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info More information is required stale
Projects
None yet
Development

No branches or pull requests

2 participants