Solved

Comparing SQL queries to determine work required

Posted on 2016-09-05
4
26 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
4 Comments
 
LVL 48

Accepted Solution

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

Assisted Solution

by:ste5an
ste5an earned 250 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 48

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

860 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