简体   繁体   中英

SQL Query very slow - Suddenly

I have a SQL stored procedure that was running perfect (.2 secs execution or less), suddenly today its taking more than 10 minutes.

I see that the issue comes because of a LEFT JOIN of a documents table (that stores the location of all the digital files associated to records in the DB).

This documents table has today 153,234 records.

The schema is

在此处输入图片说明

The table has 2 indexes:

  • Primary key (uid)
  • documenttype (nonclustered)

The stored procedure is:

SELECT 
    .....,
    CASE ISNULL(cd.countdocs,0) WHEN 0 THEN 0 ELSE 1 END as hasdocs
     .....
FROM 
    requests re
JOIN 
    employee e ON (e.employeeuid = re.employeeuid)
LEFT JOIN 
    (SELECT 
         COUNT(0) as countnotes, n.objectuid as objectuid 
     FROM 
         notes n
     WHERE 
         n.isactive = 1
     GROUP BY 
         n.objectuid) n ON n.objectuid = ma.authorizationuid

/* IF I COMMENT THIS LEFT JOIN THEN WORKS AMAZING FAST */
LEFT JOIN  
    (SELECT 
         COUNT(0) as countdocs, cd.objectuid 
     FROM 
         cloud_document cd
     WHERE  
         cd.isactivedocument = 1
         AND cd.entity = 'COMPANY'
     GROUP BY 
         cd.objectuid) cd ON cd.objectuid = re.authorizationuid
JOIN ....

So don't know if I have to add another INDEX to improve this query of maybe the LEFT JOIN I have is not ideal.

If I run the execution plan I get this:

/*
Missing Index Details from SQLQuery7.sql - (local).db_prod (Test/test (55))
The Query Processor estimates that implementing the following index could improve the query cost by 60.8843%.
*/

/*
USE [db_prod]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[cloud_document] ([objectuid],[entity],[isactivedocument])

GO
*/

Any clue on how to solve this?

Thanks.

Just don't go out and add a index. Do some research first!

Can you grab a picture of the query plan and post it? It will show if the query is using the index or not.

Also, complete details of the table would be awesome, including Primary Keys, Foreign Keys, and any indexes. Just script them out to TSQL. A couple of sample records to boot and we can recreate it in a test environment and help you.

Also, take a look at Glenn Barry's DMVs.

http://sqlserverperformance.wordpress.com/tag/dmv-queries/

Good stuff like top running queries, read/write usages of indexes, etc - to name a few.

Like many things in life, it all depends on your situation!

Just need more information before we can make a judgement call.

I would actually be surprised if an index on that field helps as it likely only has two or three values (0,1, null) and indexes are not generally useful when the data has so few values.

I would suspect that either your statistics are out of date or your current indexes need to be rebuilt.

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