select into VS insert select VS bulk insert 效率测试

本文通过对比select into、insert select及bulk insert三种方法在SQL Server中插入800万条记录的速度,测试并分析了不同批量插入方式的执行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--select into VS insert select VS bulk insert 效率测试
--数据库恢复模式:简单


--Basic table
use tempdb;
GO

if(object_id('dbo.orders','U') is not NULL) drop table dbo.orders;
GO
create table dbo.orders(
orderid int not null,
orderdate date not null,
empid int not null,
custid varchar(10) not null
)

--准备基础数据(800w rowsinsert into dbo.orders 
select * from 
(values 
(10002,'20170505',1,'B'),
(10003,'20170506',2,'B'),
(10004,'20170506',1,'A'),
(10002,'20170505',1,'B'),
(10003,'20170506',2,'B'),
(10004,'20170506',1,'A')) as D(id,date,eid,cid); 

insert into dbo.orders 
select * from dbo.orders;

select count(1) from dbo.orders;--8000000

-----select into VS insert select VS bulk insert
set statistics time on;
set nocount on;
--select into
select * into tableA from dbo.orders;
/*
SQL Server 分析和编译时间: 
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。

 SQL Server 执行时间:
   CPU 时间 = 5078 毫秒,占用时间 = 5427 毫秒。
   */
--insert select
create table dbo.tableB(
orderid int not null,
orderdate date not null,
empid int not null,
custid varchar(10) not null
);
insert into tableB 
select * from dbo.orders;
/*SQL Server 分析和编译时间: 
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
SQL Server 分析和编译时间: 
   CPU 时间 = 2 毫秒,占用时间 = 2 毫秒。

 SQL Server 执行时间:
   CPU 时间 = 12562 毫秒,占用时间 = 31522 毫秒。
   */
--bulk insert
create table dbo.tableC(
orderid int not null,
orderdate date not null,
empid int not null,
custid varchar(10) not null
);
bulk insert tableC 
from 'C:\CSVFile.csv'
with(
datafiletype='char',
fieldterminator=',',
rowterminator ='\n'
);
/*SQL Server 分析和编译时间: 
   CPU 时间 = 16 毫秒,占用时间 = 25 毫秒。

 SQL Server 执行时间:
   CPU 时间 = 24937 毫秒,占用时间 = 32332 毫秒。*/

--speed testing results:
--select into >> insert select >= bulk insert


drop table tableA;
drop table tableB;
drop table tableC;
drop table dbo.orders;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值