假设我有一个包含5列的表:
>>> from timeit import timeit
>>> 10 ** 6
1000000
>>> timeit("for _ in range(10 ** 6): pass", number=100)
3.0651066239806823
>>> timeit("for _ in [None] * (10 ** 6): pass", number=100)
1.9346517859958112
>>> timeit("for _ in repeat(None, 10 ** 6): pass", 'from itertools import repeat', number=100)
1.4315521717071533
此表有37 000 000行。
每个基金大约有4000行。从这个表中删除行的最佳和最快捷的方法是什么。 我需要一次删除大约7000000行,但大约需要10分钟,而且我需要这么多。
目前我通过Fund_Id删除行,如下所示:
Id int primary key
Date datetime
Value double
Fund_id reference
FundModel_id reference
FundDataField_id reference
此语句将影响大约200 000行,并且需要很长时间才能完成,将此语句分成2个查询,我的性能会有所提高,每个大约4秒。
有人知道更好的解决方案吗?
注意:如果有人知道使用Nhibernate的更好解决方案,我使用Fluent NHibernate进行数据访问请告诉我。 如果我要进行商店程序,这会增加我的表现吗? 感谢。
答案 0 :(得分:2)
您可以执行以下批量删除:
SELECT 'Starting' --sets @@ROWCOUNT
WHILE @@ROWCOUNT <> 0
DELETE TOP (50000) dbo.timesheet --change top value as needed
WHERE Fund_id IN (2054,2056,2058,2059,2061,2063,2064,2065,2066,2067,2069,2072,2073,2076,
2078,2079,2080,2081,2082,2086,2088,2090,2093,2095,2096,2097,2099,2101,
2102,2103,2104,2105,2106,2107,2109,2110,2114,2115,2116,2117,2118,2119,
2342,2125,2126,2127,2128,2129,2130,2131
)
由@gbn提供:Bulk Delete on SQL Server 2008
<强>更新强>
或者,您可以通过将要保留的记录插入临时表中然后截断实际表来尝试此方法。然后,将这些临时表记录传回到您的实际表中。不确定你会从中获得多少收益,但绝对会建议在执行此操作之前执行BACKUP
:
SELECT col1, col2, col3, col4, col5 INTO #Holdingtable
FROM FundYearDetail WHERE Fund_id NOT IN (2054,2056,2058,2059,2061,2063,2064,2065,
2066,2067,2069,2072,2073,2076,2078,2079,2080,2081,2082,2086,2088,2090,
2093,2095,2096,2097,2099,2101,2102,2103,2104,2105,2106,2107,2109,2110,
2114,2115,2116,2117,2118,2119,2342,2125,2126,2127,2128,2129,2130,2131
)
TRUNCATE TABLE FundYearDetail
INSERT FundYearDetail (
col1
,col2
,col3
,col4
,col5
)
SELECT
col1
,col2
,col3
,col4
,col5
FROM #Holdingtable
答案 1 :(得分:1)
这样怎么样:
decalre @tableIds table (Id int)
insert into @tableIds
select 2054 as Id union all
...
//here is other ids
...
select 2131 as Id
while exists(select 1 from FundYearDetail t1 join @table t2 on t1.Fund_id = t2.Id)
begin
delete top(10000) t1
from FundYearDetail t1 join @table t2 on t1.Fund_id = t2.Id
end