Why does the order make such a huge difference and how to optimize it?
ORDER BY needs to sort a result set, which can take a lot of time if it is large.
To optimize it, you may need to index tables correctly.
However, the access path to the index has its drawbacks, so it may even take longer.
If you have something other than equijoins in your query, or range predicates (like < , > or BETWEEN or GROUP BY ), then the index used for ORDER BY may use other indexes.
If you post a request, I can probably tell you how to optimize it.
Update:
Record the request:
SELECT * FROM View_Product_Joined j LEFT JOIN [dbo].[OPR_InventoryRules] irp ON irp.ID = j.skuid AND irp.InventoryRulesType = 'Product' LEFT JOIN [dbo].[OPR_InventoryRules] irs ON irs.ID = j.NodeSiteID AND irs.InventoryRulesType = 'Store' CROSS APPLY ( SELECT TOP 1 * FROM OPR_PriceLookup pl WHERE pl.siteID = j.NodeSiteID AND pl.skuid = j.skuid AND pl.RoleID IN (-1, 13) ORDER BY pl.RoleID desc ) pl WHERE SiteName = N'EcommerceSite' AND Published = 1 AND DocumentCulture = N'en-GB' AND NodeAliasPath LIKE N'/Products/Cats/Computers/Computer-servers/%' AND NodeSKUID IS NOT NULL AND SKUEnabled = 1 ORDER BY NodeOrder ASC
The View_Product_Joined relationship, as the name suggests, is probably a view.
Could you post your definition?
If it's indexable, you can benefit from creating an index on View_Product_Joined (SiteName, Published, DocumentCulture, SKUEnabled, NodeOrder) .
Quassnoi
source share