在表中插入行/更新行时,哪个索引对性能有好处?

时间:2013-10-25 08:25:23

标签: sql-server tsql database-performance

表具有主键聚簇索引。 上表经常发生死锁,导致数据丢失。 在插入行或更新行时,哪个索引最好添加哪个具有更好的性能?

表: ID Int Identity, EntityID Int, EntityName nvarchar(200), MeasuringID Int, Cratedon datetime

主键群集:ID

每次进入表格时,记录都会非常频繁地插入。

**要插入和更新的存储过程中的任何隔离级别或锁定?

1 个答案:

答案 0 :(得分:0)

这可能是一个非常复杂的问题,尤其是在与索引性能相关时,但在咨询顾问之前,只要问问你的SQL服务器,不要盲目地创建这些,他们只是给你一个指示。 同时记住填充因子,这可能会对您的插入性能产生有利影响。 需要2008及更高版本,并将生成脱机重建的代码。对大约1k的影响表示需要注意

    USE <YourDBName>
    SELECT (avg_total_user_cost * avg_user_impact/100.0) * (user_scans +user_seeks ) Impact
    , [Table] = [statement]
    , [CreateIndexStatement] = 'CREATE NONCLUSTERED INDEX IX_' + sys.objects.name COLLATE DATABASE_DEFAULT  + '_' 
    + REPLACE(REPLACE(REPLACE(ISNULL(mid.equality_columns,'')+ISNULL(mid.inequality_columns,''), '[', ''), ']',''), ', ','_') + ' ON ' + [statement] 
    + REPLACE(' ( ' + IsNull(mid.equality_columns, '')
    + CASE WHEN mid.inequality_columns IS NULL THEN '' ELSE CASE WHEN mid.equality_columns IS NULL THEN '' ELSE ',' END + mid.inequality_columns END + ' ASC ) ' ,',',' ASC ,') + CASE WHEN mid.included_columns IS NULL THEN '' ELSE 'INCLUDE (' + mid.included_columns + ')' END 
    + 'WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = ON, SORT_IN_TEMPDB = ON, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON,FILLFACTOR = 90) ON [PRIMARY];'
    , mid.equality_columns
    , mid.inequality_columns
    , mid.included_columns
    FROM sys.dm_db_missing_index_group_stats AS migs 
    INNER JOIN sys.dm_db_missing_index_groups AS mig ON migs.group_handle = mig.index_group_handle 
    INNER JOIN sys.dm_db_missing_index_details AS mid ON mig.index_handle = mid.index_handle 
    INNER JOIN sys.objects WITH (nolock) ON mid.OBJECT_ID = sys.objects.OBJECT_ID 
    WHERE (migs.group_handle IN (SELECT TOP (500) group_handle FROM sys.dm_db_missing_index_group_stats WITH (nolock) ORDER BY (avg_total_user_cost * avg_user_impact) * (user_seeks + user_scans) DESC))  
    AND OBJECTPROPERTY(sys.objects.OBJECT_ID, 'isusertable') = 1 
    ORDER BY Impact DESC, [CreateIndexStatement] DESC ;