Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 January SQL Server: Script to find Unused Indexes of Database

SQL Server: Script to find Unused Indexes of Database

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

As I am preparing important scripts for SQL Server DBA, so here I am also sharing one more vital script for finding the unused indexes of SQL Server Database.

The management and maintenance of database index is a day to day exercise for a Database Administrator. The wrong or unused index can create performance issues for a frequently used table.

At every insert and update, the data of an index are also changing, and it requires some IO operations. Better to find the unused index and delete it.
I also added “DropStatement” column in script for dropping unused Indexes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
SELECT
OBJECT_NAME(i.OBJECT_ID) AS ObjectName
,i.name AS UnusedIndexName
,8 * SUM(au.used_pages) AS IndexSizeInKB
,CASE
WHEN i.type = 0 THEN 'Heap'
WHEN i.type= 1 THEN 'Clustered'
WHEN i.type=2 THEN 'Non-Clustered'
WHEN i.type=3 THEN 'XML'
WHEN i.type=4 THEN 'Spatial'
WHEN i.type=5 THEN 'Clustered columnstore index'
WHEN i.type=6 THEN 'Nonclustered columnstore index'
WHEN i.type=7 THEN 'Nonclustered hash index.'
END index_type
,'DROP INDEX ' + i.name + ' ON ' + OBJECT_NAME(i.OBJECT_ID) AS DropStatement
FROM sys.indexes AS i
LEFT JOIN sys.dm_db_index_usage_stats AS dius
ON dius.OBJECT_ID = i.OBJECT_ID
AND i.index_id = dius.index_id
AND dius.database_id = DB_ID()
INNER JOIN sys.partitions AS p
ON p.OBJECT_ID = i.OBJECT_ID
AND p.index_id = i.index_id
INNER JOIN sys.allocation_units AS au
ON au.container_id = p.partition_id
WHERE OBJECTPROPERTY(i.OBJECT_ID, 'IsIndexable') = 1
AND OBJECTPROPERTY(i.OBJECT_ID, 'IsIndexed') = 1
AND dius.index_id IS NULL
OR (dius.user_updates > 0
AND dius.user_seeks = 0
AND dius.user_scans = 0
AND dius.user_lookups = 0)
GROUP BY OBJECT_NAME(i.OBJECT_ID), i.name, i.type
ORDER BY OBJECT_NAME(i.OBJECT_ID)

Other SQL Server Index related DBA Scripts:

SQL Server: Script to find Redundant and Duplicate Indexes

SQL Server: Script to find Index Average Fragmentation in Percentage

SQL Server: Script to find Missing Indexes

SQL Server: Script to find Index Operational Stats

Jan 30, 2018Anvesh Patel
SQL Server Interview: Can we restore the only data file (.mdf file)?SQL Server: Script to find a list of CHECK Constraints of Database
Comments: 2
  1. Satheesh
    September 25, 2019 at 1:31 pm

    Hi Anvesh sir,
    your scripts are awesome ………….Keep Rocking !!!!!!
    and pls update more interview questions

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      May 27, 2020 at 12:43 pm

      Sure will do…

      ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

January 30, 2018 2 Comments SQL Server, SQL Server DBA ScriptAnvesh Patel, database, database research and development, dbrnd, duplicate index, index fragmentation, Index Performance, index usage, indexes, missing index, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL, unused indexes
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....