The SQL query below shows a list of tables sorted by the largest tables first. For each table, the indexes in the table are shown. For each index it shows when the index was last used. The query is designed to collect various pieces of information in one place, and give an overview to help supporting decisions when doing index tuning.
The left hand columns show data for the table (No. of receords, data- and index size) to have a view of the impact of having indexes on the table.
The right hand columns show data for each index, including Updates (costs) and Reads (benefits) and when it was last used sine the last time SQL Server was restarted.
Further comments:
If you have comments or feedback, please feel free to post them here.
Best regards
Lars Lohndorf-Larsen
Microsoft Dynamics UK
Microsoft Customer Service and Support (CSS) EMEA
--use NavisionDatabase
IF
DROP
-- Generate list of indexes with key list
create
[l1] [int]
[F_Obj_ID] [int]
[F_Schema_Name] [nvarchar]
[F_Table_Name] [nvarchar]
[F_Row_Count] [int]
[F_Reserved] [int]
[F_Data] [int]
[F_Index_Size] [int]
[F_UnUsed] [int]
[F_Index_Name] [nvarchar]
[F_Index_ID] [int]
[F_Column_Name] [nvarchar]
[F_User_Updates] [int]
[F_User_Reads] [int]
[F_Last_Used] [datetime]
[F_Index_Type] [nvarchar]
[F_Index_Column_ID] [int]
[F_Last_Seek] [datetime]
[F_Last_Scan] [datetime]
[F_Last_Lookup] [datetime]
[Index_Key_List] [nvarchar]
)
go
CREATE
(
insert
z_IUQ2_Temp_Index_Keys
SELECT
a1
a3
a2
-- Index Description
SI
index_col
-- Index Stats
US
case
when
else
end
SIC
''
FROM
ps
LEFT
it
INNER
inner
left
WHERE
order
-- Populate key string
declare
select
for
set
open
fetch
while
from
where
ORDER
update
close
deallocate
-- clean up table to one line per index
delete
[F_Table_Name] TableName
[F_Row_Count] No_Of_Records
[F_Data] Data_Size
[F_Index_Size] Index_Size
[F_UnUsed] UnUsed_Space
[F_Index_Name] Index_Name
[F_User_Updates] Index_Updates
[F_User_Reads] Index_Reads
[F_Last_Used] Index_Last_Used
[F_Index_Type] Index_Type
[Index_Key_List] Index_Fields