Can anybody please tell why would this query take minutes to execute?
Select
d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
d.IDate, d.InstrumentID, d.DocumentStatusID,ig.Abbreviation as IGroupAbbreviation,
u.Username, j.JDAbbreviation, inf.DocumentName,
it.Abbreviation as ITypeAbbreviation, d.DocumentDate,
ds.Abbreviation as DocumentStatusAbbreviation
From Documents d
Inner Join IGroupes ig On d.IGroupID = ig.IGroupID
Left Join ITypes it On d.ITypeID = it.ITypeID
Left Join Users u On u.UserID = d.UserID
Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID
Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID
Inner Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
Where
d.DocumentID IN
(SELECT DocumentID
FROM (SELECT ROW_NUMBER() OVER (Order By DocumentID desc) peta_rn, peta_query.*
From (Select d.DocumentID
From Documents d) as peta_query) peta_paged
WHERE peta_rn > 92000 AND peta_rn <= 92100)
The subquery:
SELECT DocumentID
FROM (SELECT
ROW_NUMBER() OVER (Order By DocumentID desc) peta_rn, peta_query.*
FROM
(SELECT d.DocumentID
FROM Documents d) as peta_query) peta_paged
WHERE peta_rn > 92000 AND peta_rn <= 92100
itself takes 0 seconds but the whole query takes minutes and I don’t even have that many Joins. It’s hardly 6-7 joins which shouldn’t create so much problems in my opinion as these all foreign keys have indexes on them. Can anybody suggest what could be the problem?
Edit:
This is awesome. See this query which executes in 0 seconds:
Select d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName,
it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation
From Documents d
Inner Join IGroupes ig On d.IGroupID = ig.IGroupID
Left Join ITypes it On d.ITypeID = it.ITypeID
Left Join Users u On u.UserID = d.UserID
Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID
Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID
Inner Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
Where d.DocumentID IN
(
SELECT DocumentID FROM (SELECT
ROW_NUMBER() OVER (Order By DocumentID desc) peta_rn, peta_query.* From
(
Select d.DocumentID
From Documents d
) as peta_query) peta_paged WHERE peta_rn>92000 AND peta_rn<=92100
)
As you can see it just removes ig.Abbreviation IGroupAbbreviation, from the query. As soon as I put ig.Abbreviation IGroupAbbreviation, it starts taking minutes. Isn’t this the heights of ridiculousness? What do you think would cause this?
Edit 2:
Some more weirdness. Changing Inner Join IGroupes ig to Left Join IGroupes ig executes the query in 0 seconds. Can you please suggest why would Inner Join take up minutes? Is this my problem is this some bug in SQL server? Only thing left now is to bang my head against wall. I have wasted more than 4 hours.
Try this version of your query: