我们的仓库管理软件包有一个存储过程的瓶颈(很多)中的一个,主要的下降是由于查询产生了此执行计划。
https://www.brentozar.com/pastetheplan/?id=HkNg65elP
存储的proc可能需要3到10秒钟才能运行,这在运行业务流程的环境中非常慢。
一些其他信息:是的,有一个表正在执行全表扫描,但是该表很窄,只有76行。该查询会进行一些左联接和某种排序,以产生正确的顶部结果。总体而言,它有点像“ Rube Goldberg”类型的查询,可能可以简化,但是我的目标是看是否可以帮助进行某些索引编制(我已经做了,并且有所帮助),甚至有些如果需要,可以对查询进行一些小的调整。
最后,我需要根据计划知道下一个重点。
这是查询:
SELECT TOP 1 loc.location_id, loc.wh_id
FROM t_item_uom itu WITH (NOLOCK)
INNER JOIN t_class_loca clc WITH (NOLOCK)
ON itu.wh_id = clc.wh_id
AND ISNULL(dbo.usf_get_item_class_dia_ovrd ('13098271', '895', itu.uom, NULL), itu.class_id) = clc.class_id
INNER JOIN t_location loc WITH (NOLOCK)
ON clc.wh_id = loc.wh_id
AND clc.location_id = loc.location_id
INNER JOIN t_pick_area pka WITH (NOLOCK)
ON pka.pick_area = loc.pick_area
AND pka.wh_id = loc.wh_id
AND (pka.pick_area <> N'LABEL' OR (pka.pick_area = N'LABEL' AND 0 IS NULL AND 0 IS NULL) )
AND (pka.pick_area_type = 'R' OR (pka.pick_area_type = 'V' and 0 IS NULL) )
INNER JOIN t_zone_loca zlc WITH (NOLOCK)
ON loc.wh_id = zlc.wh_id
AND loc.location_id = zlc.location_id
INNER JOIN (
SELECT loc.wh_id, loc.pnd_location_id --, loc.location_id
FROM t_location loc with (nolock)
inner join t_class_loca clc WITH (NOLOCK)
on clc.location_id = loc.location_id
and clc.wh_id = loc.wh_id
and clc.class_id = 'APPAREL'
LEFT JOIN t_stored_item sto with (nolock)
ON sto.put_away_location = loc.location_id
AND sto.wh_id = loc.wh_id --BTH 20160907 missing wh_id
AND sto.put_away_location IS NOT NULL
AND sto.type = 0
WHERE loc.type in ('I','M')
AND loc.pnd_location_id IS NOT NULL --BTH 20160907 - remove from having clause, add here
GROUP BY loc.wh_id, loc.pnd_location_id, loc.c3
HAVING ((COUNT(sto.hu_id) < 100
and loc.pnd_location_id IS NOT NULL --BTH 201600907
and c3 is null)
OR (COUNT(sto.hu_id) < 500 --and loc.pnd_location_id IS NOT NULL --BTH 201600907
and c3 = 'BULK'))
) as pnd
ON pnd.wh_id = loc.wh_id
AND pnd.pnd_location_id = loc.pnd_location_id
LEFT OUTER JOIN t_put_rules_empty_and_unalloc_locs_by_pnd tpr WITH (NOLOCK)
ON tpr.pnd_location_id = loc.pnd_location_id
AND tpr.class_id = itu.class_id
AND tpr.wh_id = loc.wh_id
LEFT OUTER JOIN t_work_q q WITH(NOLOCK)
ON q.location_id = loc.location_id
AND q.wh_id = loc.wh_id
AND q.work_type = '08'
AND q.work_status = 'U'
WHERE loc.status = 'E'
AND ISNULL(q.work_q_id, 0) = 0
AND ( loc.c3 is null or loc.c3 not in ('R','H','S'))
AND (
(
loc.type = 'M'
AND ( (SELECT TOP 1 max_sku_count
FROM t_zone zone2 (NOLOCK)
WHERE zone2.wh_id = loc.wh_id
AND loc.zone = zone2.zone) >
(SELECT COUNT(sto2.item_number)
FROM t_stored_item sto2 (NOLOCK)
WHERE loc.wh_id = sto2.wh_id
AND loc.location_id = sto2.location_id
)
OR '13098271' IN (SELECT sto2.item_number
FROM t_stored_item sto2 (NOLOCK)
WHERE loc.wh_id = sto2.wh_id
AND loc.location_id = sto2.location_id
)
)
)
OR (loc.type = 'I' AND itu.unit_volume = 0 AND itu.nested_volume = 0)
OR (loc.type = 'I' AND loc.capacity_volume = 0)
OR (loc.type = 'I' AND loc.capacity_volume >= 0 +
(CASE
WHEN 0 = 0 THEN 0
ELSE 0
END * (1 - 1)
)
)
)
--Ensure that only one item is designated to the location
AND (loc.type = 'M'
OR (loc.type = 'I' AND NOT EXISTS (SELECT 1
FROM t_stored_item sto2 WITH (NOLOCK) -- Sum the item in the location to determine volume
WHERE sto2.wh_id = loc.wh_id
AND sto2.put_away_location = loc.location_id)))
AND itu.wh_id = '895'
AND itu.item_number = '13098271'
AND clc.class_id = 'APPAREL'
AND zlc.zone = 'ALL'
ORDER BY
tpr.percent_empty_and_unalloc DESC,
loc.type,
loc.user_count,
loc.picking_flow,
loc.location_id
答案 0 :(得分:1)
基于对执行计划的快速浏览。根据成本,问题1和问题3将为您带来最佳的投资回报率。
第一期:Key Lookup
在索引IDX_wh_id_status_pnd_location_id
上,索引的INCLUDE部分缺少列zone
。
第二个问题:Implicit Conversions
简而言之:您正在比较不同类型的列。确保您比较相同类型的列。如果这些是外键列,则两个表中的类型应完全相同。如果它们是参数,请更改类型或强制转换/隐藏它们。
第三期:汇总
您在[t_stored_item].[sto].put_away_location
上有一个汇总(最大值,计数,平均值,...),其行估计为129108。
尝试为该部分查询创建一个indexed view,并在索引视图中进行汇总。请改用索引视图。 More info
估算值也与实际相差甚远,您可以尝试重建统计信息,但可能无济于事。为什么? Read this
第四个问题:用户定义的功能
您有一个usf_get_item_class_dia_ovrd
的INNER JOIN,可以内联编写用户定义函数的逻辑吗?当我们内联执行代码时,代码通常会被优化,现在标量函数正在逐行执行,而不是基于set。
第五个问题:持续扫描-实际行数为0
这可能不是一个大问题,但是当表达式相互抵消时,通常会发生这种情况。虚拟示例:1 = 0
将始终计算为0行,因此SQL Server将其替换为空的常量扫描。
在复杂的查询中,您可能不会立即发现这一点。这对性能没有太大影响,但是当您从查询中删除这些计划时,可能会得到更好的执行计划。
如果有兴趣,请观看this video,以更好地了解查询优化器。 (有点旧,但仍然很重要)
奖金:参数嗅探
您提到这是一个存储过程。通常,存储过程与parameters sniffing有关。
答案 1 :(得分:0)
在不知道表的结构和数据的情况下,我的建议是查看执行成本较高的部分。在您的示例中,该值应位于右上角:14%(内部联接),17%和22%,以及其他位置的19%和25%。
其他重要的事情:您的索引以及它们的使用方式是否应有的。我认为不是。
专注于25%(键查找(群集)):此link可以帮助您更好地理解问题(并为我省去了很长的解释)。同样,我不知道您的表的结构。但是我觉得这里的索引不够用。
我看到了这个:CONVERT_IMPLICIT(nvarchar(4000)
。这是什么 ?会降低性能吗?
如果表的结构不良并且数据模型错误,那么优化起来将更加困难。添加更多索引或重新定义查询并不总是解决方案。