看起来非聚集索引搜寻正在承担几乎所有成本?尽管我对查询调优的这种黑技巧了解不多,但是不确定。
我的原始查询是:
SELECT mxmservcallaudit.jobid,
mxmservcallaudit.dataareaid,
mxmservcallaudit.date AS maxdate,
UPPER(mxmservcallaudit.USERID) AS maxuser
FROM mxmservcallaudit
INNER JOIN (SELECT jobid, dataareaid, MAX(RECID) AS maxrecid
FROM mxmservcallaudit
WHERE type = 9 AND dataareaid = 'ansa'
GROUP BY dataareaid, jobid) AS statusdate1
ON mxmservcallaudit.DATAAREAID = statusdate1.DATAAREAID
AND MXMSERVCALLAUDIT.RECID = statusdate1.maxrecid;
查询计划当前在这里:
https://www.brentozar.com/pastetheplan/?id=Hys80JZAS
有什么我可以做的,还是这是最好的?
编辑:表结构为:
编辑:使用RECOMPILE我得到了类似的执行计划,除了NESTED LOOP现在不见了(这样好吗?):
答案 0 :(得分:0)
我猜想,由于您使用的谓词,您的非聚集索引查找不会成功。 但是,您可以使用窗口函数来简化和优化查询,从而消除一个冗余表查询。 我想您可能会使用这样的东西:
with cte as (
select
jobid, dataareaid, date, USERID,
row_number() over (partition by jobid order by RECID desc) as RowNumber
from mxmservcallaudit
where type = 9 AND dataareaid = 'ansa'
)
select jobid,
dataareaid,
date as maxdate,
upper(USERID) as maxuser
from cte
where RowNumber = 1;
答案 1 :(得分:0)
看起来它不会更快,所以我将其保留了