SQL SERVER 快速插入千万条数据

时间:2021-10-22 21:01:20

1. 正常插入:插入非常慢。

declare @count int = 10000000, @index int = 0
begin tran
while(@index < @count)
begin
	insert into OrderTest(ID) values 
	 (ROUND(RAND() * 10000000, 0))
	set @index = @index + 1
end
commit


2.优化插入:插入速度明显提升。


declare @count int = 10000000, @index int = 0
begin tran
while(@index < @count)
begin
	insert into OrderTest(ID) values
	--1 
	 (ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--2
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--3
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--4
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--5
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--6
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--7
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--8
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--9
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	--10
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	,(ROUND(RAND() * 100000000, 0))
	set @index = @index + 100
end
commit


3.总结:一条语句可插入多条数据,以此来加快插入速度。具体原理,待大神们解释。