在搜索stackoverflow.com後,我發現有幾個問題要求如何刪除重複項目,但沒有一個能夠解決速度問題。刪除重複數據的最快技術
在我的情況下,我有一個10列的表,其中包含500萬確切的行重複。另外,我還有至少一百萬個其他行在10列中的9列中有重複。我目前的技術是(目前爲止)3小時刪除這500萬行。這裏是我的過程:
-- Step 1: **This step took 13 minutes.** Insert only one of the n duplicate rows into a temp table
select
MAX(prikey) as MaxPriKey, -- identity(1, 1)
a,
b,
c,
d,
e,
f,
g,
h,
i
into #dupTemp
FROM sourceTable
group by
a,
b,
c,
d,
e,
f,
g,
h,
i
having COUNT(*) > 1
接下來,
-- Step 2: **This step is taking the 3+ hours**
-- delete the row when all the non-unique columns are the same (duplicates) and
-- have a smaller prikey not equal to the max prikey
delete
from sourceTable
from sourceTable
inner join #dupTemp on
sourceTable.a = #dupTemp.a and
sourceTable.b = #dupTemp.b and
sourceTable.c = #dupTemp.c and
sourceTable.d = #dupTemp.d and
sourceTable.e = #dupTemp.e and
sourceTable.f = #dupTemp.f and
sourceTable.g = #dupTemp.g and
sourceTable.h = #dupTemp.h and
sourceTable.i = #dupTemp.i and
sourceTable.PriKey != #dupTemp.MaxPriKey
就如何加快這或更快的方式有什麼建議?請記住,我將不得不再次對不完全重複的行執行此操作。
非常感謝。
更新:
我不得不停止第2步跑在9小時大關。 我嘗試了OMG Ponies的方法,僅用了40分鐘就完成了。 我嘗試了第2步Andomar的批量刪除,它運行了9個小時,然後我停止了它。 更新: 使用OMG Ponies的方法,使用少一個字段的類似查詢來擺脫不同的重複集合,並且查詢僅運行4分鐘(8000行)。
我會嘗試下cte技術,但是,我懷疑OMG小馬的方法很難擊敗。
A到你的查詢夫妻容易的優化上面 - 你不必有A,B,C等頂層查詢的'SELECT' - 你只需要PriKey,並刪除HAVING - 那麼,在第二個查詢只是'DELETE FROM sourceTable WHERE PriKey NOT IN(SELECT DT.MaxPriKey FROM #dupTemp DT)' – 2010-08-17 22:30:49
感謝您的提示。 – 2010-08-17 22:35:40