?
Solved

Comparing SQL queries to determine work required

Posted on 2016-09-05
4
Medium Priority
?
32 Views
Last Modified: 2016-10-23
Hi,

I am trying to compare different queries queries in SQL server to see how much work it takes to process. So in other words the effort to get the results. So is there some sort of metric I can look at. I am guessing something I can compare in the execution plans.

Thanks,

Ward
0
Comment
Question by:whorsfall
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Vitor Montalvão earned 1000 total points
ID: 41784908
You can turn on SET STATISTICS before running each query and then compare the results.
0
 
LVL 36

Assisted Solution

by:ste5an
ste5an earned 1000 total points
ID: 41784921
1) Use the actual execution plan. Thus execute both queries in the same window. You can also add a SET STATISTICS IO ON to get thre scans and reads in the message pane, And an SET STATISTICS TIME ON for the exact computation time.
2) Use sys.dm_exec_query_stats. Here you need to tag your queries. E.g. start them with --COMPAREME. Then you can compare the results:

SELECT RANK() OVER ( ORDER BY total_logical_reads + total_logical_writes DESC, sql_handle, statement_start_offset ) AS row_no ,
        creation_time ,
        last_execution_time ,
        total_worker_time / 1000 AS total_worker_time ,
        total_logical_reads ,
        total_logical_writes ,
        execution_count ,
        total_logical_reads + total_logical_writes AS total_logical ,
        CASE WHEN sql_handle IS NULL THEN ' '
             ELSE ( SUBSTRING(st.text, ( qs.statement_start_offset + 2 ) / 2,
                              ( CASE WHEN qs.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX), st.text)) * 2
                                     ELSE qs.statement_end_offset
                                END - qs.statement_start_offset ) / 2) )
        END AS query_text ,
        st.text AS sql_text ,
        DB_NAME(st.dbid) AS database_name ,
        st.objectid AS object_id ,
        qp.query_plan ,
        cp.cacheobjtype ,
        cp.objtype ,
        qs.plan_handle
FROM    sys.dm_exec_query_stats qs
        INNER JOIN sys.dm_exec_cached_plans cp ON cp.plan_handle = qs.plan_handle
        CROSS APPLY sys.dm_exec_sql_text(sql_handle) st
        CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE   st.text LIKE '--COMPAREME%'
ORDER BY total_logical DESC;

Open in new window


This approach takes a look at the entire history of queries. So you may need to clear the plan cache before testing. This can have an effect in production, cause recompiles are enforced.
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 41805972
whorsfall, it's your question answered?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question