// SQL snippet

Find queries with high logical reads in SQL Server

Logical reads, pages touched in memory, are the most stable cost measure SQL Server keeps per query. This ranks the plan cache by them.

Main statement
SQL Server

The task

A logical read is one 8 KB page read from the buffer pool. If the page is not in memory, SQL Server first does a physical read from disk, but the logical read is counted either way, so the number does not depend on what happened to be cached. That makes it a stable measure of how much data a query touches. Statements at the top of sys.dm_exec_query_stats by total_logical_reads are usually scanning where they could seek, doing key lookups row by row, or running a nested loop over far more rows than estimated.

The snippet

Written for SQL Server.

SQL Server
SELECT TOP 20
    qs.execution_count,
    qs.total_logical_reads,
    CAST(qs.total_logical_reads * 8 / 1024.0 AS DECIMAL(18,1)) AS total_read_mb,
    CAST(qs.total_logical_reads / NULLIF(qs.execution_count, 0) * 8 / 1024.0 AS DECIMAL(18,2)) AS read_mb_per_call,
    CAST(qs.total_elapsed_time / 1000.0 / NULLIF(qs.execution_count, 0) AS DECIMAL(18,2)) AS mean_ms,
    LEFT(SUBSTRING(st.text, qs.statement_start_offset / 2 + 1,
         (CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)
               ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2 + 1), 140) AS statement_text
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY qs.total_logical_reads DESC;

작동 방식

sys.dm_exec_query_stats has one row per statement per cached plan, with totals since the plan was cached. sql_handle points at the whole batch or procedure text, so the SUBSTRING with statement_start_offset and statement_end_offset (byte offsets into Unicode text, hence the division by 2) cuts out the one statement the row describes. total_logical_reads times 8 KB is the data volume touched, counting a page again every time it is re-read. Dividing by execution_count separates one huge report from a hot loop doing a little too much per call. A point lookup that reads more than a few MB per call is almost always a scan that an index or a rewrite can remove. The query needs VIEW SERVER STATE, or VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later.

Variations

Rank by physical reads (actually hitting disk)

SELECT TOP 20
    qs.execution_count,
    qs.total_physical_reads,
    qs.total_logical_reads,
    LEFT(SUBSTRING(st.text, qs.statement_start_offset / 2 + 1,
         (CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)
               ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2 + 1), 140) AS statement_text
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY qs.total_physical_reads DESC;

Questions that come up

Why logical reads instead of duration?

Duration depends on cache warmth, blocking and concurrent load, so the same query ranks differently at 9am and 2am. Logical reads for the same plan and the same data are nearly identical every run, which makes them the number to compare before and after an index or rewrite. SET STATISTICS IO ON shows them per table for a single run.

Do plan cache stats survive a restart?

No. The whole cache is emptied on restart and individual plans evict under memory pressure. For long-term tracking, persist snapshots or use Query Store, which keeps per-query runtime stats in the database itself.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.