2012-10-29 45 views
3

我很开心测试了sql server 2012的columnstore索引功能。因为您不能使用这样的索引更新/插入表,所以我在一些选项上阅读:保留一个单独的表并为每个批量插入使用新的分区或禁用索引,执行更新/插入,然后重建索引。存储过程中的SQL Server列存储索引更新/插入

对于我的测试,我选择了后者选项,并结束了与此存储过程:

-- Disable the columnstore index. 
ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] DISABLE 

-- Insert data into tick table from staging table. 
insert into Tick 
select [Date], 
     SymbolID, 
     Price 
from TickTemporary 

-- Delete data from staging table. 
delete from TickTemporary 

-- Enable (rebuild) the columnstore index. 
ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] REBUILD 

如果我执行这些行手动一切工作正常。但是,如果我运行该过程,则会出现更新/插入无法在具有列存储索引的表上执行的错误。

这是为什么?

更新:

我跟着我以前接受答案的建议,但我仍然得到同样的事情。

-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

-- Disable the columnstore index. 
EXEC DisableColumnStoreIndex 

-- Insert data into tick table from staging table. 
insert into Tick 
select [Date], 
     SymbolID, 
     Price 
from TickTemporary 

-- Delete data from staging table. 
delete from TickTemporary 

-- Enable (rebuild) the columnstore index. 
EXEC RebuildColumnStoreIndex 

即使试图将“开始过渡”和“承诺过渡”周围的存储过程调用。

使用动态SQL这样的:

declare @sql nvarchar(max) 
set @sql = 
    'insert into Tick 
    select [Date], 
      SymbolID, 
      Price 
    from TickTemporary' 
exec(@sql) 

的作品,但说真的,我想不用动态sql度日。在这种情况下不可能吗?

回答

4

该检查在编译时完成,而不是在执行时完成。将过程分成它自己的,或使用动态SQL。

但作为一般性评论,这不是正确的方法。您应该插入到具有相同结构的不同表中,在此相同表上构建列存储索引,然后使用分区切换将新表替换为新表:将旧表替换为空表,切换新表,将旧数据丢弃。类似于How to Update a table with a Columnstore Index中描述的程序。由于使用了分区开关,因此您的表的用户会经历更短的停机时间,因为旧表在插入期间和构建列存储阶段期间仍旧在线并可用。

+0

是。我计划在未来使用该策略。但是现在这只是我的一个非常小的实验,我没有那么多的数据。但是我已经阅读过你提到的链接,所以我知道这种方法。当这个简单的事情不起作用时,我就陷入了困境。 – UmaN

+0

我不认为你了解这个建议。你的尝试stiull必须编译一个存储过程,插入一个列存储索引,这将触发错误。您必须将INSERT分离为在列存储索引存在时未编译,因此非常简单,并且没有解决方法。动态SQL是一个强制编译在正确的时间发生的例子。 –

+0

对不起。我知道了。虽然我觉得奇怪的是,当你实际执行“CREATE PROCEDURE”或“ALTER PROCEDURE”语句时,没有执行相同的检查。但是,谢谢。 – UmaN

0

解决方案对于此编译时执行是选项(重新编译)

create PROCEDURE TEST 
AS 
BEGIN 

ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] DISABLE 

-- Insert data into tick table from staging table. 

insert into Tick 
select [Date], 
     SymbolID, 
     Price 
from TickTemporary **Option(recompile)** 

-- Delete data from staging table. 
delete from TickTemporary 

-- Enable (rebuild) the columnstore index. 
ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] REBUILD 

End