Solved

Comparing SQL queries to determine work required

Posted on 2016-09-05
4
23 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 45

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 32

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 45

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now