SQL execution tuning is an endless fight. I'm not a DBA, but rather I am a designer who has put on a show to be one for a long time. I have worked with SQL Server databases with terrabytes of RAM the distance down to Stackify's gigantic armada of little SQL Azure databases. I have seen a smidgen of everything throughout the years. 

In this article, I will give a few hints to how designers can discover moderate SQL questions and do execution tuning in SQL Server. 

5 Ways to Find Slow SQL Queries 

1. Find Slow Queries With SQL DMVs 

One of the immense highlights of SQL Server is the majority of the dynamic administration sees (DMVs) that are incorporated with it. There are many them and they can give an abundance of data about an extensive variety of points. 

There are a few DMVs that give information about inquiry details, execution designs, ongoing inquiries and substantially more. These can be utilized together to give some stunning experiences. 

For instance, this question underneath can be utilized to discover the inquiries that utilization the most peruses, composes, laborer time (CPU), and so on. 

SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1, 

((CASE qs.statement_end_offset 

At the point when - 1 THEN DATALENGTH(qt.TEXT) 

ELSE qs.statement_end_offset 

END - qs.statement_start_offset)/2)+1), 

qs.execution_count, 

qs.total_logical_reads, qs.last_logical_reads, 

qs.total_logical_writes, qs.last_logical_writes, 

qs.total_worker_time, 

qs.last_worker_time, 

qs.total_elapsed_time/1000000 total_elapsed_time_in_S, 

qs.last_elapsed_time/1000000 last_elapsed_time_in_S, 

qs.last_execution_time, 

qp.query_plan 

FROM sys.dm_exec_query_stats qs 

CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt 

CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp 

Request BY qs.total_logical_reads DESC - legitimate peruses 

- ORDER BY qs.total_logical_writes DESC - legitimate composes 

- ORDER BY qs.total_worker_time DESC - CPU time 

The consequence of the inquiry will look something like this beneath. The picture beneath is from a showcasing application I made. You can see that one specific inquiry (the main one) takes up every one of the assets. 

By taking a gander at this, I can duplicate that SQL inquiry and check whether there is some approach to enhance it, include a file, and so on. 

Find moderate SQL questions with DMVs 

Experts: Always accessible essential rollup measurements. 

Cons: Doesn't disclose to you what is calling the questions. Can't envision when the inquiries are being brought after some time. 

2. Inquiry Reporting by means of APM Solutions 

One of the immense highlights of numerous application execution administration (APM) instruments is their capacity to track SQL inquiries. For instance, Retrace tracks SQL inquiries over numerous database suppliers, including SQL Server. 

Follow can disclose to you how often a question has been executed, to what extent it goes up against normal, and what exchanges are calling it. This is extremely significant data for SQL execution tuning. 

APM arrangements gather this information by doing lightweight execution profiling against your application code at runtime. 

The following is a screen capture from Retrace's application dashboard that shows for a specific application, which SQL questions take the longest. 

SQL Performance Tuning With Retrace Top Queries 

Follow TOP SQL QUERIES 

Backtrack gathers execution insights about each and every SQL question being executed. You can look for particular inquiries to chase down potential issues. 

Backtrack View All SQL Queries 

Backtrack VIEW ALL SQL QUERIES 

By choosing an individual inquiry, you can perceive how regularly that question is brought after some time and to what extent it takes. You can likewise observe what pages utilize the SQL question and how their execution is affected by it. 

Remember SQL Performance Over Time 

Remember SQL PERFORMANCE OVER TIME 

Since Retrace is a lightweight code profiler and catches ASP.NET ask for follows, it can even demonstrate to you the full setting of what your code is doing. 

The following is a caught follow that demonstrates the majority of the SQL inquiries and different insights about what the code was doing. Follow can even show log messages inside this equivalent view. Likewise, see that it demonstrates the server address and database name the inquiry is being executed on. You can likewise perceive what number of records were returned. 

Building a Graph in SQL Land

Backtrack Web Transaction Trace 

Backtrack WEB TRANSACTION TRACE 

As should be obvious, Retrace gives far reaching SQL revealing capacities as a feature of its APM abilities. It likewise gives different observing and cautioning highlights around SQL inquiries. 

Professionals: Detailed revealing crosswise over applications, per application, and per question. Can demonstrate exchange follows point by point how inquiries are utilized. Begins at just $10 per month. Is continually running once introduced. 

Cons: Does not give the quantity of peruses or composes per question. 

3. SQL Server Profiler (DEPRECATED!) 

The SQL Server Profiler has been around for quite a while. It is exceptionally helpful in the event that you are endeavoring to find continuously what SQL questions are being executed against your database. 

NOTE: Microsoft has reported that SQL Server Profiler is being censured! 

SQL Profiler catches extremely itemized occasions about your cooperation with SQL Server. 

Login associations, separations, and disappointments 

SELECT, INSERT, UPDATE, and DELETE proclamations 

RPC clump status calls 

Begin and end of put away strategies 

Begin and end of proclamations inside a put away strategy 

Begin and end of a SQL clump 

Mistakes kept in touch with the SQL Server blunder log 

A bolt gained or discharged on a database protest 

An opened cursor 

Security authorization checks 

SQL Server Profiler 

SQL SERVER PROFILER 

Instructional exercise: SQL Server Profiler 

Masters: Very point by point information accessible. 

Cons: You need to physically turn it on. This powers you to reproduce a situation you are attempting to catch. It is in the long run leaving for Extended Events. 

New invitation to take action 

4. SQL Server Extended Events 

The SQL Profiler has been supplanted by SQL Server Extended Events. This is certain to outrage many individuals yet I can comprehend why Microsoft is doing it. 

Broadened Events works by means of Event Tracing (ETW). This has been the regular route for all Microsoft related advances to uncover analytic information. 

ETW gives considerably more adaptability. As an engineer, I could without much of a stretch take advantage of ETW occasions from SQL Server to gather information for custom employments. That is extremely cool and extremely ground-breaking. 

Effortlessly take advantage of ETW occasions from SQL Server to gather information for custom employments. 

MORE: Introducing SQL Server Extended Events 

Geniuses: Easier to empower and leave running. Less demanding to create custom arrangements with. 

Cons: Since it is genuinely new, the vast majority may not know about it. 

New invitation to take action 

5. SQL Azure Query Performance Insights 

I am will accept that SQL Azure's execution announcing is based over Extended Events. Inside the Azure Portal you can gain admittance to a wide cluster of execution detailing and streamlining tips that are exceptionally useful. 

Note: These detailing capacities are accessible for databases facilitated on SQL Azure. 

In the screen capture underneath you can perceive how SQL Azure makes it simple to utilize your inquiries that utilization the most CPU, Data IO, and Log IO. It is has some extraordinary fundamental announcing incorporated with it. 

In the screen capture you can perceive how SQL Azure makes it simple to utilize your questions that utilization the most CPU, Data IO, and Log IO. It is has some extraordinary fundamental detailing incorporated with it. 

SQL AZURE TOP QUERIES 

You can likewise choose an individual inquiry and get more points of interest to help with SQL execution tuning. 

You can likewise choose an individual question and get more points of interest to help with SQL execution tuning. 

SQL AZURE QUERY DETAILS 

Aces: Great essential revealing. 

Cons: Only chips away at Azure. No detailing over different databases. 

Outline 

Next time you have to do some execution tuning with SQL Server, you will have a couple of alternatives available to you to consider. Chances are, you will utilize more than one of these apparatuses relying upon what you are endeavoring to achieve.

MySQL Performance Optimization