Transferred from: http://blog.sqlauthority.com/2010/05/14/sql-server-find-most-expensive-queries-using-dmv/
The title of this post was what I can express here for this Quick blog post. I was asked in recent Query tuning consultation project, if I can share my script which I use to figure out which is the M OST expensive queries is running on SQL Server. This script was very basic and very simple, there was many different versions is available online. This basic script does does the job which I expect to do–find out of the most expensive queries on SQL Server Box.
SELECT TOP ten SUBSTRING (qt. TEXT, (QS.STATEMENT_START_OFFSET/2) +1,
(Case Qs.statement_end_offset
WHEN-1 then Datalength (qt. TEXT)
ELSE Qs.statement_end_offset
End-qs.statement_start_offset)/2) +1),
Qs.execution_count,
Qs.total_logical_reads, Qs.last_logical_reads,
Qs.total_logical_writes, Qs.last_logical_writes,
Qs.total_worker_time,
Qs.last_worker_time,
qs.total_elapsed_time/1000000 total_elapsed_time_in_s,
qs.last_elapsed_time/1000000 last_elapsed_time_in_s,
Qs.last_execution_time,
Qp.query_plan
From Sys.dm_exec_query_stats QS
Cross APPLY sys.dm_exec_sql_text (Qs.sql_handle) QT
Cross APPLY sys.dm_exec_query_plan (qs.plan_handle) QP
ORDER by Qs.total_logical_reads DESC--logical reads
--ORDER by Qs.total_logical_writes DESC--Logical writes
--ORDER by Qs.total_worker_time DESC--CPU time
You can change the ORDER BY clause to order this table with different parameters. I invite my reader to share their scripts.
Go SQL server–find Most expensive Queries Using DMV