30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Indexing Strategy and Maintenance<br />

updated. A similar calculation can be used to get the total system reads of an index.<br />

However, we'll ignore any system activity from this point forward as it is almost always<br />

negligible in comparison to user-driven activity.<br />

Over the coming sections, we'll present scripts to:<br />

• find indexes on your system that have never been read or written<br />

• find indexes that have never been read but are being maintained (i.e. updated in<br />

response to modification of the underlying table data)<br />

• get detailed read/write stats on all indexes, looking for those where the maintenance<br />

burden may outweigh their usefulness in boosting query performance.<br />

These indexes are candidates for removal, after thorough investigation. You should<br />

never blindly drop indexes, and you must be certain that an index really isn't used (e.g. by<br />

infrequent, yet critical, monthly or quarterly reporting queries) before dropping it.<br />

Identify indexes that have never been accessed<br />

Listing 5.4 uses sys.indexes and sys.objects to find tables and indexes in the current<br />

database that do not show up in sys.dm_db_index_usage_stats. This means that<br />

these indexes have had no reads or writes since <strong>SQL</strong> <strong>Server</strong> was last started, or since the<br />

current database was closed or detached, whichever is shorter.<br />

-- List unused indexes<br />

SELECT OBJECT_NAME(i.[object_id]) AS [Table Name] ,<br />

i.name<br />

FROM sys.indexes AS i<br />

INNER JOIN sys.objects AS o ON i.[object_id] = o.[object_id]<br />

WHERE i.index_id NOT IN ( SELECT ddius.index_id<br />

FROM sys.dm_db_index_usage_stats AS ddius<br />

WHERE ddius.[object_id] = i.[object_id]<br />

AND i.index_id = ddius.index_id<br />

185

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!