Sql-Server
刪除聚集索引後,它仍在嘗試進行聚集插入
我正在嘗試將數據從一個數據庫系統轉換到另一個數據庫系統。我需要傳輸和格式化的表之一包含超過 1000 萬行。
我正在執行以下腳本來執行此操作:
USE [Cvti101687] go truncate table [IDAT_MR_NOTEHISTORY] go IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[IDAT_MR_NOTEHISTORY]') AND name = N'PK_iMR_NOTES') ALTER TABLE [dbo].[IDAT_MR_NOTEHISTORY] DROP CONSTRAINT [PK_iMR_NOTES] GO USE [101687_test2] GO declare @ChunkCounter int declare @ChunkSize int set @ChunkCounter = 0 set @ChunkSize = 50000 --hack to do a do-while loop in sql while 1 = 1 begin insert into cvti101639..IDAT_MR_NOTEHISTORY with (tablock) SELECT newid(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000' ,'00000000-0000-0000-0000-000000000000', ISNULL(TargetClientAccountNum, [REC_CLINUM]), ISNULL(TargetPetAccountNum, ' ') ,ISNULL(pck_desc, 'Converted Medical Record'), isnull([REC_DOCTOR], '99999'), isnull([REC_ENTRY],''), isnull(REC_ENTRY,''),[rec_dattim], 0 FROM [RECORD] left join [WVSSPCK] on pck_code = 99 and pck_link = [REC_TAG] left join cvti101639..idat_patients p on p.oldAccountNum = REC_CASE where rec_deleted = 0 and (rec_rtype = 0 or rec_rtype is null) and (TargetClientAccountNum is not null or [REC_CLINUM] is not null) and rec_id >= @ChunkCounter and rec_id < @ChunkCounter + @ChunkSize --If no rows inserted, break out of the loop if(@@rowcount = 0) break; set @ChunkCounter = @ChunkCounter + @ChunkSize end go USE [Cvti101687] go ALTER TABLE [dbo].[IDAT_MR_NOTEHISTORY] ADD CONSTRAINT [PK_iMR_NOTES] PRIMARY KEY CLUSTERED ( [cvtGUID] ASC )
為了加快速度,我嘗試在目標表上刪除聚集索引並在最後重建它(是的,我知道它是 GUID 上的聚集索引,我只是轉換人員,我沒有發言權數據庫是如何設計的)。
但是,當從插入的 while 循環的迭代之一查看實際執行計劃時,我看到它正在對我已經刪除的索引執行聚集索引插入。此外,它在聚集索引插入之前執行的排序花費了每個塊的 69%。
|--Clustered Index Insert(OBJECT:([Cvti101639].[dbo].[IDAT_MR_NOTEHISTORY].[PK_iMR_NOTES]), SET:([Cvti101639].[dbo].[IDAT_MR_NOTEHISTORY].[cvtGUID] = RaiseIfNull([Expr1012]),[Cvti101639].[dbo].[IDAT_MR_NOTEHISTORY].[cvtClientGUID] = [Expr1013],[Cvti |--Parallelism(Gather Streams, ORDER BY:([Expr1012] ASC)) |--Sort(ORDER BY:([Expr1012] ASC)) |--Compute Scalar(DEFINE:([Expr1012]=newid(), [Expr1013]={guid'00000000-0000-0000-0000-000000000000'}, [Expr1014]={guid'00000000-0000-0000-0000-000000000000'}, [Expr1015]={guid'00000000-0000-0000-0000-000000000000'}, [Expr1016]={guid' (snip)
這是完成分塊循環的前幾次迭代後實際執行計劃的副本。只需將 xml 保存為
.sqlplan
文件,它應該可以在 Management Studio 中打開。我做錯了什麼導致這沒有做堆插入?
您正在刪除數據庫中的主鍵
Cvti101687
並插入到數據庫中的表中cvti101639
。