代码改变世界

sqlserver 大数据量的insert、delete操作优化

2012-04-20 12:18  java环境变量  阅读(357)  评论(0编辑  收藏  举报

经常使用的语句!

 

--大批量导出orders表:insert

DBCC DROPCLEANBUFFERS 
DBCC FREEPROCCACHE
go
SET NOCOUNT ON
BEGIN TRANSACTION 
INSERT INTO test.dbo.orders with(tablock) SELECT * FROM
bak.dbo.Orders
WHERE ordertime BETWEEN '2010-05-01' AND '2010-05-10'
COMMIT
go


--大批量导出orders表:delete

DBCC DROPCLEANBUFFERS 
DBCC FREEPROCCACHE
go
SET NOCOUNT ON
BEGIN TRANSACTION 
while 1=1 
begin
 delete top(1000) from test.dbo.orders with(tablock)
WHERE ordertime BETWEEN '2010-05-01' AND '2010-05-10'
if @@rowcount<1000
 break 
end
COMMIT
go