concept most time in category sql

appears as: most time
SQL Server DMVs in Action: Better Queries with Dynamic Management Views

This is an excerpt from Manning's book SQL Server DMVs in Action: Better Queries with Dynamic Management Views.

The results are sorted by the Total time blocked and show the queries that have spent the most time being blocked. We also record the percentage of time the query spent on the CPU (% CPU column) and the percentage of time the query spent being blocked (% Waiting column).

All code statements, even those running within the CLR, can have performance problems. If you can identify which statements spend the most time in the CLR, you can target these for performance improvement. The next example script identifies those queries.

Listing 7.4. The queries that spend the most time in the CLR

The listing shows how to extract the time spent in the CLR and also shows the individual SQL query that contains the SQL CLR function name . The results are sorted by the total_clr_time because we’re interested in the queries that spend the most time in the CLR.

The listing uses one DMV and one DMF to identify the queries that spend the most time in the CLR. Table 7.2 provides a brief description of each.

Table 7.2. DMV/DMF to identify the queries that spend the most time in the CLR

DMV/DMF

Description

sys.dm_exec_query_stats Contains aggregated performance statistics for cached query plans
sys.dm_exec_sql_text DMF that returns the SQL text identified by a given sql_handle

By joining the DMV and DMF, we have enough information to identify the queries that spend the most time in the CLR, across all the databases on the server. The query’s sql_handle is passed to the DMF sys.dm_exec_sql_text to retrieve the text of the SQL query. Note that unlike other similar queries, here we don’t obtain the cached plan, because this query spends its time in the CLR. We use a common pattern, as discussed in chapter 2, to extract the Individual Query, which the timings relate to, from the Parent Query. An example of the type of output for this query is shown in figure 7.4.

Figure 7.4. Output showing the queries that spend the most time in the CLR

The query we execute selects the top 20 queries that spend the most time in the CLR, across all databases on the server. The total duration, time on the CPU, time waiting, number of reads, number of writes, name of the database, the Individual Query, and the Parent Query are also output. The output is sorted by total_clr_time in descending order. Note that the Individual Query given in the output contains the CLR code, which is typically hosted in a SQL query statement.