Toad World Blog

How to Find How Many Rows Each Query Returned Along with Execution Plan? – Interview Question of the Week #114

Mar 18, 2017 8:30:47 PM by Pinal Dave

Question: How to Find How Many Rows Each Query Returned Along with Execution Plan?

Answer: During my performance tuning consultancy engagement, this is one of the most asked questions. Everyone wants to know how many row any query returned and what is its execution plan. I personally believe this information is not so useful as what we should be concern is about how many reads any queries are doing and what is the worker time. If you are interested to know that here is the query for the same: SQL SERVER – Find Most Expensive Queries Using DMV (I would bookmark this page as it can come handy quite frequently).

Now to answer the original question here is the query which returns the query execution count, number of rows it returned along with the execution plan. If you want to see the execution plan of the query, you just have to click on the link in the last column.

SELECT
DB_NAME (qt.dbid) database_name,
qs.execution_count,
qt.text as query_text,
qs.total_rows,
qs.last_rows,
qs.min_rows,
qs.max_rows,
qp.query_plan
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) AS qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
ORDER BY qs.execution_count DESC

Please note, this query only returns results from the cache. On the busy system the cache might be cleaned more frequently and you may not get accurate results. Additionally, just like any other DMV, this returns results from the time when your SQL Server services were restarted last.

Please bookmark this page if you find it useful.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Tags: SQL Server

Pinal Dave

Written by Pinal Dave

Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 1700 articles on the subject on his blog at http://blog.sqlauthority.com. He is a dynamic and proficient Principal Database Architect who specializes in SQL Server Performance Tuning and has 7+ years of hands-on experience. He holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is also Regional Mentor for PASS Asia. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.