EF Core difficulty with INNER JOINS and LEFT JOINS - c#

I am trying to retrieve some data from a very big database. In order to retrieve it I need to get some data from within 6 inner joins. The following options is what I tried already:
Option 1 Using EF Core en ThenInclude
var enquiry = await _context.Enquiries
.Where(e => e.Id == id)
.Include(e => e.EnqPartJobs.Where(ej => ej.RecordStateId == 0))
.ThenInclude(ej => ej.AnswerGrpBatch)
.ThenInclude(agb => agb.AnswerGrps)
.ThenInclude(ag => ag.EnqPropResults)
.ThenInclude(epr => epr.EnqPropResultDevs)
.ThenInclude(epr => epr.Deviation)
.ToListAsync();
The advantage of this method is that it will map nicely in a Enquiry class with a list of AnswerGrpBatches. Btw I did not name these classes this is just how the database looks.
When looking at the query it is the following:
SELECT [e].[SID], [e].[FK_CRMPartner_SID], [e].[FK_Certificate_SID], [e].[createDate], [e].[displayDate], [e].[extID], [e].[finishDate], [e].[fixedGenId], [e].[aName], [t2].[SID], [t2].[FK_Enquiry_SID], [t2].[aName], [t2].[FK_RecordState_SID], [t2].[remark], [t2].[validFrom], [t2].[validTo], [t2].[SID0], [t2].[FK_EnqPartJob_SID], [t2].[FK_RecordState_SID0], [t2].[SID1], [t2].[FK_AnswerGrpBatch_SID], [t2].[FK_RecordState_SID1], [t2].[SID00], [t2].[FK_AnswerGrp_SID], [t2].[FK_EnqPropertyForm_SID], [t2].[FK_PossResult_SID], [t2].[FK_RecordState_SID00], [t2].[value], [t2].[SID000], [t2].[FK_Deviation_SID], [t2].[FK_EnqPropResult_SID], [t2].[SID0000], [t2].[aDescription], [t2].[aName0]
FROM [Enquiry] AS [e]
LEFT JOIN (
SELECT [e0].[SID], [e0].[FK_Enquiry_SID], [e0].[aName], [e0].[FK_RecordState_SID], [e0].[remark], [e0].[validFrom], [e0].[validTo], [a].[SID] AS [SID0], [a].[FK_EnqPartJob_SID], [a].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t1].[SID] AS [SID1], [t1].[FK_AnswerGrpBatch_SID], [t1].[FK_RecordState_SID] AS [FK_RecordState_SID1], [t1].[SID0] AS [SID00], [t1].[FK_AnswerGrp_SID], [t1].[FK_EnqPropertyForm_SID], [t1].[FK_PossResult_SID], [t1].[FK_RecordState_SID0] AS [FK_RecordState_SID00], [t1].[value], [t1].[SID00] AS [SID000], [t1].[FK_Deviation_SID], [t1].[FK_EnqPropResult_SID], [t1].[SID000] AS [SID0000], [t1].[aDescription], [t1].[aName] AS [aName0]
FROM [EnqPartJob] AS [e0]
LEFT JOIN [AnswerGrpBatch] AS [a] ON [e0].[SID] = [a].[FK_EnqPartJob_SID]
LEFT JOIN (
SELECT [a0].[SID], [a0].[FK_AnswerGrpBatch_SID], [a0].[FK_RecordState_SID], [t0].[SID] AS [SID0], [t0].[FK_AnswerGrp_SID], [t0].[FK_EnqPropertyForm_SID], [t0].[FK_PossResult_SID], [t0].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t0].[value], [t0].[SID0] AS [SID00], [t0].[FK_Deviation_SID], [t0].[FK_EnqPropResult_SID], [t0].[SID00] AS [SID000], [t0].[aDescription], [t0].[aName]
FROM [AnswerGrp] AS [a0]
LEFT JOIN (
SELECT [e1].[SID], [e1].[FK_AnswerGrp_SID], [e1].[FK_EnqPropertyForm_SID], [e1].[FK_PossResult_SID], [e1].[FK_RecordState_SID], [e1].[value], [t].[SID] AS [SID0], [t].[FK_Deviation_SID], [t].[FK_EnqPropResult_SID], [t].[SID0] AS [SID00], [t].[aDescription], [t].[aName]
FROM [EnqPropResult] AS [e1]
LEFT JOIN (
SELECT [e2].[SID], [e2].[FK_Deviation_SID], [e2].[FK_EnqPropResult_SID], [d].[SID] AS [SID0], [d].[aDescription], [d].[aName]
FROM [EnqPropResultDev] AS [e2]
INNER JOIN [Deviation] AS [d] ON [e2].[FK_Deviation_SID] = [d].[SID]
) AS [t] ON [e1].[SID] = [t].[FK_EnqPropResult_SID]
) AS [t0] ON [a0].[SID] = [t0].[FK_AnswerGrp_SID]
) AS [t1] ON [a].[SID] = [t1].[FK_AnswerGrpBatch_SID]
WHERE [e0].[FK_RecordState_SID] = 0
) AS [t2] ON [e].[SID] = [t2].[FK_Enquiry_SID]
WHERE [e].[SID] = 1790797
But it needs to be one of these 2 options:
using a IS NOT NULL statement but I cannot figure out how to make this one beceause it is deep inside different classes.
SELECT [e].[SID], [e].[FK_CRMPartner_SID], [e].[FK_Certificate_SID], [e].[createDate], [e].[displayDate], [e].[extID], [e].[finishDate], [e].[fixedGenId], [e].[aName], [t2].[SID], [t2].[FK_Enquiry_SID], [t2].[aName], [t2].[FK_RecordState_SID], [t2].[remark], [t2].[validFrom], [t2].[validTo], [t2].[SID0], [t2].[FK_EnqPartJob_SID], [t2].[FK_RecordState_SID0], [t2].[SID1], [t2].[FK_AnswerGrpBatch_SID], [t2].[FK_RecordState_SID1], [t2].[SID00], [t2].[FK_AnswerGrp_SID], [t2].[FK_EnqPropertyForm_SID], [t2].[FK_PossResult_SID], [t2].[FK_RecordState_SID00], [t2].[value], [t2].[SID000], [t2].[FK_Deviation_SID], [t2].[FK_EnqPropResult_SID], [t2].[SID0000], [t2].[aDescription], [t2].[aName0]
FROM [Enquiry] AS [e]
LEFT JOIN (
SELECT [e0].[SID], [e0].[FK_Enquiry_SID], [e0].[aName], [e0].[FK_RecordState_SID], [e0].[remark], [e0].[validFrom], [e0].[validTo], [a].[SID] AS [SID0], [a].[FK_EnqPartJob_SID], [a].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t1].[SID] AS [SID1], [t1].[FK_AnswerGrpBatch_SID], [t1].[FK_RecordState_SID] AS [FK_RecordState_SID1], [t1].[SID0] AS [SID00], [t1].[FK_AnswerGrp_SID], [t1].[FK_EnqPropertyForm_SID], [t1].[FK_PossResult_SID], [t1].[FK_RecordState_SID0] AS [FK_RecordState_SID00], [t1].[value], [t1].[SID00] AS [SID000], [t1].[FK_Deviation_SID], [t1].[FK_EnqPropResult_SID], [t1].[SID000] AS [SID0000], [t1].[aDescription], [t1].[aName] AS [aName0]
FROM [EnqPartJob] AS [e0]
LEFT JOIN [AnswerGrpBatch] AS [a] ON [e0].[SID] = [a].[FK_EnqPartJob_SID]
LEFT JOIN (
SELECT [a0].[SID], [a0].[FK_AnswerGrpBatch_SID], [a0].[FK_RecordState_SID], [t0].[SID] AS [SID0], [t0].[FK_AnswerGrp_SID], [t0].[FK_EnqPropertyForm_SID], [t0].[FK_PossResult_SID], [t0].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t0].[value], [t0].[SID0] AS [SID00], [t0].[FK_Deviation_SID], [t0].[FK_EnqPropResult_SID], [t0].[SID00] AS [SID000], [t0].[aDescription], [t0].[aName]
FROM [AnswerGrp] AS [a0]
LEFT JOIN (
SELECT [e1].[SID], [e1].[FK_AnswerGrp_SID], [e1].[FK_EnqPropertyForm_SID], [e1].[FK_PossResult_SID], [e1].[FK_RecordState_SID], [e1].[value], [t].[SID] AS [SID0], [t].[FK_Deviation_SID], [t].[FK_EnqPropResult_SID], [t].[SID0] AS [SID00], [t].[aDescription], [t].[aName]
FROM [EnqPropResult] AS [e1]
LEFT JOIN (
SELECT [e2].[SID], [e2].[FK_Deviation_SID], [e2].[FK_EnqPropResult_SID], [d].[SID] AS [SID0], [d].[aDescription], [d].[aName]
FROM [EnqPropResultDev] AS [e2]
INNER JOIN [Deviation] AS [d] ON [e2].[FK_Deviation_SID] = [d].[SID]
) AS [t] ON [e1].[SID] = [t].[FK_EnqPropResult_SID]
) AS [t0] ON [a0].[SID] = [t0].[FK_AnswerGrp_SID]
) AS [t1] ON [a].[SID] = [t1].[FK_AnswerGrpBatch_SID]
WHERE [e0].[FK_RecordState_SID] = 0
) AS [t2] ON [e].[SID] = [t2].[FK_Enquiry_SID]
WHERE [e].[SID] = 1790797 AND [t2].[SID000] IS NOT NULL
Forcing INNER JOINS:
SELECT [e].[SID], [e].[FK_CRMPartner_SID], [e].[FK_Certificate_SID], [e].[createDate], [e].[displayDate], [e].[extID], [e].[finishDate], [e].[fixedGenId], [e].[aName], [t2].[SID], [t2].[FK_Enquiry_SID], [t2].[aName], [t2].[FK_RecordState_SID], [t2].[remark], [t2].[validFrom], [t2].[validTo], [t2].[SID0], [t2].[FK_EnqPartJob_SID], [t2].[FK_RecordState_SID0], [t2].[SID1], [t2].[FK_AnswerGrpBatch_SID], [t2].[FK_RecordState_SID1], [t2].[SID00], [t2].[FK_AnswerGrp_SID], [t2].[FK_EnqPropertyForm_SID], [t2].[FK_PossResult_SID], [t2].[FK_RecordState_SID00], [t2].[value], [t2].[SID000], [t2].[FK_Deviation_SID], [t2].[FK_EnqPropResult_SID], [t2].[SID0000], [t2].[aDescription], [t2].[aName0]
FROM [Enquiry] AS [e]
LEFT JOIN (
SELECT [e0].[SID], [e0].[FK_Enquiry_SID], [e0].[aName], [e0].[FK_RecordState_SID], [e0].[remark], [e0].[validFrom], [e0].[validTo], [a].[SID] AS [SID0], [a].[FK_EnqPartJob_SID], [a].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t1].[SID] AS [SID1], [t1].[FK_AnswerGrpBatch_SID], [t1].[FK_RecordState_SID] AS [FK_RecordState_SID1], [t1].[SID0] AS [SID00], [t1].[FK_AnswerGrp_SID], [t1].[FK_EnqPropertyForm_SID], [t1].[FK_PossResult_SID], [t1].[FK_RecordState_SID0] AS [FK_RecordState_SID00], [t1].[value], [t1].[SID00] AS [SID000], [t1].[FK_Deviation_SID], [t1].[FK_EnqPropResult_SID], [t1].[SID000] AS [SID0000], [t1].[aDescription], [t1].[aName] AS [aName0]
FROM [EnqPartJob] AS [e0]
LEFT JOIN [AnswerGrpBatch] AS [a] ON [e0].[SID] = [a].[FK_EnqPartJob_SID]
LEFT JOIN (
SELECT [a0].[SID], [a0].[FK_AnswerGrpBatch_SID], [a0].[FK_RecordState_SID], [t0].[SID] AS [SID0], [t0].[FK_AnswerGrp_SID], [t0].[FK_EnqPropertyForm_SID], [t0].[FK_PossResult_SID], [t0].[FK_RecordState_SID] AS [FK_RecordState_SID0], [t0].[value], [t0].[SID0] AS [SID00], [t0].[FK_Deviation_SID], [t0].[FK_EnqPropResult_SID], [t0].[SID00] AS [SID000], [t0].[aDescription], [t0].[aName]
FROM [AnswerGrp] AS [a0]
INNER JOIN (
SELECT [e1].[SID], [e1].[FK_AnswerGrp_SID], [e1].[FK_EnqPropertyForm_SID], [e1].[FK_PossResult_SID], [e1].[FK_RecordState_SID], [e1].[value], [t].[SID] AS [SID0], [t].[FK_Deviation_SID], [t].[FK_EnqPropResult_SID], [t].[SID0] AS [SID00], [t].[aDescription], [t].[aName]
FROM [EnqPropResult] AS [e1]
INNER JOIN (
SELECT [e2].[SID], [e2].[FK_Deviation_SID], [e2].[FK_EnqPropResult_SID], [d].[SID] AS [SID0], [d].[aDescription], [d].[aName]
FROM [EnqPropResultDev] AS [e2]
INNER JOIN [Deviation] AS [d] ON [e2].[FK_Deviation_SID] = [d].[SID]
) AS [t] ON [e1].[SID] = [t].[FK_EnqPropResult_SID]
) AS [t0] ON [a0].[SID] = [t0].[FK_AnswerGrp_SID]
) AS [t1] ON [a].[SID] = [t1].[FK_AnswerGrpBatch_SID]
WHERE [e0].[FK_RecordState_SID] = 0
) AS [t2] ON [e].[SID] = [t2].[FK_Enquiry_SID]
WHERE [e].[SID] = 1790797
The problem is that it will use Left Joins for every Include and ThenInclude except the last one. But this will also retrieve information that I do not need. So I tried to make a LINQ query.
Option 2 LINQ
The LINQ query always uses INNER JOINS, so I exactly get what I want from the database. The only problem is that I have no clue how to map this to an Enquiry class where the INNER JOINS are a list within the Enquiry class. Just like with option 1.
var query = from enquiry in _context.Set<Enquiry>()
join enqPartJob in _context.Set<EnqPartJob>()
on enquiry.Id equals enqPartJob.EnquiryId
join answerGrpBatch in _context.Set<AnswerGrpBatch>()
on enqPartJob.Id equals answerGrpBatch.EnqPartJobId
join answerGrp in _context.Set<AnswerGrp>()
on answerGrpBatch.Id equals answerGrp.AnswerGrpBatchId
join enqPropResult in _context.Set<EnqPropResult>()
on answerGrp.Id equals enqPropResult.AnswerGrpId
join enqPropResultDev in _context.Set<EnqPropResultDev>()
on enqPropResult.Id equals enqPropResultDev.EnqPropResultId
join deviation in _context.Set<Deviation>()
on enqPropResultDev.DeviationId equals deviation.Id
where enquiry.Id == id
select new { enquiry, enqPropResultDev };
And yis I get that there is a reason EFCore uses LEFT JOINS but this is not my datbase so I have to adapt to it.

Thx to Ivan Stoev who commented this suggestion.
Starting with Deviation and working my way up to Enquiry did the trick for me.
The query now looks like this (as you can see it contains INNER JOINS).
SELECT [t].[SID], [t].[aDescription], [t].[aName], [t0].[SID], [t0].[FK_Deviation_SID], [t0].[FK_EnqPropResult_SID], [t0].[SID0], [t0].[FK_AnswerGrp_SID], [t0].[FK_EnqPropertyForm_SID], [t0].[FK_PossResult_SID], [t0].[FK_RecordState_SID], [t0].[value], [t0].[SID1], [t0].[FK_AnswerGrpBatch_SID], [t0].[FK_RecordState_SID0], [t0].[SID2], [t0].[FK_EnqPartJob_SID], [t0].[FK_RecordState_SID1], [t0].[SID3], [t0].[FK_Enquiry_SID], [t0].[aName], [t0].[FK_RecordState_SID2], [t0].[remark], [t0].[validFrom], [t0].[validTo], [t0].[SID4], [t0].[FK_CRMPartner_SID], [t0].[FK_Certificate_SID], [t0].[createDate], [t0].[displayDate], [t0].[extID], [t0].[finishDate], [t0].[fixedGenId], [t0].[aName0]
FROM (
SELECT TOP(1) [d].[SID], [d].[aDescription], [d].[aName]
FROM [Deviation] AS [d]
WHERE [d].[SID] = 5038
) AS [t]
LEFT JOIN (
SELECT [e].[SID], [e].[FK_Deviation_SID], [e].[FK_EnqPropResult_SID], [e0].[SID] AS [SID0], [e0].[FK_AnswerGrp_SID], [e0].[FK_EnqPropertyForm_SID], [e0].[FK_PossResult_SID], [e0].[FK_RecordState_SID], [e0].[value], [a].[SID] AS [SID1], [a].[FK_AnswerGrpBatch_SID], [a].[FK_RecordState_SID] AS [FK_RecordState_SID0], [a0].[SID] AS [SID2], [a0].[FK_EnqPartJob_SID], [a0].[FK_RecordState_SID] AS [FK_RecordState_SID1], [e1].[SID] AS [SID3], [e1].[FK_Enquiry_SID], [e1].[aName], [e1].[FK_RecordState_SID] AS [FK_RecordState_SID2], [e1].[remark], [e1].[validFrom], [e1].[validTo], [e2].[SID] AS [SID4], [e2].[FK_CRMPartner_SID], [e2].[FK_Certificate_SID], [e2].[createDate], [e2].[displayDate], [e2].[extID], [e2].[finishDate], [e2].[fixedGenId], [e2].[aName] AS [aName0]
FROM [EnqPropResultDev] AS [e]
INNER JOIN [EnqPropResult] AS [e0] ON [e].[FK_EnqPropResult_SID] = [e0].[SID]
INNER JOIN [AnswerGrp] AS [a] ON [e0].[FK_AnswerGrp_SID] = [a].[SID]
INNER JOIN [AnswerGrpBatch] AS [a0] ON [a].[FK_AnswerGrpBatch_SID] = [a0].[SID]
INNER JOIN [EnqPartJob] AS [e1] ON [a0].[FK_EnqPartJob_SID] = [e1].[SID]
INNER JOIN [Enquiry] AS [e2] ON [e1].[FK_Enquiry_SID] = [e2].[SID]
WHERE [e0].[FK_RecordState_SID] = 0
) AS [t0] ON [t].[SID] = [t0].[FK_Deviation_SID]
ORDER BY [t].[SID], [t0].[SID], [t0].[SID0], [t0].[SID1], [t0].[SID2], [t0].[SID3], [t0].[SID4]

Related

Entity Framework Core : query improvement

I'm new to EF, I have a database in which a certain table contains keys of a number of other tables, it is the central table from which I go to the other tables (ProductIdentifiers).
The input to the query is not id, but the Name which is not defined as any key.
Here's my Entity Framework query :
public ProductIdentifier? GetFullCedMed(string v)
=> db.ProductIdentifiers.Where(a => a.Name == v)
.Include(ced => ced.Project)
.Include(ced => ced.LimitValues).ThenInclude(l => l.Parameter)
.Include(ced => ced.LimitValues).ThenInclude(l => l.Bin)
.Include(ced => ced.LimitValues).ThenInclude(l => l.Stage)
.Include(ced => ced.LimitValues).ThenInclude(l => l.TestTypeNavigation)
.Include(ced => ced.ConfigValues).ThenInclude(c => c.Parameter)
.Include(ced => ced.ConfigValues).ThenInclude(c => c.Bin)
.Include(ced => ced.ConfigValues).ThenInclude(c => c.Stage)
.Include(ced => ced.ConfigValues).ThenInclude(c => c.TestTypeNavigation)
.Include(ced => ced.FatherCedmed)
.ToList().FirstOrDefault();
The code is converted to a SQL query that looks like this:
exec sp_executesql N'SELECT [p].[id], [p].[FatherCedmedId], [p].[Name], [p].[ProjectId], [m].[id], [m].[IsActive], [m].[Name], [m].[ProductLineName], [p0].[id], [t].[id], [t].[BinId], [t].[CED_MED], [t].[LSL], [t].[ParameterID], [t].[StageID], [t].[TestType], [t].[USL], [t].[id0], [t].[Enabled], [t].[FORMAT], [t].[IsLimit], [t].[ParamID], [t].[Parameter_Name], [t].[Print], [t].[Unit], [t].[id1], [t].[BinDescription], [t].[BinDescriptionOverride], [t].[BinNumber], [t].[BinNumberOverride], [t].[GroupID], [t].[ParamID0], [t].[StageID0], [t].[id2], [t].[Name], [t].[StageNumber], [t].[id3], [t].[LevelId], [t].[Name0], [t].[OrderingId], [t0].[id], [t0].[BinId], [t0].[CED_MED], [t0].[ParameterID], [t0].[StageID], [t0].[TestType], [t0].[Value], [t0].[id0], [t0].[Enabled], [t0].[FORMAT], [t0].[IsLimit], [t0].[ParamID], [t0].[Parameter_Name], [t0].[Print], [t0].[Unit], [t0].[id1], [t0].[BinDescription], [t0].[BinDescriptionOverride], [t0].[BinNumber], [t0].[BinNumberOverride], [t0].[GroupID], [t0].[ParamID0], [t0].[StageID0], [t0].[id2], [t0].[Name], [t0].[StageNumber], [t0].[id3], [t0].[LevelId], [t0].[Name0], [t0].[OrderingId], [p0].[FatherCedmedId], [p0].[Name], [p0].[ProjectId]
FROM [ProductIdentifiers] AS [p]
LEFT JOIN [main_Projects] AS [m] ON [p].[ProjectId] = [m].[id]
LEFT JOIN [ProductIdentifiers] AS [p0] ON [p].[FatherCedmedId] = [p0].[id]
LEFT JOIN (
SELECT [l].[id], [l].[BinId], [l].[CED_MED], [l].[LSL], [l].[ParameterID], [l].[StageID], [l].[TestType], [l].[USL], [p1].[id] AS [id0], [p1].[Enabled], [p1].[FORMAT], [p1].[IsLimit], [p1].[ParamID], [p1].[Parameter_Name], [p1].[Print], [p1].[Unit], [b].[id] AS [id1], [b].[BinDescription], [b].[BinDescriptionOverride], [b].[BinNumber], [b].[BinNumberOverride], [b].[GroupID], [b].[ParamID] AS [ParamID0], [b].[StageID] AS [StageID0], [s].[id] AS [id2], [s].[Name], [s].[StageNumber], [m0].[id] AS [id3], [m0].[LevelId], [m0].[Name] AS [Name0], [m0].[OrderingId]
FROM [LimitValues] AS [l]
INNER JOIN [Parameters] AS [p1] ON [l].[ParameterID] = [p1].[id]
INNER JOIN [Bins] AS [b] ON [l].[BinId] = [b].[id]
LEFT JOIN [Stages] AS [s] ON [l].[StageID] = [s].[id]
LEFT JOIN [main_TestTypes] AS [m0] ON [l].[TestType] = [m0].[id]
) AS [t] ON [p].[id] = [t].[CED_MED]
LEFT JOIN (
SELECT [c].[id], [c].[BinId], [c].[CED_MED], [c].[ParameterID], [c].[StageID], [c].[TestType], [c].[Value], [p2].[id] AS [id0], [p2].[Enabled], [p2].[FORMAT], [p2].[IsLimit], [p2].[ParamID], [p2].[Parameter_Name], [p2].[Print], [p2].[Unit], [b0].[id] AS [id1], [b0].[BinDescription], [b0].[BinDescriptionOverride], [b0].[BinNumber], [b0].[BinNumberOverride], [b0].[GroupID], [b0].[ParamID] AS [ParamID0], [b0].[StageID] AS [StageID0], [s0].[id] AS [id2], [s0].[Name], [s0].[StageNumber], [m1].[id] AS [id3], [m1].[LevelId], [m1].[Name] AS [Name0], [m1].[OrderingId]
FROM [ConfigValues] AS [c]
INNER JOIN [Parameters] AS [p2] ON [c].[ParameterID] = [p2].[id]
INNER JOIN [Bins] AS [b0] ON [c].[BinId] = [b0].[id]
LEFT JOIN [Stages] AS [s0] ON [c].[StageID] = [s0].[id]
LEFT JOIN [main_TestTypes] AS [m1] ON [c].[TestType] = [m1].[id]
) AS [t0] ON [p].[id] = [t0].[CED_MED]
WHERE [p].[Name] = #__v_0
ORDER BY [p].[id], [m].[id], [p0].[id], [t].[id], [t].[id0], [t].[id1], [t].[id2], [t].[id3], [t0].[id], [t0].[id0], [t0].[id1], [t0].[id2]',N'#__v_0 varchar(255)',#__v_0='NAME_OF_RECORD_FROM_ProductIdentifier_TABLE'
Pay attention to the input in the ORDER BY line.
My question is: how can the query be improved?
The tables contain a lot of data and the query takes a lot of time.
Will retrieval by ID make it faster? Indicates that all the data is relevant for me, which means that it is necessary to join all the tables.
Thanks for any other advice.
for creating separated query and getting better performance use .AsSplitQuery()

How to do a where in subquery using Entity Framework?

I have a query like:
var result = from u in this.DataContext.Users
join l in DataContext.Locations on u.Id equals l.userId
where u.active == 1
select u;
return result ;
I want to add a subquery WHERE IN clause like:
WHERE u.Id IN (SELECT userId FROM approved_users)
Is this possible?
I am not sure why you want it in a sub query, it seems simpler to just join the Approved Users table, but I do not know the requirement so I have presented two options. One option that has a sub query and one option with the additional join. I am also making an assumption that you don't have any navigation properties.
Option 1 - Subquery:
var subQuery =
from u in context.Users.Where(x => context.ApprovedUsers.Select(y => y.ApprovedUserId).Contains(x.UserId))
join l in context.Locations on u.UserId equals l.UserId
where u.IsActive == true
select u;
which generates something like this
SELECT
[Extent1].[UserId] AS [UserId],
[Extent1].[Name] AS [Name],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[User] AS [Extent1]
INNER JOIN [dbo].[Location] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[UserId]
WHERE ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[ApprovedUser] AS [Extent3]
WHERE [Extent3].[ApprovedUserId] = [Extent1].[UserId]
)) AND (1 = [Extent1].[IsActive])
Option 2 - Additional Join:
var query =
from u in context.Users
join l in context.Locations on u.UserId equals l.UserId
join au in context.ApprovedUsers on u.UserId equals au.ApprovedUserId
where u.IsActive == true
select u;
which generates:
SELECT
[Extent1].[UserId] AS [UserId],
[Extent1].[Name] AS [Name],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[User] AS [Extent1]
INNER JOIN [dbo].[Location] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[UserId]
INNER JOIN [dbo].[ApprovedUser] AS [Extent3] ON [Extent1].[UserId] = [Extent3].[ApprovedUserId]
WHERE 1 = [Extent1].[IsActive]

Limit results from multiple individual tables in a single LINQ-to-Entities query. Resultant T-SQL is wrong

I need to query multiple tables with one query, and I need to limit the results from each table individually.
An example ...
I have a ContentItem, Retailer, and Product table.
ContentItem has a Type (int) field that corresponds to an enum of content types like "Retailer" and "Product." I am filtering ContentItem using this field for each sub-subquery.
ContentItem has an Id (pkey) field.
Retailer and Product have an Id (pkey) field. Id is also an FK to ContentItem.Id.
I can select from all three tables with a LEFT JOIN query. From there, I can then limit the total number of rows returned, let's say 6 rows total.
What I want to do is limit the number of rows returned from Retailer and Product individually. This way, I will have 12 rows (max) total: 6 from Retailer, and 6 from Product.
I can already accomplish this with SQL, but I am having a difficult time getting LINQ-to-Entities to "do the right thing."
Here's my SQL
SELECT * From
(
(SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 0 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Retailers)
UNION ALL
(SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 1 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Brands)
UNION ALL
(SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 2 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Products)
UNION ALL
(SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 3 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Certifications)
UNION ALL
(SELECT * FROM (SELECT * FROM [dbo].[ContentItem] WHERE Type = 4 ORDER BY ContentItem.mtime OFFSET 0 ROWS FETCH NEXT 6 ROWS ONLY) Claims)
) as ContentItem
LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id);
Here's one of my many iterations of LINQ queries (which is not returning the desired result).
var queryRetailers = contentItemModel
.Where(contentItem => contentItem.Type == ContentTypeEnum.Retailer)
.OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryBrands = contentItemModel
.Where(contentItem => contentItem.Type == ContentTypeEnum.Brand)
.OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryProducts = contentItemModel
.Where(contentItem => contentItem.Type == ContentTypeEnum.Product)
.OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryCertifications = contentItemModel
.Where(contentItem => contentItem.Type == ContentTypeEnum.Certification)
.OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var queryClaims = contentItemModel
.Where(contentItem => contentItem.Type == ContentTypeEnum.Claim)
.OrderByDescending(o => o.mtime).Skip(skip).Take(take).Select(o => new { Id = o.Id });
var query = from contentItem in
queryRetailers
.Concat(queryBrands)
.Concat(queryProducts)
.Concat(queryCertifications)
.Concat(queryClaims)
join item in context.Retailer on contentItem.Id equals item.Id into retailerGroup
from retailer in retailerGroup.DefaultIfEmpty(null)
join item in context.Brand on contentItem.Id equals item.Id into brandGroup
from brand in brandGroup.DefaultIfEmpty(null)
join item in context.Product on contentItem.Id equals item.Id into productGroup
from product in productGroup.DefaultIfEmpty(null)
join item in context.Certification on contentItem.Id equals item.Id into certificationGroup
from certification in certificationGroup.DefaultIfEmpty(null)
join item in context.Claim on contentItem.Id equals item.Id into claimGroup
from claim in claimGroup.DefaultIfEmpty(null)
select new
{
contentItem,
retailer,
brand,
product,
certification,
claim
};
var results = query.ToList();
This query returns SQL that essentially "nests" my UNION ALL statements, and the server returns all rows from the database.
SELECT
[Distinct4].[C1] AS [C1],
[Distinct4].[C2] AS [C2],
[Extent6].[Id] AS [Id],
[Extent6].[RowVersion] AS [RowVersion],
[Extent6].[ctime] AS [ctime],
[Extent6].[mtime] AS [mtime],
[Extent7].[Id] AS [Id1],
[Extent7].[Recommended] AS [Recommended],
[Extent7].[RowVersion] AS [RowVersion1],
[Extent7].[ctime] AS [ctime1],
[Extent7].[mtime] AS [mtime1],
[Extent8].[Id] AS [Id2],
[Extent8].[OverrideGrade] AS [OverrideGrade],
[Extent8].[PlantBased] AS [PlantBased],
[Extent8].[Recommended] AS [Recommended1],
[Extent8].[RowVersion] AS [RowVersion2],
[Extent8].[ctime] AS [ctime2],
[Extent8].[mtime] AS [mtime2],
[Extent8].[Brand_Id] AS [Brand_Id],
[Extent8].[Grade_Name] AS [Grade_Name],
[Extent8].[Grade_Value] AS [Grade_Value],
[Extent9].[Id] AS [Id3],
[Extent9].[RowVersion] AS [RowVersion3],
[Extent9].[ctime] AS [ctime3],
[Extent9].[mtime] AS [mtime3],
[Extent9].[Grade_Name] AS [Grade_Name1],
[Extent9].[Grade_Value] AS [Grade_Value1],
[Extent10].[Id] AS [Id4],
[Extent10].[RowVersion] AS [RowVersion4],
[Extent10].[ctime] AS [ctime4],
[Extent10].[mtime] AS [mtime4],
[Extent10].[Grade_Name] AS [Grade_Name2],
[Extent10].[Grade_Value] AS [Grade_Value2]
FROM (SELECT DISTINCT
[UnionAll4].[C1] AS [C1],
[UnionAll4].[C2] AS [C2]
FROM (SELECT
[Distinct3].[C1] AS [C1],
[Distinct3].[C2] AS [C2]
FROM ( SELECT DISTINCT
[UnionAll3].[C1] AS [C1],
[UnionAll3].[C2] AS [C2]
FROM (SELECT
[Distinct2].[C1] AS [C1],
[Distinct2].[C2] AS [C2]
FROM ( SELECT DISTINCT
[UnionAll2].[C1] AS [C1],
[UnionAll2].[C2] AS [C2]
FROM (SELECT
[Distinct1].[C1] AS [C1],
[Distinct1].[C2] AS [C2]
FROM ( SELECT DISTINCT
[UnionAll1].[C1] AS [C1],
[UnionAll1].[Id] AS [C2]
FROM (SELECT TOP (1000)
[Project1].[C1] AS [C1],
[Project1].[Id] AS [Id]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[mtime] AS [mtime], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[mtime] DESC) AS [row_number]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[mtime] AS [mtime],
1 AS [C1]
FROM [dbo].[ContentItem] AS [Extent1]
WHERE 0 = CAST( [Extent1].[Type] AS int)
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[mtime] DESC
UNION ALL
SELECT TOP (1000)
[Project3].[C1] AS [C1],
[Project3].[Id] AS [Id]
FROM ( SELECT [Project3].[Id] AS [Id], [Project3].[mtime] AS [mtime], [Project3].[C1] AS [C1], row_number() OVER (ORDER BY [Project3].[mtime] DESC) AS [row_number]
FROM ( SELECT
[Extent2].[Id] AS [Id],
[Extent2].[mtime] AS [mtime],
1 AS [C1]
FROM [dbo].[ContentItem] AS [Extent2]
WHERE 1 = CAST( [Extent2].[Type] AS int)
) AS [Project3]
) AS [Project3]
WHERE [Project3].[row_number] > 0
ORDER BY [Project3].[mtime] DESC) AS [UnionAll1]
) AS [Distinct1]
UNION ALL
SELECT TOP (1000)
[Project7].[C1] AS [C1],
[Project7].[Id] AS [Id]
FROM ( SELECT [Project7].[Id] AS [Id], [Project7].[mtime] AS [mtime], [Project7].[C1] AS [C1], row_number() OVER (ORDER BY [Project7].[mtime] DESC) AS [row_number]
FROM ( SELECT
[Extent3].[Id] AS [Id],
[Extent3].[mtime] AS [mtime],
1 AS [C1]
FROM [dbo].[ContentItem] AS [Extent3]
WHERE 2 = CAST( [Extent3].[Type] AS int)
) AS [Project7]
) AS [Project7]
WHERE [Project7].[row_number] > 0
ORDER BY [Project7].[mtime] DESC) AS [UnionAll2]
) AS [Distinct2]
UNION ALL
SELECT TOP (1000)
[Project11].[C1] AS [C1],
[Project11].[Id] AS [Id]
FROM ( SELECT [Project11].[Id] AS [Id], [Project11].[mtime] AS [mtime], [Project11].[C1] AS [C1], row_number() OVER (ORDER BY [Project11].[mtime] DESC) AS [row_number]
FROM ( SELECT
[Extent4].[Id] AS [Id],
[Extent4].[mtime] AS [mtime],
1 AS [C1]
FROM [dbo].[ContentItem] AS [Extent4]
WHERE 3 = CAST( [Extent4].[Type] AS int)
) AS [Project11]
) AS [Project11]
WHERE [Project11].[row_number] > 0
ORDER BY [Project11].[mtime] DESC) AS [UnionAll3]
) AS [Distinct3]
UNION ALL
SELECT TOP (1000)
[Project15].[C1] AS [C1],
[Project15].[Id] AS [Id]
FROM ( SELECT [Project15].[Id] AS [Id], [Project15].[mtime] AS [mtime], [Project15].[C1] AS [C1], row_number() OVER (ORDER BY [Project15].[mtime] DESC) AS [row_number]
FROM ( SELECT
[Extent5].[Id] AS [Id],
[Extent5].[mtime] AS [mtime],
1 AS [C1]
FROM [dbo].[ContentItem] AS [Extent5]
WHERE 4 = CAST( [Extent5].[Type] AS int)
) AS [Project15]
) AS [Project15]
WHERE [Project15].[row_number] > 0
ORDER BY [Project15].[mtime] DESC) AS [UnionAll4] ) AS [Distinct4]
LEFT OUTER JOIN [dbo].[Retailer] AS [Extent6] ON [Distinct4].[C2] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[Brand] AS [Extent7] ON [Distinct4].[C2] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[Product] AS [Extent8] ON [Distinct4].[C2] = [Extent8].[Id]
LEFT OUTER JOIN [dbo].[Certification] AS [Extent9] ON [Distinct4].[C2] = [Extent9].[Id]
LEFT OUTER JOIN [dbo].[Claim] AS [Extent10] ON [Distinct4].[C2] = [Extent10].[Id]
So my overall questions are:
1) Is there a simpler SQL query I can execute to get the same results? I know that T-SQL doesn't support offsets per table in a subquery, hence the subquery wrapping.
2) If there isn't, what am I doing wrong in my LINQ query? Is this even possible with LINQ?
I wanted to add the SQL from #radar here all nice and formatted. It at least appears to be an elegant solution to avoid the sub-subqueries, and still accomplishes the offset/fetch.
SELECT *
FROM (SELECT
[ContentItem].*,
row_number() OVER ( PARTITION BY Type ORDER BY ContentItem.mtime ) as rn
FROM [dbo].[ContentItem]
LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id)
) as x
WHERE x.rn >= a AND x.rn <= b;
a is the lower threshold (offset) and b is the upper threshold (fetch-ish). The only catch is that b now equals fetch + a instead of just fetch. The first set of results would be WHERE x.rn >= 0 AND x.rn <= 6, the second set WHERE x.rn >= 6 AND x.rn <= 12, third WHERE x.rn >= 12 AND x.rn <= 18, and so on.
As you are looking simpler SQL, you can use row_number analytic function, which would be faster
You need to try and see as there are many left joins and also proper index need to exists in these tables.
select *
from (
select *, row_number() over ( partition by Type order by ContentItem.mtime ) as rn
from [dbo].[ContentItem]
LEFT JOIN [dbo].[Retailer] ON (Retailer.Id = ContentItem.Id)
LEFT JOIN [dbo].[Brand] ON (Brand.Id = ContentItem.Id)
LEFT JOIN [dbo].[Product] ON (Product.Id = ContentItem.Id)
LEFT JOIN [dbo].[Certification] ON (Certification.Id = ContentItem.Id)
LEFT JOIN [dbo].[Claim] ON (Claim.Id = ContentItem.Id);
)
where rn <= 6
Well, it appears that I'm an idiot. That TOP(1000) call should have tipped me off. I assumed that my take variable was set to 6 but it was, in fact, set to 1000. Turns out my giant LINQ query works as expected, but the nested UNION ALL statements threw me off.
Still, I'm going to investigate #radar's answer further. It's hard to argue with better performance.

Entity Framework 6 too many joins

I have the following LINQ query:
var query = from a in c.ArticleSet
where a.GlobalAccess == false &&
a.Published == true &&
a.MagazineSet.IsPublished == true &&
a.MagazineSet.Press_Id == pressId &&
a.Tests.Id==a.Id &&
a.Tests.IsDeleted == false &&
a.Tests.IsPublished == true
orderby a.Id descending
select a.Id;
It tranforms to SQL like this:
ADO.NET:Execute Reader "SELECT
[Project1].[Id] AS [Id]
FROM ( SELECT
[Filter3].[Id1] AS [Id]
FROM (SELECT [Filter2].[Id1], [Filter2].[Magazine_Id1], [Filter2].[Press_Id1], [Filter2].[Press_Id2], [Filter2].[Press_Id3]
FROM (SELECT [Filter1].[Id1], [Filter1].[Magazine_Id1], [Extent5].[Press_Id] AS [Press_Id1], [Extent6].[Press_Id] AS [Press_Id2], [Extent7].[Press_Id] AS [Press_Id3]
FROM (SELECT [Extent1].[Id] AS [Id1], [Extent1].[Magazine_Id] AS [Magazine_Id1]
FROM [dbo].[ArticleSet] AS [Extent1]
INNER JOIN [dbo].[Tests] AS [Extent2] ON ([Extent1].[Id] = [Extent2].[Id]) AND ([Extent2].[Id] = [Extent1].[Id])
INNER JOIN [dbo].[MagazineSet] AS [Extent3] ON [Extent1].[Magazine_Id] = [Extent3].[Id]
WHERE (0 = [Extent1].[GlobalAccess]) AND (1 = [Extent1].[Published]) AND (1 = [Extent3].[IsPublished]) ) AS [Filter1]
INNER JOIN [dbo].[MagazineSet] AS [Extent4] ON [Filter1].[Magazine_Id1] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent5] ON [Filter1].[Magazine_Id1] = [Extent5].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent6] ON [Filter1].[Magazine_Id1] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent7] ON [Filter1].[Magazine_Id1] = [Extent7].[Id]
INNER JOIN [dbo].[Tests] AS [Extent8] ON [Filter1].[Id1] = [Extent8].[Id]
WHERE 0 = [Extent8].[IsDeleted] ) AS [Filter2]
INNER JOIN [dbo].[Tests] AS [Extent9] ON [Filter2].[Id1] = [Extent9].[Id]
INNER JOIN [dbo].[Tests] AS [Extent10] ON [Filter2].[Id1] = [Extent10].[Id]
WHERE 1 = [Extent10].[IsPublished] ) AS [Filter3]
INNER JOIN [dbo].[Tests] AS [Extent11] ON [Filter3].[Id1] = [Extent11].[Id]
WHERE (([Filter3].[Press_Id1] = #p__linq__0) AND ( NOT ([Filter3].[Press_Id2] IS NULL OR #p__linq__0 IS NULL))) OR (([Filter3].[Press_Id3] IS NULL) AND (#p__linq__0 IS NULL))
) AS [Project1]
ORDER BY [Project1].[Id] DESC"
The command text
"SELECT [Project1].[Id] AS [Id]
FROM ( SELECT
[Filter3].[Id1] AS [Id]
FROM (SELECT [Filter2].[Id1], [Filter2].[Magazine_Id1], [Filter2].[Press_Id1], [Filter2].[Press_Id2], [Filter2].[Press_Id3]
FROM (SELECT [Filter1].[Id1], [Filter1].[Magazine_Id1], [Extent5].[Press_Id] AS [Press_Id1], [Extent6].[Press_Id] AS [Press_Id2], [Extent7].[Press_Id] AS [Press_Id3]
FROM (SELECT [Extent1].[Id] AS [Id1], [Extent1].[Magazine_Id] AS [Magazine_Id1]
FROM [dbo].[ArticleSet] AS [Extent1]
INNER JOIN [dbo].[Tests] AS [Extent2] ON ([Extent1].[Id] = [Extent2].[Id]) AND ([Extent2].[Id] = [Extent1].[Id])
INNER JOIN [dbo].[MagazineSet] AS [Extent3] ON [Extent1].[Magazine_Id] = [Extent3].[Id]
WHERE (0 = [Extent1].[GlobalAccess]) AND (1 = [Extent1].[Published]) AND (1 = [Extent3].[IsPublished]) ) AS [Filter1]
INNER JOIN [dbo].[MagazineSet] AS [Extent4] ON [Filter1].[Magazine_Id1] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent5] ON [Filter1].[Magazine_Id1] = [Extent5].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent6] ON [Filter1].[Magazine_Id1] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[MagazineSet] AS [Extent7] ON [Filter1].[Magazine_Id1] = [Extent7].[Id]
INNER JOIN [dbo].[Tests] AS [Extent8] ON [Filter1].[Id1] = [Extent8].[Id]
WHERE 0 = [Extent8].[IsDeleted] ) AS [Filter2]
INNER JOIN [dbo].[Tests] AS [Extent9] ON [Filter2].[Id1] = [Extent9].[Id]
INNER JOIN [dbo].[Tests] AS [Extent10] ON [Filter2].[Id1] = [Extent10].[Id]
WHERE 1 = [Extent10].[IsPublished] ) AS [Filter3]
INNER JOIN [dbo].[Tests] AS [Extent11] ON [Filter3].[Id1] = [Extent11].[Id]
WHERE (([Filter3].[Press_Id1] = #p__linq__0) AND ( NOT ([Filter3].[Press_Id2] IS NULL OR #p__linq__0 IS NULL))) OR (([Filter3].[Press_Id3] IS NULL) AND (#p__linq__0 IS NULL))
) AS [Project1]
ORDER BY [Project1].[Id] DESC
I think 3 join is enough in this example.
Why does Entity framework create such huge query?
Your linq query is fine, other than the fact that it references a lot of tables at once. If this is really what you want, there there is nothing to be afraid of. It's only not "super efficient" because you are referencing 3 tables at once.
Just remember that the sql query that EF produces is machine-created. It's not always the best, but it should alway be perfectly valid. I'm not sure why it chose this form over another, but if it works, why worry?
I always suggest that people limit their queries to only the referencing tables they need. As the system grows, the processing time will increase as well, and with this many table joins, it might start to get more noticeable.

How can paging a query be *slower*?

I'm timing the execution time of both a query, and the same query, paged.
foreach (var x in productSource.OrderBy(p => p.AdminDisplayName)
.Where(p => allIds.Any(val => val == p.SiteProductId))) ;
foreach (var x in productSource.OrderBy(p => p.AdminDisplayName)
.Where(p => allIds.Any(val => val == p.SiteProductId)).Skip(20).Take(20)) ;
Somehow, the first query is taking 0.5 seconds, and the second is taking three times that long. How is that possible? Unfortunately allIds is fairly complex, so the SQL generated is quite long. I'm using Linq-to-SQL, which is why I used Any instead of Contains, since the latter causes an error for complex queries like this.
EDIT
It looks like the paged query runs faster (in absolute times) when the returned result set is larger. When the base query returns 6,000 rows (before paging) the paged version runs at 1.7 seconds. When the base query returns 200 rows (before paging) the paged version runs at 1.7 seconds. This seems crazy to me.
EDIT 2
I was asked to supply query execution plans. I've looked through both of them, and they appear to be identical, except for the very beginning. Here are the parts that actually differ.
NOT PAGED
PAGED
END EDIT
SELECT [t0].[SiteProductId], [t0].[SiteId], [t0].[SiteDivisionId], [t0].[ProductDisplayId], [t0].[ItemId], [t0].[SiteProductTypeId], [t0].[PrimaryParentSiteCategoryId], [t0].[PrimaryParentSiteProductId], [t0].[PrimaryChildSiteProductId], [t0].[UsesMasterPrice], [t0].[ListPrice], [t0].[SalePrice], [t0].[ShowWasIsPricing], [t0].[ArrivalDate], [t0].[SiteUrlKey], [t0].[IsDisplayedOnIndexPages], [t0].[HasDetailPage], [t0].[IsPersonalizable], [t0].[RequiresPersonalization], [t0].[ShowPersonalizationInline], [t0].[PzTemplateId], [t0].[AdminDisplayName], [t0].[DetailPageHeading], [t0].[SiteLabelForIndex], [t0].[SiteLabelForDetail], [t0].[UsesVariantAttributes], [t0].[VariantSelectionPrompt], [t0].[VariantSelectionOptionLabel], [t0].[VariantSortOrder], [t0].[VariantSelectionImageAssignmentId], [t0].[IndexImageAssignmentId], [t0].[DetailImageAssignmentId], [t0].[SiteProductDescription], [t0].[SiteTargetSearchTerms], [t0].[SiteWebPageTitle], [t0].[SiteWebPageKeywords], [t0].[SiteWebPageDescription], [t0].[ItemStatusId], [t0].[UsesMasterInventory], [t0].[CurrentInventory], [t0].[RestockDate], [t0].[IsBackorderable], [t0].[IsPreorderable], [t0].[OutOfStockLevel], [t0].[CreatedBy], [t0].[CreatedDT], [t0].[ModifiedBy], [t0].[ModifiedDT], [t0].[UsesPixamiPreview], [t0].[ShowsDynamicPreview], [t0].[IsNewProduct], [t0].[IsExclusiveProduct], [t0].[IsInternetOnlyProduct], [t0].[IsCustomerFavorite], [t0].[StartDate], [t0].[EndDate], [t0].[InternalKeywords], [t0].[ProductAlert], [t0].[AdditionalProductInfo], [t0].[UsesPixamiPz], [t0].[IsFreeGift], [t2].[test], [t2].[ItemId] AS [ItemId2], [t2].[ItemSku], [t2].[ErpItemId], [t2].[SupplierSku], [t2].[VendorSku], [t2].[UPC], [t2].[SerialNumber], [t2].[DisplayName], [t2].[IsPersonalizable] AS [IsPersonalizable2], [t2].[RequiresPersonalization] AS [RequiresPersonalization2], [t2].[ListPrice] AS [ListPrice2], [t2].[ItemTypeId], [t2].[ItemTypeCode], [t2].[DisplayIndividuallyOnSite], [t2].[ItemStatusId] AS [ItemStatusId2], [t2].[ItemStatusCode], [t2].[ParentItemId], [t2].[VariantTemplateCode], [t2].[PzFormatCode], [t2].[OmsPzTemplateId], [t2].[Height], [t2].[Width], [t2].[Depth], [t2].[Weight], [t2].[CurrentInventory] AS [CurrentInventory2], [t2].[RestockDate] AS [RestockDate2], [t2].[IsTaxable], [t2].[PostHand], [t2].[LastSyncDate], [t2].[CreatedBy] AS [CreatedBy2], [t2].[CreatedDT] AS [CreatedDT2], [t2].[ModifiedBy] AS [ModifiedBy2], [t2].[ModifiedDT] AS [ModifiedDT2]
FROM [dbo].[SiteProduct] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[ItemId], [t1].[ItemSku], [t1].[ErpItemId], [t1].[SupplierSku], [t1].[VendorSku], [t1].[UPC], [t1].[SerialNumber], [t1].[DisplayName], [t1].[IsPersonalizable], [t1].[RequiresPersonalization], [t1].[ListPrice], [t1].[ItemTypeId], [t1].[ItemTypeCode], [t1].[DisplayIndividuallyOnSite], [t1].[ItemStatusId], [t1].[ItemStatusCode], [t1].[ParentItemId], [t1].[VariantTemplateCode], [t1].[PzFormatCode], [t1].[OmsPzTemplateId], [t1].[Height], [t1].[Width], [t1].[Depth], [t1].[Weight], [t1].[CurrentInventory], [t1].[RestockDate], [t1].[IsTaxable], [t1].[PostHand], [t1].[LastSyncDate], [t1].[CreatedBy], [t1].[CreatedDT], [t1].[ModifiedBy], [t1].[ModifiedDT]
FROM [dbo].[ItemMaster] AS [t1]
) AS [t2] ON [t2].[ItemId] = [t0].[ItemId]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t43].[SiteProductId]
FROM (
SELECT [t28].[SiteProductId]
FROM (
SELECT [t13].[SiteProductId]
FROM (
SELECT [t3].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t3]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t4] ON [t4].[ItemId] = [t3].[ItemId]
WHERE ([t3].[SiteProductTypeId] <> #p0) AND (([t3].[AdminDisplayName] LIKE #p1) OR ([t4].[ItemSku] LIKE #p2)) AND ([t3].[SiteId] = #p3)
UNION
SELECT [t12].[SiteProductId]
FROM (
SELECT [t5].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t5]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t6] ON [t6].[ItemId] = [t5].[ItemId]
WHERE ([t5].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t7].[PrimaryParentSiteProductId] AS [value], [t7].[PrimaryParentSiteProductId], [t7].[SiteProductTypeId], [t7].[AdminDisplayName], [t8].[ItemSku], [t7].[SiteId]
FROM [dbo].[SiteProduct] AS [t7]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t8] ON [t8].[ItemId] = [t7].[ItemId]
) AS [t9]
WHERE ([t9].[value] = ([t5].[PrimaryParentSiteProductId])) AND ([t9].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t9].[SiteProductTypeId] <> #p4) AND (([t9].[AdminDisplayName] LIKE #p5) OR ([t9].[ItemSku] LIKE #p6)) AND ([t9].[SiteId] = #p7)
))
UNION
SELECT [t10].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t10]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t11] ON [t11].[ItemId] = [t10].[ItemId]
WHERE ([t10].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t10].[SiteProductTypeId] <> #p8) AND (([t10].[AdminDisplayName] LIKE #p9) OR ([t11].[ItemSku] LIKE #p10)) AND ([t10].[SiteId] = #p11)
) AS [t12]
) AS [t13]
UNION
SELECT [t14].[ParentSiteProductId]
FROM [dbo].[SiteProductAssociation] AS [t14]
INNER JOIN [dbo].[SiteProductAssociationType] AS [t15] ON [t15].[SiteProductAssociationTypeId] = [t14].[SiteProductAssociationTypeId]
WHERE (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t26].[SiteProductId]
FROM (
SELECT [t16].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t16]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t17] ON [t17].[ItemId] = [t16].[ItemId]
WHERE ([t16].[SiteProductTypeId] <> #p12) AND (([t16].[AdminDisplayName] LIKE #p13) OR ([t17].[ItemSku] LIKE #p14)) AND ([t16].[SiteId] = #p15)
UNION
SELECT [t25].[SiteProductId]
FROM (
SELECT [t18].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t18]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t19] ON [t19].[ItemId] = [t18].[ItemId]
WHERE ([t18].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t20].[PrimaryParentSiteProductId] AS [value], [t20].[PrimaryParentSiteProductId], [t20].[SiteProductTypeId], [t20].[AdminDisplayName], [t21].[ItemSku], [t20].[SiteId]
FROM [dbo].[SiteProduct] AS [t20]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t21] ON [t21].[ItemId] = [t20].[ItemId]
) AS [t22]
WHERE ([t22].[value] = ([t18].[PrimaryParentSiteProductId])) AND ([t22].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t22].[SiteProductTypeId] <> #p16) AND (([t22].[AdminDisplayName] LIKE #p17) OR ([t22].[ItemSku] LIKE #p18)) AND ([t22].[SiteId] = #p19)
))
UNION
SELECT [t23].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t23]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t24] ON [t24].[ItemId] = [t23].[ItemId]
WHERE ([t23].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t23].[SiteProductTypeId] <> #p20) AND (([t23].[AdminDisplayName] LIKE #p21) OR ([t24].[ItemSku] LIKE #p22)) AND ([t23].[SiteId] = #p23)
) AS [t25]
) AS [t26]
) AS [t27]
WHERE [t27].[SiteProductId] = [t14].[ChildSiteProductId]
)) AND ([t14].[SiteProductAssociationTypeId] = #p24)
) AS [t28]
UNION
SELECT [t29].[ChildSiteProductId]
FROM [dbo].[SiteProductAssociation] AS [t29]
INNER JOIN [dbo].[SiteProductAssociationType] AS [t30] ON [t30].[SiteProductAssociationTypeId] = [t29].[SiteProductAssociationTypeId]
WHERE (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t41].[SiteProductId]
FROM (
SELECT [t31].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t31]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t32] ON [t32].[ItemId] = [t31].[ItemId]
WHERE ([t31].[SiteProductTypeId] <> #p25) AND (([t31].[AdminDisplayName] LIKE #p26) OR ([t32].[ItemSku] LIKE #p27)) AND ([t31].[SiteId] = #p28)
UNION
SELECT [t40].[SiteProductId]
FROM (
SELECT [t33].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t33]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t34] ON [t34].[ItemId] = [t33].[ItemId]
WHERE ([t33].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t35].[PrimaryParentSiteProductId] AS [value], [t35].[PrimaryParentSiteProductId], [t35].[SiteProductTypeId], [t35].[AdminDisplayName], [t36].[ItemSku], [t35].[SiteId]
FROM [dbo].[SiteProduct] AS [t35]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t36] ON [t36].[ItemId] = [t35].[ItemId]
) AS [t37]
WHERE ([t37].[value] = ([t33].[PrimaryParentSiteProductId])) AND ([t37].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t37].[SiteProductTypeId] <> #p29) AND (([t37].[AdminDisplayName] LIKE #p30) OR ([t37].[ItemSku] LIKE #p31)) AND ([t37].[SiteId] = #p32)
))
UNION
SELECT [t38].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t38]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t39] ON [t39].[ItemId] = [t38].[ItemId]
WHERE ([t38].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t38].[SiteProductTypeId] <> #p33) AND (([t38].[AdminDisplayName] LIKE #p34) OR ([t39].[ItemSku] LIKE #p35)) AND ([t38].[SiteId] = #p36)
) AS [t40]
) AS [t41]
) AS [t42]
WHERE [t42].[SiteProductId] = [t29].[ParentSiteProductId]
)) AND ([t29].[SiteProductAssociationTypeId] = #p37)
) AS [t43]
) AS [t44]
WHERE [t44].[SiteProductId] = [t0].[SiteProductId]
)
ORDER BY [t0].[AdminDisplayName]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
SELECT [t45].[SiteProductId], [t45].[SiteId], [t45].[SiteDivisionId], [t45].[ProductDisplayId], [t45].[ItemId], [t45].[SiteProductTypeId], [t45].[PrimaryParentSiteCategoryId], [t45].[PrimaryParentSiteProductId], [t45].[PrimaryChildSiteProductId], [t45].[UsesMasterPrice], [t45].[ListPrice], [t45].[SalePrice], [t45].[ShowWasIsPricing], [t45].[ArrivalDate], [t45].[SiteUrlKey], [t45].[IsDisplayedOnIndexPages], [t45].[HasDetailPage], [t45].[IsPersonalizable], [t45].[RequiresPersonalization], [t45].[ShowPersonalizationInline], [t45].[PzTemplateId], [t45].[AdminDisplayName], [t45].[DetailPageHeading], [t45].[SiteLabelForIndex], [t45].[SiteLabelForDetail], [t45].[UsesVariantAttributes], [t45].[VariantSelectionPrompt], [t45].[VariantSelectionOptionLabel], [t45].[VariantSortOrder], [t45].[VariantSelectionImageAssignmentId], [t45].[IndexImageAssignmentId], [t45].[DetailImageAssignmentId], [t45].[SiteProductDescription], [t45].[SiteTargetSearchTerms], [t45].[SiteWebPageTitle], [t45].[SiteWebPageKeywords], [t45].[SiteWebPageDescription], [t45].[ItemStatusId], [t45].[UsesMasterInventory], [t45].[CurrentInventory], [t45].[RestockDate], [t45].[IsBackorderable], [t45].[IsPreorderable], [t45].[OutOfStockLevel], [t45].[CreatedBy], [t45].[CreatedDT], [t45].[ModifiedBy], [t45].[ModifiedDT], [t45].[UsesPixamiPreview], [t45].[ShowsDynamicPreview], [t45].[IsNewProduct], [t45].[IsExclusiveProduct], [t45].[IsInternetOnlyProduct], [t45].[IsCustomerFavorite], [t45].[StartDate], [t45].[EndDate], [t45].[InternalKeywords], [t45].[ProductAlert], [t45].[AdditionalProductInfo], [t45].[UsesPixamiPz], [t45].[IsFreeGift], [t45].[test], [t45].[ItemId2], [t45].[ItemSku], [t45].[ErpItemId], [t45].[SupplierSku], [t45].[VendorSku], [t45].[UPC], [t45].[SerialNumber], [t45].[DisplayName], [t45].[IsPersonalizable2], [t45].[RequiresPersonalization2], [t45].[ListPrice2], [t45].[ItemTypeId], [t45].[ItemTypeCode], [t45].[DisplayIndividuallyOnSite], [t45].[ItemStatusId2], [t45].[ItemStatusCode], [t45].[ParentItemId], [t45].[VariantTemplateCode], [t45].[PzFormatCode], [t45].[OmsPzTemplateId], [t45].[Height], [t45].[Width], [t45].[Depth], [t45].[Weight], [t45].[CurrentInventory2], [t45].[RestockDate2], [t45].[IsTaxable], [t45].[PostHand], [t45].[LastSyncDate], [t45].[CreatedBy2], [t45].[CreatedDT2], [t45].[ModifiedBy2], [t45].[ModifiedDT2]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[AdminDisplayName]) AS [ROW_NUMBER], [t0].[SiteProductId], [t0].[SiteId], [t0].[SiteDivisionId], [t0].[ProductDisplayId], [t0].[ItemId], [t0].[SiteProductTypeId], [t0].[PrimaryParentSiteCategoryId], [t0].[PrimaryParentSiteProductId], [t0].[PrimaryChildSiteProductId], [t0].[UsesMasterPrice], [t0].[ListPrice], [t0].[SalePrice], [t0].[ShowWasIsPricing], [t0].[ArrivalDate], [t0].[SiteUrlKey], [t0].[IsDisplayedOnIndexPages], [t0].[HasDetailPage], [t0].[IsPersonalizable], [t0].[RequiresPersonalization], [t0].[ShowPersonalizationInline], [t0].[PzTemplateId], [t0].[AdminDisplayName], [t0].[DetailPageHeading], [t0].[SiteLabelForIndex], [t0].[SiteLabelForDetail], [t0].[UsesVariantAttributes], [t0].[VariantSelectionPrompt], [t0].[VariantSelectionOptionLabel], [t0].[VariantSortOrder], [t0].[VariantSelectionImageAssignmentId], [t0].[IndexImageAssignmentId], [t0].[DetailImageAssignmentId], [t0].[SiteProductDescription], [t0].[SiteTargetSearchTerms], [t0].[SiteWebPageTitle], [t0].[SiteWebPageKeywords], [t0].[SiteWebPageDescription], [t0].[ItemStatusId], [t0].[UsesMasterInventory], [t0].[CurrentInventory], [t0].[RestockDate], [t0].[IsBackorderable], [t0].[IsPreorderable], [t0].[OutOfStockLevel], [t0].[CreatedBy], [t0].[CreatedDT], [t0].[ModifiedBy], [t0].[ModifiedDT], [t0].[UsesPixamiPreview], [t0].[ShowsDynamicPreview], [t0].[IsNewProduct], [t0].[IsExclusiveProduct], [t0].[IsInternetOnlyProduct], [t0].[IsCustomerFavorite], [t0].[StartDate], [t0].[EndDate], [t0].[InternalKeywords], [t0].[ProductAlert], [t0].[AdditionalProductInfo], [t0].[UsesPixamiPz], [t0].[IsFreeGift], [t2].[test], [t2].[ItemId] AS [ItemId2], [t2].[ItemSku], [t2].[ErpItemId], [t2].[SupplierSku], [t2].[VendorSku], [t2].[UPC], [t2].[SerialNumber], [t2].[DisplayName], [t2].[IsPersonalizable] AS [IsPersonalizable2], [t2].[RequiresPersonalization] AS [RequiresPersonalization2], [t2].[ListPrice] AS [ListPrice2], [t2].[ItemTypeId], [t2].[ItemTypeCode], [t2].[DisplayIndividuallyOnSite], [t2].[ItemStatusId] AS [ItemStatusId2], [t2].[ItemStatusCode], [t2].[ParentItemId], [t2].[VariantTemplateCode], [t2].[PzFormatCode], [t2].[OmsPzTemplateId], [t2].[Height], [t2].[Width], [t2].[Depth], [t2].[Weight], [t2].[CurrentInventory] AS [CurrentInventory2], [t2].[RestockDate] AS [RestockDate2], [t2].[IsTaxable], [t2].[PostHand], [t2].[LastSyncDate], [t2].[CreatedBy] AS [CreatedBy2], [t2].[CreatedDT] AS [CreatedDT2], [t2].[ModifiedBy] AS [ModifiedBy2], [t2].[ModifiedDT] AS [ModifiedDT2]
FROM [dbo].[SiteProduct] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[ItemId], [t1].[ItemSku], [t1].[ErpItemId], [t1].[SupplierSku], [t1].[VendorSku], [t1].[UPC], [t1].[SerialNumber], [t1].[DisplayName], [t1].[IsPersonalizable], [t1].[RequiresPersonalization], [t1].[ListPrice], [t1].[ItemTypeId], [t1].[ItemTypeCode], [t1].[DisplayIndividuallyOnSite], [t1].[ItemStatusId], [t1].[ItemStatusCode], [t1].[ParentItemId], [t1].[VariantTemplateCode], [t1].[PzFormatCode], [t1].[OmsPzTemplateId], [t1].[Height], [t1].[Width], [t1].[Depth], [t1].[Weight], [t1].[CurrentInventory], [t1].[RestockDate], [t1].[IsTaxable], [t1].[PostHand], [t1].[LastSyncDate], [t1].[CreatedBy], [t1].[CreatedDT], [t1].[ModifiedBy], [t1].[ModifiedDT]
FROM [dbo].[ItemMaster] AS [t1]
) AS [t2] ON [t2].[ItemId] = [t0].[ItemId]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t43].[SiteProductId]
FROM (
SELECT [t28].[SiteProductId]
FROM (
SELECT [t13].[SiteProductId]
FROM (
SELECT [t3].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t3]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t4] ON [t4].[ItemId] = [t3].[ItemId]
WHERE ([t3].[SiteProductTypeId] <> #p0) AND (([t3].[AdminDisplayName] LIKE #p1) OR ([t4].[ItemSku] LIKE #p2)) AND ([t3].[SiteId] = #p3)
UNION
SELECT [t12].[SiteProductId]
FROM (
SELECT [t5].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t5]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t6] ON [t6].[ItemId] = [t5].[ItemId]
WHERE ([t5].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t7].[PrimaryParentSiteProductId] AS [value], [t7].[PrimaryParentSiteProductId], [t7].[SiteProductTypeId], [t7].[AdminDisplayName], [t8].[ItemSku], [t7].[SiteId]
FROM [dbo].[SiteProduct] AS [t7]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t8] ON [t8].[ItemId] = [t7].[ItemId]
) AS [t9]
WHERE ([t9].[value] = ([t5].[PrimaryParentSiteProductId])) AND ([t9].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t9].[SiteProductTypeId] <> #p4) AND (([t9].[AdminDisplayName] LIKE #p5) OR ([t9].[ItemSku] LIKE #p6)) AND ([t9].[SiteId] = #p7)
))
UNION
SELECT [t10].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t10]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t11] ON [t11].[ItemId] = [t10].[ItemId]
WHERE ([t10].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t10].[SiteProductTypeId] <> #p8) AND (([t10].[AdminDisplayName] LIKE #p9) OR ([t11].[ItemSku] LIKE #p10)) AND ([t10].[SiteId] = #p11)
) AS [t12]
) AS [t13]
UNION
SELECT [t14].[ParentSiteProductId]
FROM [dbo].[SiteProductAssociation] AS [t14]
INNER JOIN [dbo].[SiteProductAssociationType] AS [t15] ON [t15].[SiteProductAssociationTypeId] = [t14].[SiteProductAssociationTypeId]
WHERE (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t26].[SiteProductId]
FROM (
SELECT [t16].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t16]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t17] ON [t17].[ItemId] = [t16].[ItemId]
WHERE ([t16].[SiteProductTypeId] <> #p12) AND (([t16].[AdminDisplayName] LIKE #p13) OR ([t17].[ItemSku] LIKE #p14)) AND ([t16].[SiteId] = #p15)
UNION
SELECT [t25].[SiteProductId]
FROM (
SELECT [t18].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t18]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t19] ON [t19].[ItemId] = [t18].[ItemId]
WHERE ([t18].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t20].[PrimaryParentSiteProductId] AS [value], [t20].[PrimaryParentSiteProductId], [t20].[SiteProductTypeId], [t20].[AdminDisplayName], [t21].[ItemSku], [t20].[SiteId]
FROM [dbo].[SiteProduct] AS [t20]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t21] ON [t21].[ItemId] = [t20].[ItemId]
) AS [t22]
WHERE ([t22].[value] = ([t18].[PrimaryParentSiteProductId])) AND ([t22].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t22].[SiteProductTypeId] <> #p16) AND (([t22].[AdminDisplayName] LIKE #p17) OR ([t22].[ItemSku] LIKE #p18)) AND ([t22].[SiteId] = #p19)
))
UNION
SELECT [t23].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t23]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t24] ON [t24].[ItemId] = [t23].[ItemId]
WHERE ([t23].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t23].[SiteProductTypeId] <> #p20) AND (([t23].[AdminDisplayName] LIKE #p21) OR ([t24].[ItemSku] LIKE #p22)) AND ([t23].[SiteId] = #p23)
) AS [t25]
) AS [t26]
) AS [t27]
WHERE [t27].[SiteProductId] = [t14].[ChildSiteProductId]
)) AND ([t14].[SiteProductAssociationTypeId] = #p24)
) AS [t28]
UNION
SELECT [t29].[ChildSiteProductId]
FROM [dbo].[SiteProductAssociation] AS [t29]
INNER JOIN [dbo].[SiteProductAssociationType] AS [t30] ON [t30].[SiteProductAssociationTypeId] = [t29].[SiteProductAssociationTypeId]
WHERE (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t41].[SiteProductId]
FROM (
SELECT [t31].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t31]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t32] ON [t32].[ItemId] = [t31].[ItemId]
WHERE ([t31].[SiteProductTypeId] <> #p25) AND (([t31].[AdminDisplayName] LIKE #p26) OR ([t32].[ItemSku] LIKE #p27)) AND ([t31].[SiteId] = #p28)
UNION
SELECT [t40].[SiteProductId]
FROM (
SELECT [t33].[SiteProductId]
FROM [dbo].[SiteProduct] AS [t33]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t34] ON [t34].[ItemId] = [t33].[ItemId]
WHERE ([t33].[PrimaryParentSiteProductId] IS NOT NULL) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t35].[PrimaryParentSiteProductId] AS [value], [t35].[PrimaryParentSiteProductId], [t35].[SiteProductTypeId], [t35].[AdminDisplayName], [t36].[ItemSku], [t35].[SiteId]
FROM [dbo].[SiteProduct] AS [t35]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t36] ON [t36].[ItemId] = [t35].[ItemId]
) AS [t37]
WHERE ([t37].[value] = ([t33].[PrimaryParentSiteProductId])) AND ([t37].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t37].[SiteProductTypeId] <> #p29) AND (([t37].[AdminDisplayName] LIKE #p30) OR ([t37].[ItemSku] LIKE #p31)) AND ([t37].[SiteId] = #p32)
))
UNION
SELECT [t38].[PrimaryParentSiteProductId] AS [value]
FROM [dbo].[SiteProduct] AS [t38]
LEFT OUTER JOIN [dbo].[ItemMaster] AS [t39] ON [t39].[ItemId] = [t38].[ItemId]
WHERE ([t38].[PrimaryParentSiteProductId] IS NOT NULL) AND ([t38].[SiteProductTypeId] <> #p33) AND (([t38].[AdminDisplayName] LIKE #p34) OR ([t39].[ItemSku] LIKE #p35)) AND ([t38].[SiteId] = #p36)
) AS [t40]
) AS [t41]
) AS [t42]
WHERE [t42].[SiteProductId] = [t29].[ParentSiteProductId]
)) AND ([t29].[SiteProductAssociationTypeId] = #p37)
) AS [t43]
) AS [t44]
WHERE [t44].[SiteProductId] = [t0].[SiteProductId]
)
) AS [t45]
WHERE [t45].[ROW_NUMBER] BETWEEN #p38 + 1 AND #p38 + #p39
ORDER BY [t45].[ROW_NUMBER]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
Instead of using the "select top X from" syntax, the generated SQL uses the ROW_NUMBER() function, and than selects based on it.
It is of course a very heavy operation, as the function is called for every row, and only then is the "WHERE" clause evaluated, while the "SELECT TOP X" syntax just stops execution after X rows are selected.
I can't tell you if it's possible to fix this in LINQ2SQL, but I am quite sure Entity Framework uses "SELECT TOP X FROM" syntax, although I can't tell you for sure.
I don't know how complicated is it for you to move to Entity Framework, but I can tell you for sure it will make your life easier in many ways, and will perform much better, provided you use the .NET 4.0 Entity Framework and not .NET 3.5 SP1.

Categories

Resources