插入时的索引性能

时间:2015-05-21 00:33:21

标签: sql-server tsql indexing

我有一个包含4列的表,我在这个表上有3个索引:

   CREATE TABLE [dbo].CustomerInfo(
        ID [int] IDENTITY(1,1) NOT NULL,
        UserHashID [varchar](20) NOT NULL,
        ShippingID [int] NOT NULL,
        Received [bit] NOT NULL,
     CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_CustomerInfo_ShippingID] ON [dbo].[CustomerInfo]  (  [ShippingID] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_CustomerInfo_UserHashID] ON [dbo].[CustomerInfo]  (  [UserHashID] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_CustomerInfo_UserHashIDShippingID] ON [dbo].[CustomerInfo]  (    [UserHashID] ASC,   [ShippingID] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

我正在插入大约4-5百万条记录,此过程大约需要45分钟。我意识到,如果我删除索引,插入速度会更快(2-3分钟)。

通过删除索引,在插入完成后执行插入和重建索引,想知道是否存在任何副作用。如果我启用了索引,整个过程将花费我5分钟,而45分钟。

2 个答案:

答案 0 :(得分:3)

不,删除索引,插入完成后重新构建索引没有副作用(假设在执行插入时没有其他任何东西需要访问表)。

这是一种非常常见的模式。

[那都说,我对4列和3个索引的桌子上的时差感到惊讶。你可以发布你的架构和索引定义]

正如@ PJ8912所指出的,事务日志记录可能会有所不同,具体取决于您备份事务日志的频率。

更新:不相关,但此索引

CREATE NONCLUSTERED INDEX [IDX_CustomerInfo_UserHashID] 
    ON [dbo].[CustomerInfo]  ([UserHashID] ASC)

是多余的,因为它被这个索引覆盖:

CREATE NONCLUSTERED INDEX [IDX_CustomerInfo_UserHashIDShippingID] 
    ON [dbo].[CustomerInfo]  ([UserHashID] ASC, [ShippingID] ASC)

答案 1 :(得分:0)

根据事务日志记录的级别,TLog可能会定期重新创建索引。如果截断索引以消除它们,则不会记录该操作。

创建新索引后,执行计划的统计信息可能不是最新的。您可能希望使用FULL SCAN模式更新统计信息。