Solved

Low use Indexes

Posted on 2011-02-25
3
237 Views
Last Modified: 2012-05-11
I need to compile statictics info on useage of all indexes to see which are not being used and then delete them from the system. How can I do that.

I need to monitor index usage for 2 weeks

Using SQL Server 2008 R2
0
Comment
Question by:tantradude
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 34984030
select * from sys.dm_db_index_usage_stats
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34984120
To find unused indexes:
DECLARE  @dbid INT
SELECT @dbid = DB_ID(DB_NAME())
SELECT   OBJECTNAME = OBJECT_NAME(I.OBJECT_ID),
            INDEXNAME = I.NAME,
            I.INDEX_ID
    FROM     SYS.INDEXES I
            JOIN SYS.OBJECTS O
            ON I.OBJECT_ID = O.OBJECT_ID
    WHERE    OBJECTPROPERTY(O.OBJECT_ID,'IsUserTable') = 1
        AND I.INDEX_ID NOT IN (
    SELECT S.INDEX_ID
        FROM   SYS.DM_DB_INDEX_USAGE_STATS S
        WHERE  S.OBJECT_ID = I.OBJECT_ID
            AND I.INDEX_ID = S.INDEX_ID
            AND DATABASE_ID = @dbid)
    ORDER BY OBJECTNAME,
            I.INDEX_ID,
            INDEXNAME ASC

Open in new window

0
 
LVL 15

Accepted Solution

by:
Aaron Shilo earned 500 total points
ID: 34986167
this will list unused indexes:

the condition  (user_seeks + user_scans + user_lookups ) = 0
will retrive indexes with no seeks scans or lookups

declare @dbid int
select @dbid = db_id()
select objectname=object_name(s.object_id), s.object_id
      , indexname=i.name, i.index_id
      , user_seeks, user_scans, user_lookups, user_updates
from sys.dm_db_index_usage_stats s,
      sys.indexes i
where database_id = @dbid
and objectproperty(s.object_id,'IsUserTable') = 1
and i.object_id = s.object_id
and i.index_id = s.index_id
and  (user_seeks + user_scans + user_lookups) = 0
order by (user_seeks + user_scans + user_lookups) asc
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question