大家都知道,索引能提升数据库的查询和更新效率。那么当SQL SERVER每天都运行那么多的SQL语句的时候,怎么才能快速的找到效率提升点呢?
在SQL SERVER中,有一批动态管理视图(DMV)会存放missing index的信息(SQL SERVER启动以来),这些动态管理视图就是sys.dm_db_missing_index_###
使用下面的SQL能快速的找到当前数据库中各个表缺失的索引,以及这些索引可能带来的收益.
SELECT TOP 25
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.avg_user_impact,dm_migs.user_seeks,dm_migs.user_scans,
dm_migs.last_user_seek AS Last_User_Seek,
object_name(dm_mid.object_id,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + object_name(dm_mid.object_id,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') +
CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN '_' ELSE '' END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')+ ']'+ ' ON ' + dm_mid.statement
+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END
+ ISNULL (dm_mid.inequality_columns, '') + ')' + ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
GO
从上面的截图可以看出,第三列显示,缺失的索引能提升的性能比例,第四列则是这个索引查找次数(seek),第五列则是扫描次数(scan).
一般情况下,我们都会执行提升效率最高的那个创建索引的SQL(Create_Statement),但是尤其要注意的是,部分Create_Statement会建立同名的但是包含性列(include)不一样的索引,也会建立一些包含另一个索引的无效索引,因此在执行前需要针对同一个表的索引进行筛选后再新建.
最后,尽量少的建立索引,能合并的两个或多个索引尽量合并成一个.
毕竟索引会影响插入和更新效率的.
用起来!!!