Find unused indexes in SQL Server 2008 R2

Source: Internet
Author: User
Tags join table name

SELECT Top 1000

O.name as table name

, I.name as index name

, i.index_id as index ID

, dm_ius.user_seeks as Search times

, Dm_ius.user_scans as scan times

, dm_ius.user_lookups as lookup times

, dm_ius.user_updates as update times

, the number of rows p.tablerows as table

, ' DROP INDEX ' + QuoteName (i.name)

+ ' on ' + QuoteName (s.name) + '. ' + QuoteName (object_name Dm_ius. object_id)) as ' DELETE statement '

From Sys.dm_db_index_usage_stats Dm_iusatch

INNER JOIN sys.indexes i on i.index_id = dm_ius.index_id and Dm_ius. object_id = i.object_id

INNER JOIN sys.objects o on Dm_ius. object_id = o.object_id

INNER JOIN sys.schemas s on o.schema_id = s.schema_id

INNER JOIN (SELECT SUM (p.rows) tablerows, p.index_id, p.object_id

From Sys.partitions p GROUP by p.index_id, p.object_id) p

On p.index_id = dm_ius.index_id and Dm_ius. object_id = p.object_id

WHERE objectproperty (Dm_ius. object_id, ' isusertable ') = 1

and dm_ius.database_id = db_id ()

and I.type_desc = ' nonclustered '

and I.is_primary_key = 0

and I.is_unique_constraint = 0

and O.name= ' tblorders '--based on actual modification of the table name

Order BY (Dm_ius.user_seeks + Dm_ius.user_scans + dm_ius.user_lookups) ASC

--Result Processing:

User_updates is very large, and finding User_seeks and User_scans is little or 0, that means the index has been updated,

But never used, just create and modify, do not provide any help for the query, you can consider deleting the

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.