简体   繁体   中英

I am trying to tune a SQL query to get better performance

It looks like the Nonclustered Index Seek is taking pretty much all the cost? Although I dont know much about this dark art of query tuning, so not sure.

My original query is:

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;

And the query plan currently is here:

https://www.brentozar.com/pastetheplan/?id=Hys80JZAS

Is there anything I can do, or is this the best it can be?

Edit: table structure is:

在此处输入图片说明

在此处输入图片说明

Edit: using RECOMPILE I get a similar execution plan, except NESTED LOOP is now gone (is that good?):

https://www.brentozar.com/pastetheplan/?id=SJq4fG-AS

I guess, your nonclustered index seek won't go anywhere because of predicates you're using. But you can simplify and optimize your query by using window functions, thus eliminating one redundant table query. I presume you might use something like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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