Manage your SQL Server databases with SQL DMVs

Microsoft ships SQL Server with several built-in tools that database administrators (DBAs) can use to manage their SQL Server environment. With SQL Server 2005, Microsoft introduced SQL dynamic management views, or DMVs, as well as dynamic management functions, or DMFs. They provide plenty of information about server and database state, which you can use to monitor the health of a SQL Server instance, diagnose problems and tune SQL Server instance or database performance.

Checkout my article here, where you will find out how with detailed examples of some of the most common SQL DMVs (dynamic management views) and DMFs (dynamic managemnt functions) that can help you simplify administration of your SQL Server instances and databases.

This article is published on SearchSQLServer.techtarget.com.

Advertisement

Bug fixed: it now rebuilds indexes, with space in their name.

Basit's SQL Server Tips

In my previous post here, I’ve discussed how we can detect fragmentation in SQL Server databases indexes using dynamic management view function sys.dm_db_index_physical_stats. In this post, I’m sharing my stored procedure which I’ve created a while ago to rebuild fragmented indexes based on the fragmentation level. This stored procedure is based on sys.dm_db_index_physical_stats and resides in master database. This procedure automatically rebuilds indexes if the fragmentation level is above 30% and reorganises indexes if fragmentation is less than 30%.

Code for the procedure is as follow:

Mark this procedure as system object by executing the following command so that this procedure can be accessed from any database:

In the next version, I will add a logging feature so that you can see what indexes were defragmented, see how bad the fragmentation was, and perhaps do some trending on the data.

Let me know, if you find a bug or…

View original post 6 more words

Determine Index Fragmentation in a SQL Server Database

Index fragmentation can adversely affect query response time. When accessing data through an index, SQL Server must read each page in the specified range to retrieve the indexed values. If the index is highly fragmented, SQL Server may have to search many more pages, and possibly levels, to get this information. This results in poor performance and causes your application to respond slowly.

We can use system function sys.dm_db_index_physical_stats to detect fragmentation in specific index, all indexes in a table or indexed view, or all indexes in databases, or all indexes in all databases. The column avg_fragmentation_in_percent returns the percentage of fragmented data.

Although there is no hard and fast rule, a common recommendation is to keep the index fragmentation below 10 percent if possible. Following query can be used to identify indexes in the current database that have more than 5% fragmentation:

-- Ensure a USE statement has been executed first.

SELECT dm.[object_id]
	,DB_NAME(DB_ID()) + '.' + s.[name] + '.' + o.[name]
	,dm.[index_id]
	,i.[name]
	,dm.[partition_number]
	,dm.[index_type_desc]
	,[pad_index] = CASE i.[is_padded]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,i.[fill_factor]
	,[statistics_norecompute] = CASE st.[no_recompute]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[ignore_dup_key] = CASE i.[ignore_dup_key]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[allow_row_locks] = CASE i.[allow_row_locks]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[allow_page_locks] = CASE i.[allow_page_locks]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,dm.[avg_fragmentation_in_percent]
FROM sys.objects o
INNER JOIN sys.indexes i ON o.[object_id] = i.[object_id]
	AND i.[name] = 'NULL'
INNER JOIN sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') dm ON i.[object_id] = dm.[object_id]
	AND i.[index_id] = dm.[index_id]
	AND dm.[avg_fragmentation_in_percent] >= 5
INNER JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
INNER JOIN sys.stats st ON i.[name] = st.[name]
	AND o.[object_id] = st.[object_id]
	AND o.[type] = 'U'

Microsoft recommends reorganizing index if an index fragmentation is between 5-30% and rebuilding index if the index fragmentation is over 30%. For more details about reorganize and rebuild Indexes refer to Microsoft Books Online at http://msdn.microsoft.com/en-us/library/ms189858.aspx.