SQL 資料插入 刪除 大資料

2021-09-11 15:13:22 字數 3208 閱讀 1384

**:

幾種資料庫的大資料批量插入: 

--測試表

create table [dbo].[employee] (

[employeeno] int primary key,

[employeename] [nvarchar](50) null,

[createuser] [nvarchar](50) null,

[createdatetime] [datetime] null

);

--1、迴圈插入

set statistics time on;

declare @index int = 1;

declare @timer datetime = getdate();

while @index <= 100000

begin

insert [dbo].[employee](employeeno, employeename, createuser, createdatetime) values(@index, 'employee_' + cast(@index as char(6)), 'system', getdate());

set @index = @index + 1;

endselect datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

--2、事務迴圈

begin tran;

set statistics time on;

declare @index int = 1;

declare @timer datetime = getdate();

while @index <= 100000

begin

insert [dbo].[employee](employeeno, employeename, createuser, createdatetime) values(@index, 'employee_' + cast(@index as char(6)), 'system', getdate());

set @index = @index + 1;

endselect datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

commit;

--3、批量插入

set statistics time on;

declare @timer datetime = getdate();

insert [dbo].[employee](employeeno, employeename, createuser, createdatetime)

select top(100000) employeeno = row_number() over (order by c1.[object_id]), 'employee_', 'system', getdate()

from sys.columns as c1 cross join sys.columns as c2

order by c1.[object_id]

select datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

--4、cet插入

set statistics time on;

declare @timer datetime = getdate();

;with cte(employeeno, employeename, createuser, createdatetime) as(

select top(100000) employeeno = row_number() over (order by c1.[object_id]), 'employee_', 'system', getdate()

from sys.columns as c1 cross join sys.columns as c2

order by c1.[object_id]

)insert [dbo].[employee] select employeeno, employeename, createuser, createdatetime from cte;

select datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

--5、迴圈刪除

set statistics time on;

declare @timer datetime = getdate();

delete from [dbo].[employee];

select datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

--6、批量刪除

set statistics time on;

declare @timer datetime = getdate();

set rowcount 100000;

while 1 = 1

begin

begin tran

delete from [dbo].[employee];

commit

if @@rowcount = 0

break;

endset rowcount 0;

select datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

--6、truncate刪除

set statistics time on;

declare @timer datetime = getdate();

truncate table [dbo].[employee];

select datediff(ms, @timer, getdate()) as [執行時間(毫秒)];

set statistics time off;

大資料量表刪除插入

1。alter table t nologging 不記錄日誌,完成後記錄日誌。2。先停用索引,在全部操作完成後啟用索引。3。多次小批量提交。4。選擇業務操作量少的時間進行。create or replace procedure delbigtab p tablename in varchar2,p...

SQL 插入資料

1.1 插入完整的行 注意 into 關鍵字 上面的sql語句高度依賴於表中列的定義次序,還依賴於其容易獲得的次序資訊。即使可以得到這種次序資訊,也不能保證各列再下一次表結構變動後保持完全相同的次序。因此編寫依賴於特定列次序的sql語句是很不安全的。編寫insert語句的更安全 不過更煩瑣 inse...

資料插入更新刪除

insert into 表名 values 值1,值2 insert into student id,name,age,email value 1 張三 18 男 zhangsan 163.com insert into 表名 欄位名1 欄位名2 value 值1,值2 insert into 表名...