【发布时间】:2018-09-19 09:13:45
【问题描述】:
这是我的查询:
SELECT top 1 w.WONumber, * FROM TSP_TSR_Job t left join
wsm_WorkOrderSchedule w on w.tsrjobid = t.JobId WHERE t.JobID=325809
我尝试过的:
DECLARE @i INT
SET @i=1
SELECT TOP (@i) ObjectType='Job',w.WONumber,*
FROM TSP_TSR_Job t
left HASH join wsm_WorkOrderSchedule w
on w.tsrjobid = t.JobId
WHERE t.JobID=325809
执行计划:
问题是这两个查询都需要 0:0:1 秒。
wsm_WorkOrderSchedule 中的 2493073 行和 TSP_TSR_Job 中的 524444
brentozar.com/pastetheplan/?id=ByXUickKQ 执行计划
> SQL Server parse and compile time: CPU time = 0 ms, elapsed time =
> 0 ms.
>
> SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.
> Warning: The join order has been enforced because a local join hint is
> used. SQL Server parse and compile time: CPU time = 5 ms, elapsed
> time = 5 ms.
>
> (1 row(s) affected) Table 'Workfile'. Scan count 0, logical reads 0,
> physical reads 0, read-ahead reads 0, lob logical reads 0, lob
> physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan
> count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob
> logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table
> 'wsm_WorkOrderSchedule'. Scan count 1, logical reads 6, physical reads
> 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob
> read-ahead reads 0. Table 'TSP_TSR_Job'. Scan count 0, logical reads
> 6, physical reads 0, read-ahead reads 0, lob logical reads 0, lob
> physical reads 0, lob read-ahead reads 0.
>
> (1 row(s) affected)
>
> SQL Server Execution Times: CPU time = 16 ms, elapsed time = 11
> ms. SQL Server parse and compile time: CPU time = 0 ms, elapsed
> time = 1 ms.
>
> SQL Server Execution Times: CPU time = 1 ms, elapsed time = 1 ms.
【问题讨论】:
-
绿色有索引推荐。
-
wsm_WorkOrderSchedule未命中连接列上的索引。 SSMS 在第二张截图上已经告诉你了。 -
索引是什么样的?如果缺少正确的索引,尝试更改连接的工作方式将无济于事。
wsm_WorkOrderSchedule.tsrjobid和TSP_TSR_Job .JobId字段应出现在两个表的索引中。 -
您在两个计划中的 CIX 扫描上都有一个感叹号。详情是什么?
-
请注意,您的 TOP 1 缺少
ORDER BY子句,因此每次都可能导致随机输出。
标签: sql-server query-performance