I want to add some useful methods to avoid a lot of n + 1 issues in an outdated application.
The overall picture is as follows:
select a.*
from [table-A] a
where
Received in the ClassArecord instance collection
Then lazy retrieved helper instances:
select b.*
from [sub-table-B] b
where b.ParentId = @ClassA_ID
As a result, there is a problem with choosing n + 1. Basically, this is not a serious problem, since only a few copies ClassAare extracted on the page with a rare hit, but in more and more places this n + 1 problem causes the pages to become too slow as the application scales,
, ClassA ClassB .
, :
1) ClassA, , ClassB :
select b.*
from [sub-table-B] b
where b.ParentId in ( )
, SQL (- ).
2) ClassB :
select b.*
from [sub-table-B] b
inner join [table-A] a
on b.ParentId = a.[ID]
where
, [table-A] .
3) ClassA:
select a.*, b.*
from [table-A] a
left outer join [sub-table-B] b
on a.[ID] = b.ParentId
where
, [table-A] - , .
, 3 :
, . , ? , ? ?
, Linq, EF NHibernate?
4- , , 3?