SQL Query Optimization and Execution Understanding

by Imranshakeel Khan

Recently, I was reviewing an education based application which I’d developed. When I clicked to the open course detail screen, it took almost 25 seconds to open, which was really too much time. At first glance, I thought that my machine might be slow.

I restarted my machine but the problem was still there! This could be bad, I thought, and impact me negatively when my client decides to  use this application. I investigated and found that my stored procedure was the culprit, taking too much time.

I checked the defects in the query, and after correcting the execution time of my stored procedures, I reduced the lag from 25 sec. to just 12 seconds! Through this process, I figure out that most of programmers don’t care about some minor (but very important) points, such as this one, and don’t even know that how queries execute the server! With that in mind, I think this blog is going to be very helpful for programmers.

Click here to read the original post!

Performance Optimization: Smart SQL Profiling

By Nadeem Akhter

Problem

Recently I had a chance to work on a system, which literally had millions of calls to the database. I wanted to know which queries were being called most frequently and which ones were taking most of the CPU cycles. I tried using SQL profiler but it made the situation worse due to its own overhead. I needed to figure out how to do this without further taxing the system.

Another issue I uncovered was every time I wanted to profile something I had to go through the same manual process over and over again i.e. pick certain events, choose few columns and apply a couple of filters, and that is a painful of doing monotonous tasks.

Solution

To solve this problem, I created a lightweight utility, which just subscribes to SQL profiler events and groups them based on the objects. Then it shows statistics such as the name of the object, how many times it was called, and the total duration of all the calls. Once I have this information, I can quickly sort it into different ways and I would know, if I am using dynamic queries, which statements/procedures are taking most of the CPU cycles. These would be the ones that need my attention!

Here is a screenshot:

I mentioned that there could be millions of calls, and you don’t want to capture each and every one of them. Otherwise there will be too much data and it will be difficult to analyze too. What I have done is just capture only top twenty most time consuming queries for each object. This way you can easily analyze them find and fix the issue one at a time and repeat the process.

Here is the updated screenshot of the utility. You can export/import the data and increase/decrease the number of rows you want to show.

What’s Next?

I hope you like the blog. If you need assistance on a similar issue, comment below and I’ll be happy to help. Next time we will see what are some common mistakes and some easy ways to fix those big problems, so stay tuned!

Read more about SQL here!