2012-10-29 92 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