简体   繁体   English

where子句中的SQL性能问题

[英]SQL performance issue in where clause

I am running a query on SQLSERVER 2008. The query is taking 4 seconds to process. 我正在SQLSERVER 2008上运行查询。查询需要4秒钟来处理。 I cannot understand why it's taking so long. 我不明白为什么要花这么长时间。

SELECT tbl_Operations.Workcenter
,SUM(tbl_Used_Components.Used_Quantity) as CNF_TODAY
FROM tbl_Used_Components
JOIN tbl_Pack_Division on tbl_Pack_Division.Pack_Division_ID =        
tbl_Used_Components.Pack_Division_ID
JOIN tbl_Operations on tbl_Operations.Operation_ID = tbl_Pack_Division.Operation_ID

where CONVERT(date, tbl_Pack_Division.Stop_Time) = CONVERT(date, getdate())
AND tbl_Pack_Division.Memo = 'NORMAL'
and tbl_Pack_Division.Status = 'CNF_MACH'
GROUP BY tbl_Operations.Workcenter

The problem is in the where clause. 问题出在where子句中。 When i run the query without Where clause it runs in 0.1 second. 当我运行不带Where子句的查询时,它将在0.1秒内运行。 When I add the first 2 arguments it still runs fine. 当我添加前两个参数时,它仍然可以正常运行。 But when I add the third argument on Status-field it goes wrong. 但是,当我在Status-field上添加第三个参数时,它出错了。

How can this be a problem? 这怎么可能是个问题? It is a selection on the same granularity as the seond one (Memo-field). 它是与第二个(备注字段)具有相同粒度的选择。

EDIT: 编辑:

Status varchar(10) -- Can have 5 different values 状态varchar(10)-可以有5个不同的值

Memo varchar(150) 备注varchar(150)

Only index: Pack_Division_ID bigint 唯一索引:Pack_Division_ID bigint

XML Execution plan XML执行计划

Generally if you add one more column to the where it means that it has to be read fist from the table. 通常,如果在的位置再增加一列,则意味着必须首先从表中读取该列。 So without a where condition may your query could be faster even if it has more rows to aggregate because it can use indexes only, and has to read less. 因此,没有条件的情况下,即使要聚合的行更多,您的查询也可能会更快,因为它只能使用索引,并且读取次数更少。

If you check the EXECUTION PLAN it says you should use an index on that two column, which would give you a boost. 如果检查执行计划,它说您应该在该两列上使用一个索引,这将对您有所帮助。

The problematic step is here: 有问题的步骤在这里:

<RelOp AvgRowSize="48" EstimateCPU="0.136774" EstimateIO="1.94831" 
EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" 
LogicalOp="Clustered Index Scan" NodeId="6" Parallel="false" 
PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="2.08508" 
TableCardinality="124197">

 <ScalarOperator ScalarString="[MII03].[dbo].[tbl_Pack_Division].[Memo]='NORMAL'
 AND [MII03].[dbo].[tbl_Pack_Division].[Status]='CNF_MACH' 
AND CONVERT(date,[MII03].[dbo].[tbl_Pack_Division].
[Stop_Time],0)=CONVERT(date,getdate(),0)">

I think that the answer could be the low "selectivity" in your column Status, so since it tries to use all of the three values in the same time it means now more test, because the status, fitting more times then simply the first two values. 我认为答案可能是“状态”列中的低“选择性”,因此,由于它尝试同时使用所有三个值,因此这意味着现在需要更多测试,因为状态比前两个更适合拟合次数价值观。 However I'm not 100% sure how the optimizer working with the AND logical operators (what does mean the order) 但是我不确定100%优化程序如何与AND逻辑运算符配合使用(这意味着顺序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM