Unnecessary (?) alert
janpio opened this issue · 7 comments
I have a page that is (imo) fully eager loaded:
select * from `users` where `id` = '1' limit 1
select * from `accounts` where `id` = '105' limit 1
select * from `balances` where `balances`.`account_id` in ('105')
select * from `statements` where `statements`.`id` in ('2702', '2703', '2704', '2705', '2706', '2707', '2708', '2709', '2710', '2711', '2712', '2713', '2714', '2715', '2716', '2717', '2718', '2719', '2720')
select * from `transactions` where `transactions`.`statement_id` in ('2702', '2703', '2704', '2705', '2706', '2707', '2708', '2709', '2710', '2711', '2712', '2713', '2714', '2715', '2716', '2717', '2718', '2719', '2720')
select * from `uploads` where `uploads`.`id` in ('2352', '2353', '2354', '2355', '2356', '2357', '2358', '2359', '2360', '2361', '2362', '2363', '2364', '2365', '2366', '2367', '2368', '2369', '2370')
select * from `statements` where `statements`.`account_id` in ('105')
select * from `uploads` where `uploads`.`id` in ('2352', '2353', '2354', '2355', '2356', '2357', '2358', '2359', '2360', '2361', '2362', '2363', '2364', '2365', '2366', '2367', '2368', '2369', '2370')
select * from `transactions` where `transactions`.`account_id` = '105' and `transactions`.`account_id` is not null
select * from `banks` where `banks`.`id` = '36' limit 1
select * from `institutes` where `institutes`.`id` = '1' limit 1
The uploads query is duplicated, but this is caused by different "routes" to the relation. Still the package is giving me an alert:
Model: App\Statement => Relation: App\Upload - You should add "with('AppUpload')" to eager-load this relation.
Could this be a bug or edge case or did I misunderstand something?
I could reproduce it on another page, where it is a bit clearer why the data is loaded:
$user->banks->accounts->balances
$user->accounts->balances
These are two ways to get the same data. As the page is looping over ->accounts of both $user->banks and $user for two different parts of the interface, this seems to trigger the alert:
Found the following N+1 queries in this request:
Model: App\Account => Relation: App\Balance - You should add "with('AppBalance')" to eager-load this relation.
Is this expected?
What do you mean by routes? Then I can try to see if this can be detected.
The problem is that I can only listen for sql queries and then need to backtrack from there.
Hmm maybe I should just use the latest stack trace location to determine if the calls are actually duplicates. That might be a good idea :)
See my second post for an example of "routes": There are two ways to get from a to b via relationships in this app's relationship model. This results in the same query being fired twice, but I couldn't find a way to avoid that until now with Eloquent.
In this app I can trigger the faulty behavior by just doing this:
$user = Auth()->user();
$user->load('banks.accounts.balances', 'accounts.balances');
In this case, this package will be unable to tell the difference.
I will add the backtrack source location as another unique parameter to check. This will at least minimize this a lot, I guess.
And well, in theory you still do resolve the same relation twice.
I know, but: Is there a way around that?
I pushed some commits in order to take the last stack trace frame into account.
Unfortunately, there's currently no way to fix this:
$user = Auth()->user();
$user->load('banks.accounts.balances', 'accounts.balances');