View fragmentation percentage of the indexes on a table 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.Just pass in the name of the table and the database for which you want to view the fragmentation.

SELECT i.name AS IndexName
, ROUND(s.avg_fragmentation_in_percent,2) AS [Fragmentation %]
FROM sys.dm_db_index_physical_stats(DB_ID(‘parisdev’),
OBJECT_ID(‘currency’), NULL, NULL, NULL) s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id

 

image

 

I would like to have feedback from my blog readers.

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

Query to see the fill factor of the indexes on the tables in the current database 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..

SELECT DB_NAME() AS DatabaseName
, SCHEMA_NAME(o.Schema_ID) AS SchemaName
, OBJECT_NAME(s.[object_id]) AS TableName
, i.name AS IndexName
, i.fill_factor
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = O.object_id
WHERE s.database_id = DB_ID()
AND i.name IS NOT NULL
AND OBJECTPROPERTY(s.[object_id], ‘IsMsShipped’) = 0
ORDER BY fill_factor DESC

 

I would like to have feedback from my blog readers.

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

Get Index usage information 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 provided by SQL server.Open new query window in SQL Server and fire the below query  which will return information regarding the most used indexes on your server by SQL server.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
DB_NAME() AS DatabaseName
, SCHEMA_NAME(o.Schema_ID) AS SchemaName
, OBJECT_NAME(s.[object_id]) AS TableName
, i.name AS IndexName
, (s.user_seeks + s.user_scans + s.user_lookups) AS [Usage]
, s.user_updates
, i.fill_factor
INTO #TempUsage
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = O.object_id
WHERE 1=2
EXEC sp_MSForEachDB ‘USE [?];
INSERT INTO #TempUsage
SELECT TOP 10
DB_NAME() AS DatabaseName
, SCHEMA_NAME(o.Schema_ID) AS SchemaName
, OBJECT_NAME(s.[object_id]) AS TableName
, i.name AS IndexName
, (s.user_seeks + s.user_scans + s.user_lookups) AS [Usage]
, s.user_updates
, i.fill_factor
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = O.object_id
WHERE s.database_id = DB_ID()
AND i.name IS NOT NULL
AND OBJECTPROPERTY(s.[object_id], ”IsMsShipped”) = 0
ORDER BY [Usage] DESC’
SELECT TOP 10 * FROM #TempUsage ORDER BY [Usage] DESC
DROP TABLE #TempUsage

 

I would like to have feedback from my blog readers.

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

Follow my blog for regular updates related to DBA side on SQL Server.

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.

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.