Created
June 14, 2011 15:10
-
-
Save johnnonolan/1025090 to your computer and use it in GitHub Desktop.
A couple of useful scripts for non dbas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- this one may impact performance run at a quiet time | |
| -- change INSERT YOUR DB NAME HERE to your db name | |
| SELECT object_name(IPS.object_id) AS [TableName], | |
| SI.name AS [IndexName], | |
| IPS.Index_type_desc, | |
| IPS.avg_fragmentation_in_percent, | |
| IPS.avg_fragment_size_in_pages, | |
| IPS.avg_page_space_used_in_percent, | |
| IPS.record_count, | |
| IPS.ghost_record_count, | |
| IPS.fragment_count, | |
| IPS.avg_fragment_size_in_pages | |
| FROM sys.dm_db_index_physical_stats(db_id(N'INSERT YOUR DB NAME HERE'), NULL, NULL, NULL , 'DETAILED') IPS | |
| JOIN sys.tables ST WITH (nolock) ON IPS.object_id = ST.object_id | |
| JOIN sys.indexes SI WITH (nolock) ON IPS.object_id = SI.object_id AND IPS.index_id = SI.index_id | |
| WHERE ST.is_ms_shipped = 0 | |
| ORDER BY 1,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- none of this stuff is mine... just cobbled from the internets | |
| SELECT | |
| mid.statement | |
| ,migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure,OBJECT_NAME(mid.Object_id), | |
| 'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) | |
| + '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']' | |
| + ' ON ' + mid.statement | |
| + ' (' + ISNULL (mid.equality_columns,'') | |
| + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END | |
| + ISNULL (mid.inequality_columns, '') | |
| + ')' | |
| + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement, | |
| migs.*, mid.database_id, mid.[object_id] | |
| FROM sys.dm_db_missing_index_groups mig | |
| INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle | |
| INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle | |
| WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10 | |
| ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- if the index is being updated and not used get rid. | |
| SELECT o.name AS object_name, i.name AS index_name | |
| , i.type_desc, u.user_seeks, u.user_scans, u.user_lookups | |
| , u.user_updates, u.last_user_seek, u.last_user_scan | |
| , 'Drop index ' + i.name + ' on ' + o.name as DropIndexStatement | |
| FROM sys.indexes i | |
| JOIN sys.objects o ON i.object_id = o.object_id | |
| LEFT JOIN sys.dm_db_index_usage_stats u ON i.object_id = u.object_id | |
| AND i.index_id = u.index_id | |
| AND u.database_id = DB_ID() | |
| WHERE o.type <> 'S' | |
| and isnull(u.user_updates,0) > 0 | |
| and i.type_desc <> 'HEAP' | |
| ORDER BY (convert(decimal(19,4),ISNULL(u.user_seeks, 0)) | |
| + ISNULL(u.user_scans, 0) | |
| + ISNULL(u.user_lookups, 0))/ISNULL(u.user_updates, 0) asc | |
| , user_updates desc, o.name, i.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment