我正在努力使这个视图足够快,以便在合理的时间内获取结果集,这个时间超过30分钟,并行并导致各种痛苦,增加了cpu时间。我已经确定了问题查询,但我无法通过重写查询或在需要时添加适当的索引来找出减少执行时间的方法。我们已经在两个表中的hash_key列上的client_id和非聚集索引上有聚簇索引。这些相应的连接表也接近来自work_orders的大约2.38亿条记录,以及来自s_inspections表的总共287011570条记录。
select
wo.client_id,
wo.work_orders_hash_key,
wo.work_order_number,
wo.work_order_id,
si.inspection_id,
si.inspection_name,
si.inspection_detail,
si.master_inspection_id,
si.master_inspection_detail,
si.status_id,
si.exception,
si.inspection_order,
si.comment,
si.[procedure_id],
si.[flag_id],
si.[asset_id],
si.[asset_name],
si.[inspection_status],
si.[is_removed],
si.[response],
row_number() over(partition by si.work_orders_hash_key, si.inspection_id order by si.dss_version desc) rnk
from
datavault.dbo.h_work_orders wo with (readuncommitted)
join datavault.dbo.s_inspections si with (readuncommitted) on wo.client_id = si.client_id and wo.work_orders_hash_key = si.work_orders_hash_key
where
wo.client_id in (7700876368663, 8800387996408)
以下是估计的执行计划,因为它花了很长时间,所以我无法提供实际的执行计划。
https://www.brentozar.com/pastetheplan/?id=ryLzvNwUN
任何帮助将不胜感激。
您的计算标量是查询成本的59%。我猜它是这一行:row_number()over(分区由si.work_orders_hash_key,si.inspection_id由si.dss_version desc命令)rnk它估计159014000000000行!重击这一行(返回行号的很多工作)并再次运行它。
也许这会使你保持业务,因为row_number()是问题。尝试:
;with x as (
select
wo.client_id,
wo.work_orders_hash_key,
wo.work_order_number,
wo.work_order_id,
si.inspection_id,
si.inspection_name,
si.inspection_detail,
si.master_inspection_id,
si.master_inspection_detail,
si.status_id,
si.exception,
si.inspection_order,
si.comment,
si.[procedure_id],
si.[flag_id],
si.[asset_id],
si.[asset_name],
si.[inspection_status],
si.[is_removed],
si.[response],
si.dss_version
from
datavault.dbo.h_work_orders wo with (readuncommitted)
join datavault.dbo.s_inspections si with (readuncommitted) on wo.client_id = si.client_id and wo.work_orders_hash_key = si.work_orders_hash_key
where
wo.client_id in (7700876368663, 8800387996408)
)
select
x.client_id,
x.work_orders_hash_key,
x.work_order_number,
x.work_order_id,
x.inspection_id,
x.inspection_name,
x.inspection_detail,
x.master_inspection_id,
x.master_inspection_detail,
x.status_id,
x.exception,
x.inspection_order,
x.comment,
x.[procedure_id],
x.[flag_id],
x.[asset_id],
x.[asset_name],
x.[inspection_status],
x.[is_removed],
x.[response],
row_number() over(partition by x.work_orders_hash_key, x.inspection_id order by x.dss_version desc) rnk
from x;