On the flip side, the database is responsible for examining large amounts of data to retrieve the status of that order. There may be several tables involved containing millions of rows of data each and inefficiencies can quickly become huge bottlenecks. Tuning the underlying query in this case typically provides the most performance benefit rather than focusing on the application code.
Which SQL statement should be tuned?
If SQL statement tuning can provide the most benefit, the next question is “Which SQL statement should I focus on”? Often I find that a DBA or developer did a great job of tuning a SQL statement, but later discovered it was not the root cause of the performance problem the end users were complaining about. Tuning the wrong SQL statement is clearly a waste of time, so what is the best way to know which SQL to tune? Many will choose a poorly performing SQL using the following metrics about those statements:
• Perform the most logical I/O
• Consume the most CPU
• Perform costly full table or index scans
• Use an execution plan with a high cost
• And many more…
What if the underlying problem was a blocking issue for a SQL statement? The problematic queries may not appear in any of these lists and you would miss them. How do you know which queries are causing your performance issues? In my opinion, the answer lies in measuring total elapsed times rather than using the above measurements, i.e. which SQL statements spend the most time executing in the database. A query similar to the following will retrieve a list of SQL statements from SQL Server taking the longest cumulative time to execute: