讓你的insert操作速度增加1000倍的方法

2022-09-20 11:18:15 字數 1800 閱讀 5077

很多時候方法選對了對於我們做事將會是事半功倍。

大家平時都會使用insert語句,特別是有時候需要乙個大批量的資料來做測試,一條一條insert將會是非常程式設計客棧慢的,那麼我們如何讓我們的inser更快呢。

先看個例子:

我們需要在如下這個表中插入測試資料,包含兩列,乙個是itemid,乙個是itemname。如果向這個表中插入103,680,000crlzoovfb 條記錄,普通的插入方法可能需要20多天程式設計客棧才能完成,但是用這裡介紹的新方法在5個小時內就能夠完成。

先看一般的資料插入方法,假設我們向上表中插入100000 條資料:

複製** **如下:

create table #temptable([item id] [bigint], [item name] nvarchar(30))

declare @counter int

set @counter = 1

while (@counter < 100000)

begin

insert into #temptable values (@counter, 'hammer')

set @counter = @counter + 1

end

select * from #temptable

drop table #temptable

新的插入方法會使用已經插入的資料來進行下一條記錄的操作,原理如下:

那麼看看我的新insert**:

複製** **如下:

create table #temptable([item id] [bigint], [item name] nvarchar(30))

insert into #temptable values (1, 'hammer')

while((select count(*) from #temptable) < 100000)

begin

insert into #temptable ([item id], [item name])

(select [item id] + (select count(*) from #temptable), 'hammer' from #temptable)

end

select * from #temptable

drop table #temptable

用第一種方法可能需要幾十分鐘插入100000資料,但是用第二種只要4秒鐘。再改進下,2秒鐘就完成:

複製** **如下:

create 程式設計客棧table #temptable([item id] [bigint], [item name] nvarchar(30))

insert into #temptable values (1, 'hammer')

declare @counter int

set @counter = 1

while(@counter <= 17)

begin

insert into #temptable ([item id], [item name])

(select [item id]程式設計客棧 + (select count(*) from #temptable), 'hammer' from #temptable)

set @counter = @counter + 1

end

select * from #temptable

drop table #temptable

本文標題: 讓你的insert操作速度增加1000倍的方法

本文位址:

如果讓你的insert操作速度增加1000倍

很多時候方法選對了對於我們做事將會是事半功倍。大家平時都會使用insert語句,特別是有時候需要乙個大批量的資料來做測試,一條一條insert將會是非常慢的,那麼我們如何讓我們的inser更快呢。先看個例子 我們需要在如下這個表中插入測試資料,包含兩列,乙個是itemid,乙個是itemname。如...

INSERT語句的速度

mysql 5.5中文參考手冊 文件insert語句的速度 插入乙個記錄需要的時間由下列因素組成,其中的數字表示大約比例 這不考慮開啟表的初始化開銷,每個併發執行的查詢開啟。表的大小以logn b樹 的速度減慢索引的插入。加快插入的一些方法 如果同時從同乙個客戶端插入很多行,使用含多個value的i...

怎讓使trigger 中的insert自動提交

create or replace trigger bi order tab before insert on order tab for each row declare pragma autonomous transaction begin insert into order tran comi...