作者:浆果范_163 | 来源:互联网 | 2024-10-18 10:27
Ihaveaqueryjoining4tableswithalotofconditionsintheWHEREclause.Thequeryalsoinclude
I have a query joining 4 tables with a lot of conditions in the WHERE clause. The query also includes ORDER BY clause on a numeric column. It takes 6 seconds to return which is too long and I need to speed it up. Surprisingly I found that if I remove the ORDER BY clause it takes 2 seconds. Why the order by makes so massive difference and how to optimize it? I am using SQL server 2005. Many thanks.
我有一个查询连接4个表,WHERE子句中有很多条件。该查询还包括数字列上的ORDER BY子句。返回需要6秒,这太长了,我需要加快速度。令人惊讶的是,我发现如果我删除ORDER BY子句需要2秒钟。为什么订单会产生如此巨大的差异以及如何优化它?我正在使用SQL Server 2005.非常感谢。
I cannot confirm that the ORDER BY makes big difference since I am clearing the execution plan cache. However can you shed light at how to speed this up a little bit? The query is as follows (for simplicity there is "SELECT *" but I am only selecting the ones I need).
由于我正在清除执行计划缓存,因此我无法确认ORDER BY有很大的不同。但是,你能说明如何加快这一点吗?查询如下(为简单起见,有“SELECT *”,但我只选择我需要的那些)。
SELECT *
FROM View_Product_Joined j
INNER JOIN [dbo].[OPR_PriceLookup] pl on pl.siteID = NodeSiteID and pl.skuid = j.skuid
LEFT JOIN [dbo].[OPR_InventoryRules] irp on irp.ID = pl.SkuID and irp.InventoryRulesType = 'Product'
LEFT JOIN [dbo].[OPR_InventoryRules] irs on irs.ID = pl.siteID and irs.InventoryRulesType = 'Store'
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) AND pl.PriceLookupID in (select TOP 1 PriceLookupID from OPR_PriceLookup pl2 where pl.skuid = pl2.skuid and (pl2.RoleID = -1 or pl2.RoleId = 13) order by pl2.RoleID desc)))
ORDER BY NodeOrder ASC
1 个解决方案
8
Why the order by makes so massive difference and how to optimize it?
为什么订单会产生如此巨大的差异以及如何优化它?
The ORDER BY
needs to sort the resultset which may take long if it's big.
ORDER BY需要对结果集进行排序,如果结果集很大则可能需要很长时间。
To optimize it, you may need to index the tables properly.
要优化它,您可能需要正确索引表。
The index access path, however, has its drawbacks so it can even take longer.
但是,索引访问路径有其缺点,因此甚至可能需要更长时间。
If you have something other than equijoins in your query, or the ranged predicates (like <
, >
or BETWEEN
, or GROUP BY
clause), then the index used for ORDER BY
may prevent the other indexes from being used.
如果在查询中有除equijoins以外的东西,或者有远程谓词(如<,>或BETWEEN或GROUP BY子句),则用于ORDER BY的索引可能会阻止使用其他索引。
If you post the query, I'll probably be able to tell you how to optimize it.
如果您发布查询,我可能会告诉您如何优化它。
Update:
更新:
Rewrite the query:
重写查询:
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 relation View_Product_Joined
, as the name suggests, is probably a view.
顾名思义,关系View_Product_Joined可能是一个视图。
Could you please post its definition?
你可以发表它的定义吗?
If it is indexable, you may benefit from creating an index on View_Product_Joined (SiteName, Published, DocumentCulture, SKUEnabled, NodeOrder)
.
如果它是可索引的,您可以从View_Product_Joined(SiteName,Published,DocumentCulture,SKUEnabled,NodeOrder)上创建索引中受益。