gpt4 book ai didi

sql - 删除重复数据的最快技术

转载 作者:行者123 更新时间:2023-12-04 13:30:29 24 4
gpt4 key购买 nike

在搜索 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

关于如何加快速度或更快的方法的任何提示?请记住,对于不完全重复的行,我将不得不再次运行它。

非常感谢。

更新:
我不得不在 9 小时标记处停止第 2 步。
我尝试了 OMG Ponies 的方法,仅在 40 分钟后就完成了。
我用 Andomar 的批量删除尝试了我的第 2 步,它在我停止之前运行了 9 个小时。
更新:
使用少一个字段运行一个类似的查询以消除一组不同的重复项,并且使用 OMG Ponies 的方法仅运行了 4 分钟(8000 行)的查询。

下次有机会我会尝试 cte 技术,但是,我怀疑 OMG Ponies 的方法很难被击败。

最佳答案

EXISTS 怎么样:

DELETE FROM sourceTable
WHERE EXISTS(SELECT NULL
FROM #dupTemp dt
WHERE sourceTable.a = dt.a
AND sourceTable.b = dt.b
AND sourceTable.c = dt.c
AND sourceTable.d = dt.d
AND sourceTable.e = dt.e
AND sourceTable.f = dt.f
AND sourceTable.g = dt.g
AND sourceTable.h = dt.h
AND sourceTable.i = dt.i
AND sourceTable.PriKey < dt.MaxPriKey)

关于sql - 删除重复数据的最快技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com