SELECT TOP(50) qs.execution_count AS [Execution Count], (qs.total_logical_reads)/1000.0 AS [Total Logical Reads in ms], (qs.total_logical_reads/qs.execution_count)/1000.0 AS [Avg Logical Reads in ms], (qs.total_worker_time)/1000.0 AS [Total Worker Time in ms], (qs.total_worker_time/qs.execution_count)/1000.0 AS [Avg Worker Time in ms], (qs.total_elapsed_time)/1000.0 AS [Total Elapsed Time in ms], (qs.total_elapsed_time/qs.execution_count)/1000.0 AS [Avg Elapsed Time in ms], qs.creation_time AS [Creation Time] ,t.text AS [Complete Query Text], qp.query_plan AS [Query Plan] FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK) CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp WHERE t.dbid = DB_ID() ORDER BY qs.execution_count DESC OPTION (RECOMPILE);-- for frequently ran query -- ORDER BY [Avg Logical Reads in ms] DESC OPTION (RECOMPILE);-- for High Disk Reading query -- ORDER BY [Avg Worker Time in ms] DESC OPTION (RECOMPILE);-- for High CPU query -- ORDER BY [Avg Elapsed Time in ms] DESC OPTION (RECOMPILE);-- for Long Running query
# TODO: replace with your own tests (TDD), these are just how-to examples to get you started.
# Ruby/Rspec/Sequel Example:
# While the code section is pure SQL, for testing we use Ruby & Rspec.
# Sequel (https://github.com/jeremyevans/sequel) is used to setup the database and run queries.
# The connection is already made for you, use DB to access.
DB.create_table :items do
primary_key :id
String :name
Float :price
end
items = DB[:items] # Create a dataset
# Populate the table
items.insert(:name => 'a', :price => 10)
items.insert(:name => 'b', :price => 35)
items.insert(:name => 'c', :price => 20)
# calling run_sql will print the results and return them so that you can test data within them.
# if you want to test different sets of data, then its best to move this code into its own top level describe
# block. If you are only testing one set though, its better to set the results before you enter a describe block
# so that the results are presented at the top of the output.
results = run_sql
describe :items do
it "should return 3 items" do
expect(results.count).to eq 3
end
end
# Other tips about using run_sql:
# The SQL/code section supports multiple statements, seperated of course by a ";".
# When multiple SELECT statements are issued:
# run_sql will return an array of arrays, unless only one SELECT statement returned results
# INSERT and UPDATE results will not be included in the list
# SELECT statements that return no results will not be included in the list