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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Indexing Strategy and Maintenance<br />

5.1, which returns some statistics from the dm_db_index_usage_stats DMV, and<br />

joins to sys.indexes to get the index names.<br />

SELECT DB_NAME(ddius.[database_id]) AS database_name ,<br />

OBJECT_NAME(ddius.[object_id], DB_ID('AdventureWorks'))<br />

AS [object_name] ,<br />

asi.[name] AS index_name ,<br />

ddius.user_seeks + ddius.user_scans + ddius.user_lookups AS user_reads<br />

FROM sys.dm_db_index_usage_stats ddius<br />

INNER JOIN AdventureWorks.sys.indexes asi<br />

ON ddius.[object_id] = asi.[object_id]<br />

AND ddius.index_id = asi.index_id ;<br />

Listing 5.1: Querying index use in the AdventureWorks database.<br />

The results are shown in Figure 5.1.<br />

Figure 5.1: Results for indexes in the AdventureWorks database.<br />

Firstly, you may notice the use of the OBJECT_NAME function to return the table/view<br />

that owns the indexes retrieved from sys.dm_db_index_usage_stats. Note that<br />

database_id is not an identified column in these system catalog views. This is because<br />

these views reside in each database on the <strong>SQL</strong> <strong>Server</strong> instance, not <strong>with</strong>in one of the<br />

system databases (globally) across the instance. Therefore, we'll make use of a littleknown<br />

feature of the OBJECT_NAME function which, in <strong>SQL</strong> 2005 and later editions,<br />

accepts the database_id in the second parameter slot. In Listing 5.1, we could have<br />

simply used:<br />

OBJECT_NAME(ddius.[object_id], ddius.[database_id]) AS [object_name]<br />

174

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

Saved successfully!

Ooh no, something went wrong!