MS Sql optimization step optimized and not in one case

  Today received a customer complaint system, said the card is dead, after a doubled effort, finally solved. Will now record what resolution steps, so that the next reference:

        Because the client system focused on Ali cloud above, using ms sql2008 database, there are N clients above, all of a sudden can not know which customers.

  The first step, first open the Task Manager and see cpu usage,

To see that this is a large ms sql server queries taking up all the CPU time, so stuck system.

The second step, open ms sql server activity monitor to see which statement is stuck.

The method of open Activity Monitor. in 

The Object Explorer, locate the server, right-click. See "Activity Monitor", or use the shortcut keys ctrl + alt + A ..

 

The third step, find the offending statement. Opening the process, the task status, running the screening process.

Check one by one run in the statement, most likely stuck analysis system of statements, to run it. Which can be found stuck statement.

My situation is the following sentence:

SELECT A.cCrm, A.cCode AS cOrderCode, A.dRequire, A.dSubmit, B.*,
        C.cCode AS cProductCode, B.cProductSpec BcProductSpec, A.dConfirm,
        A.dCheck1, C.cParamter, C.cSpec AS cProductSpec, C.cColor, A.cCreator
    FROM Orders A WITH ( NOLOCK )
    LEFT JOIN Orders_Product B WITH ( NOLOCK ) ON A.cID = B.cOrdersID
    LEFT JOIN Product C WITH ( NOLOCK ) ON B.cProductID = C.cID
    LEFT JOIN (
                            --生产的产品ID
                SELECT DISTINCT A1.cProductID
                    FROM dbo.Product_Item A1
                    LEFT JOIN dbo.Orders_ProductItem A2 ON A1.cProductID = A2.cProductID
                    WHERE A1.iProduct != 0
              ) D ON B.cProductID = D.cProductID
    WHERE 1 = 1
        AND B.cProductID = D.cProductID
        AND A.iCancel = '0' 
        AND (Status =  30 
              OR (Status =  20 
                   AND iNewCRM ! =  1 
                   AND  NOT  Exist ( SELECT  1 
                                        FROM Orders_ProductItem WITH (NOLOCK)
                                         wherein iCustom =  1 
                                            AND cOrdersID = A.cID) 
                 ) 
            ) 
        AND (A.iStatusPP =  0 
              OR A .iStatusPP =  1 
            ) 
        AND  NOT EXISTS ( SELECT 1
                            FROM MOrders_Product
                            LEFT JOIN dbo.MOrders ON MOrders.cID = MOrders_Product.cMOrdersID
                            WHERE cOrdersProductID = B.cSubID
                                AND dbo.MOrders.iStatus != 2 )
        AND B.iCancelM = 0
        AND B.cSubID NOT IN (
        SELECT B.cOrdersProductID
            FROM DOrders A
            LEFT JOIN DOrders_Sub B ON A.cID = B.cDOrdersID
            WHERE iStatus = 3 )
    ORDER BY A.dUDate DESC; 

After analysis navigate to: not in Caton guide to the system:

 

 B.cSubID NOT IN (
        SELECT B.cOrdersProductID
            FROM DOrders A
            LEFT JOIN DOrders_Sub B ON A.cID = B.cDOrdersID
            WHERE iStatus = 3

 

 Will not in changed not exists the problem is resolved, the system normal operation.

After change code:

SELECT A.cCrm, A.cCode AS cOrderCode, A.dRequire, A.dSubmit, B.*,
        C.cCode AS cProductCode, B.cProductSpec BcProductSpec, A.dConfirm,
        A.dCheck1, C.cParamter, C.cSpec AS cProductSpec, C.cColor, A.cCreator
    FROM Orders A WITH ( NOLOCK )
    LEFT JOIN Orders_Product B WITH ( NOLOCK ) ON A.cID = B.cOrdersID
    LEFT JOIN Product C WITH ( NOLOCK ) ON B.cProductID = C.cID
    LEFT JOIN (
                            --生产的产品ID
                SELECT DISTINCT A1.cProductID
                    FROM dbo.Product_Item A1
                    LEFT JOIN dbo.Orders_ProductItem A2 ON A1.cProductID = A2.cProductID
                    WHERE A1.iProduct != 0
              ) D ON B.cProductID = D.cProductID
    WHERE 1 = 1
        AND B.cProductID = D.cProductID
        AND A.iCancel = '0' 
        AND (Status =  30 
              OR (Status =  20 
                   AND iNewCRM ! =  1 
                   AND  NOT  Exist ( SELECT  1 
                                        FROM Orders_ProductItem WITH (NOLOCK)
                                         wherein iCustom =  1 
                                            AND cOrdersID = A.cID) 
                 ) 
            ) 
        AND (A.iStatusPP =  0 
              OR A .iStatusPP =  1 
            ) 
        AND  NOT EXISTS ( SELECT 1
                            FROM MOrders_Product
                            LEFT JOIN dbo.MOrders ON MOrders.cID = MOrders_Product.cMOrdersID
                            WHERE cOrdersProductID = B.cSubID
                                AND dbo.MOrders.iStatus != 2 )
        AND B.iCancelM = 0
        AND NOT EXISTS (
        SELECT 1
            FROM DOrders AA
            LEFT JOIN DOrders_Sub BB ONAA.cID = BB.cDOrdersID
             wherein Status =  3  AND B.cSubID = bb.cOrdersProductID)
     ORDER  BY A.dUDate desc ;

 

The reason you can refer to the following article:

 https://www.cnblogs.com/totian/p/7597300.html

 

 

https://blog.csdn.net/zxu_1995/article/details/82388395

 

Guess you like

Origin www.cnblogs.com/KevinMO/p/11407588.html