简体   繁体   中英

SQL Server : index not fragmented but query became slow

In the production environment where I work there's a very odd problem.

We got a simple query that is running very often, and without any problem. A DBCC DBREINDEX(..) of the table is planned every night at 01.30 am, and usually everything is fine.

But once every one or two weeks, the query became slow and all users can't work. This happen early in the morning (9.00 am) so right next the index rebuild.

When this happen I do a reindex of two indexes of the table and all came back to normality for another week.

If I check the index stats when that slowdown happens everything is fine, fragmentation is about at 0.2

If I check the estimated execution plan PRE and AFTER the issue I see no changes.

The query is very simple:

SELECT SUM(A.QTY) 
FROM WMSORDER A 
WHERE ((DATAAREAID = '01') AND (INVENTTRANSID = '046830648'))

and there is and index on the columns DATAAREAID and INVENTTRANSID

Here I post two images with the index stats where the issue occurs:

PRE REINDEX STATISTICS WITH SLOW PERFORMANCE

EXECUTION PLAN

How can I avoid this issue?

The index you are using is not the best one for that query. The execution plan includes a KeyLookup to get the qty. I suggest you to drop that index and create the following covering index:

CREATE INDEX IX_WMSORDER_INVENTTRANSID_DATAAREAID 
ON WMSORDER(NVENTTRANSID, DATAAREAID)
INCLUDE (QTY)

It was a problem of parameter sniffing.

I solved forcing literals in the DATAAREAID parameters instead of using the @parameter.

Thank you for the tip

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