Reads and Writes per DB in SQL Server

Hi Folks,

Today I am sharing another useful query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s and DMF’s provided by SQL server.Open new query window in SQL Server and fire the below query by passing the respective database in the where clause.For this example,I have used Adventure Works as my database.You can modify it as per your requirement.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT SUM(qs.total_logical_reads) AS [Total Reads]
, SUM(qs.total_logical_writes) AS [Total Writes]
, DB_NAME(qt.dbid) AS DatabaseName
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
WHERE DB_NAME(qt.dbid) = ‘AdventureWorks’
GROUP BY DB_NAME(qt.dbid)

image

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Advertisements

Queries last run on SQL Server

Hi Geeks,

Today I am sharing another useful query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s and DMF’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.For this example,I have restricted the output to only 20 rows using the TOP clause.You can modify it as per your requirement.

 

SELECT TOP 20 qt.text, qs.last_execution_time
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt

 

image

 

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Quickly find a cached plan for Stored Procedure in SQL Server

Hi Geeks,

Today,on the first day of this year 2014 I am sharing useful query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s and DMF’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.For this example,I have restricted the output to only 10 rows using the TOP clause and haven’t’ provided any specific name of any stored proc.You can modify it as per your requirement by providing any procedure name whose plan you want to analyze.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 10
st.text AS [SQL]
, cp.cacheobjtype
, cp.objtype
, COALESCE(DB_NAME(st.dbid),
DB_NAME(CAST(pa.value AS INT))+’*’,
‘Resource’) AS [DatabaseName]
, cp.usecounts AS [Plan usage]
, qp.query_plan
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
OUTER APPLY sys.dm_exec_plan_attributes(cp.plan_handle) pa
WHERE pa.attribute = ‘dbid’
AND st.text LIKE ‘%CREATE PROCEDURE%’

 

 

Result:

image

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Identify which SQL is running now in SQL Server

Hi Geeks,

Today I am sharing another useful query often required generally to view which queries are currently running on the server.The query makes use of system DMV’s and DMF’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.

SELECT
er.session_Id AS [Spid]
, sp.ecid
, DB_NAME(sp.dbid) AS [Database]
, sp.nt_username
, er.status
, er.wait_type
, SUBSTRING (qt.text, (er.statement_start_offset/2) + 1,
((CASE WHEN er.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE er.statement_end_offset
END – er.statement_start_offset)/2) + 1) AS [Individual Query]
, qt.text AS [Parent Query]
, sp.program_name
, sp.Hostname
, sp.nt_domain
, er.start_time
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt
WHERE session_Id > 50
AND session_Id NOT IN (@@SPID)
ORDER BY session_Id, ecid

 

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Find missing Indexes using DMV’s in SQL Server

Hi Geeks,

Today I am sharing SQL query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.For this example,I have restricted the output to only 10 rows using the TOP clause.You can modify it as per your requirement.

SELECT TOP 10
ROUND(s.avg_total_user_cost * s.avg_user_impact *
(s.user_seeks + s.user_scans),0) AS [Total Cost]
, s.avg_user_impact
, d.statement AS TableName
, d.equality_columns
, d.inequality_columns
, d.included_columns
FROM sys.dm_db_missing_index_groups g
INNER JOIN sys.dm_db_missing_index_group_stats s
ON s.group_handle = g.index_group_handle
INNER JOIN sys.dm_db_missing_index_details d
ON d.index_handle = g.index_handle
ORDER BY [Total Cost] DESC

 

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Find slowest running queries within a Stored Procedure in SQL Server

Hi Geeks,

Today I am sharing another useful query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.For this example,I have restricted the output to only 10 rows using the TOP clause.You can modify it as per your requirement.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 10
CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2))
AS [Total Elapsed Duration (s)]
, qs.execution_count
, SUBSTRING (qt.text,(qs.statement_start_offset/2) + 1,
((CASE WHEN qs.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE
qs.statement_end_offset
END – qs.statement_start_offset)/2) + 1) AS [Individual Query]
, qt.text AS [Parent Query]
, DB_NAME(qt.dbid) AS DatabaseName
, 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 total_elapsed_time DESC

Result:

image

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Generating dynamic Synonyms for your Database tables

Hi Folks,

Today I am posting a very useful query which is required when you want to make a dynamic script which will help you to create synonyms for all your tables within a database.Query is as follows,Run it on the required database.

select ‘create synonym syn_’ + t.name + ‘ for [‘ + DB_NAME() + ‘].[‘ + s.name + ‘].[‘ + t.name + ‘]’
    from sys.tables t
        inner join sys.schemas s
            on t.schema_id = s.schema_id
    where t.type = ‘U’

image

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.