mssql如何快速插入大級別資料

2021-06-26 21:11:45 字數 2265 閱讀 3214

提供兩個方法實現插入大級別資料(1.使用set nocount on+transaction,2.推薦使用cte)

1.加上set nocount on,並盡可能加上transaction

truncate table customers

go----清除干擾查詢

dbcc dropcleanbuffers

dbcc freeproccache

set statistics io on;

set statistics time on;

godeclare @d datetime

set @d=getdate();

set nocount on -----我是set nocount on

declare @i int=1

begin transaction -----我是transaction

while @i<=10000

begin

insert into customers (customernumber, customername,

customercity)

select replace(str(@i, 4), ' ', '0'),'customer ' + str(@i,6),

char(65 + (@i % 26)) + '-city'

set @i=@i+1

endcommit

select [語句執行花費時間(毫秒)]=datediff(ms,@d,getdate())

set statistics io off ;

set statistics time off;

go

set nocount on的作用:使返回的結果中不包含有關受 transact-sql 語句影響的行數的資訊。這在批量插入時將顯著提高效能

2.推薦使用cte

本人試過用遞迴cte插入1千萬資料,大概花了15分鐘左右,效率還是相當可以的。

truncate table customers

godbcc dropcleanbuffers

dbcc freeproccache

set statistics io on;

set statistics time on;

godeclare @d datetime

set @d=getdate();

with seq (num,customernumber, customername, customercity) as

(select 1,cast('0000'as char(4)),cast('customer 0' as nvarchar(50)),cast('x-city' as nvarchar(20))

union all

select num + 1,cast(replace(str(num, 4), ' ', '0') as char(4)),

cast('customer ' + str(num,6) as nvarchar(50)),

cast(char(65 + (num % 26)) + '-city' as nvarchar(20))

from seq

where num <= 10000

)insert into customers (customernumber, customername, customercity)

select customernumber, customername, customercity

from seq

option (maxrecursion 0)

select [語句執行花費時間(毫秒)]=datediff(ms,@d,getdate())

set statistics io off ;

set statistics time off;

go

如果你覺得上面的遞迴cte看著有點亂,下面給你提供它的縮減版

with seq (num) as

(select 1

union all

select num + 1

from seq

where num <= 10000

)select * from seq

option (maxrecursion 0)



PHP 千萬級別資料插入

header content type text html charset utf 8 設定 執行不受時間限制 set time limit 0 鏈結資料庫 con mysqli connect 127.0.0.1 root test if mysqli connect error 設定編碼為utf...

千萬級別資料插入實現方案

上次面試問我上萬級別的資料如何快速插入資料庫,當時不知怎麼回答,回來通過查資料和實踐,通過執行緒池和事務管理實現了批量快速插入資料,特地總結一下。目錄結構,乙個簡單的springboot工程 首先建立乙個普通的表只有三個字段 create database if not exists demo us...

JAVA使用POI如何匯出百萬級別資料

經常使用excel的人應該都能知道excel2007及以上版本可以輕鬆實現儲存百萬級別的資料,但是系統中的大量資料如何能夠快速準確的匯入到excel中這好像是個難題,對於一般的web系統,我們為了解決成本,基本都是使用的入門級web伺服器tomcat,jdk在32為系統中支援的記憶體不能超過2個g,...