SQL Statement taking long to execute

WingHong Shiu 5 Reputation points
2024-07-15T03:15:50.06+00:00

I have two SQL Databases that is on the same SQL server but on one database it only takes 4 seconds to complete the SQL statement but on the other SQL database it takes 25 minutes to complete. Both databases uses the same SQL elastic pool and I don't see the CPU usage, memory or I/O being a bottleneck. How would I diagnose the reason for such an issue since both databases setup is the same.  

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,323 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
66 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Olaf Helper 43,166 Reputation points
    2024-07-15T06:04:47.76+00:00

    First compare the execution plan of both queries to see if, there are differences, may one don't use the right index etc.


  3. MikeyQiaoMSFT-0444 1,755 Reputation points
    2024-07-16T07:20:20.9766667+00:00

    Hi,WingHong Shiu

    Check the execution plan and analyze the points that take the longest time.

    1. Is the compilation time too long?
    2. The statistics are outdated, and SQL Server has chosen an inefficient execution plan.
    3. The table does not have good indexes, making it difficult for the query to search effectively and quickly.
    4. Resource contention, with blocking or locking in the table.
    5. The statement is too complex.
    dbcc freeproccache
    go
    dbcc dropcleanbuffers
    go
    Set statistics io on -- Check the logical reads and physical reads of the execution 
    go 
    Set statistics time on -- Check the compilation time and execution time to determine if the long compilation time is causing low query efficiency 
    go 
    set statistics profile on -- Return the execution plan of the statement
    go
    %Your excute statement%
    go
    Set statistics time off
    go
    Set statistics io off
    go
    set statistics profile off
    
    
    

    Post your output results, ad we will help you analyze the issue.

    Best regards,

    Mikey Qiao


  4. Erland Sommarskog 106.5K Reputation points
    2024-07-21T20:58:04.8166667+00:00

    Sorry for the delay, but here are some observations. Let me first say that even with actual execution plans it is not always possible to say things with certainty. Having the table and index definitions could help more, but it is still a handicap not having access to the actual data. And for that matter not knowing the business rules.

    Anyway, the original question was why there is a performance difference between Prod and Test. The answer is the amount of data is not the same. It is approximately the same, but the difference is big enough to result in a plan difference. More precisely, the difference is in this part of the query:

    from TPS_POLine_Source tl
                  inner join dbo.THK_PurchPurchaseOrderLineStaging pl
                         on pl.PURCHASEORDERNUMBER = tl.PurchaseOrderNumber
                         and pl.LINENUMBER = tl.LineNumber
    

    In Prod this join is carried out as a Nested Loop join, whereas in test this is done with a Hash Join. An important difference is also that the Key Lookup required for THK_PurchPurchaseOrderLineStaging is before the join in Test, but after in Prod.

    So what can be done about this query? I would recommend removing the index hints in the query. I don't think they serve any purpose. It can very well be that for both THK_PurchPurchaseOrderLineStaging and LSG_Order_Tracking a Clustered Index Scan is a better choice. Whether removing the hints is sufficient remains to see.

    As for the inflated estimates we discussed earlier, they seem to come from this join:

    left join dbo.LSG_Order_Tracking ot
                         on ot.POInventTransID = tl.POInventTransId
    

    The big estimates suggests that POInventTransID is not a key in either table. But the actual values suggests that it may in practice be. Do you have any more information about this column?

    0 comments No comments