mysql存儲(chǔ)中使用while批量插入數(shù)據(jù)(批量提交和單個(gè)提交的區(qū)別)
批量提交
while 語(yǔ)句寫法:
? ? while '條件' do ? ? ? ? ? ? 循環(huán)體語(yǔ)句; ? ? end while;
完整寫法
drop procedure if exists test_insert; delimiter $$ create procedure test_insert(n int) ?? ?begin ?? ?declare v int default 0; ?? ?set AUTOCOMMIT = 0; ?? ?while v < n ?? ??? ?do ?? ??? ??? ??? ?insert into test(second_key, text, field_4,status, create_date) ?? ??? ??? ??? ?values ((v*10), ?? ??? ??? ??? ?concat('t',v), ? ? ? ? ? ? ? ? substring(md5(rand()), 1, 10), ? ? ? ? ? ? ? ? 'good', ? ? ? ? ? ? ? ? adddate('1970-01-01', rand(v) * 10000)); ? ? ? ? set v = v + 1; ? ? ?end while; ? ? ? set AUTOCOMMIT = 1; end$$ delimiter ;
查看、刪除存儲(chǔ)過程:
mysql> show procedure status like 'test_insert'; mysql> show create procedure test_insert\G; mysql> drop procedure if exists test_insert;
創(chuàng)建表
CREATE TABLE test ( id INT NOT NULL AUTO_INCREMENT, second_key INT, text VARCHAR(20), field_4 VARCHAR(20), status VARCHAR(10), create_date date, PRIMARY KEY (id), KEY idx_second_key (second_key) ) Engine=InnoDB CHARSET=utf8;
插入100萬(wàn)條數(shù)據(jù)
mysql> call test_insert(1000000); Query OK, 0 rows affected (31.86 sec)
單個(gè)提交
完整寫法
drop procedure if exists test_insert; delimiter $$ create procedure test_insert(n int) ?? ?begin ?? ?declare v int default 0; ?? ?while v < n ?? ??? ?do ?? ??? ??? ??? ?insert into test(second_key, text, field_4,status, create_date) ?? ??? ??? ??? ?values ((v*10), ?? ??? ??? ??? ?concat('t',v), ? ? ? ? ? ? ? ? substring(md5(rand()), 1, 10), ? ? ? ? ? ? ? ? 'good', ? ? ? ? ? ? ? ? adddate('1970-01-01', rand(v) * 10000)); ? ? ? ? set v = v + 1; ? ? ?end while; end$$ delimiter ;
插入1萬(wàn)條數(shù)據(jù)
mysql> call test_insert(10000); Query OK, 1 row affected (1 min 8.52 sec)
打開另一個(gè)窗口查看
mysql> select count(*) from test.test; +----------+ | count(*) | +----------+ | ? ? 1428 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) from test.test; +----------+ | count(*) | +----------+ | ? ? 1598 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) from test.test; +----------+ | count(*) | +----------+ | ? ? 1721 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) from test.test; +----------+ | count(*) | +----------+ | ? ? 1983 | +----------+ 1 row in set (0.00 sec)
結(jié)論
批量提交100萬(wàn)條數(shù)據(jù)用了30秒,單個(gè)提交1萬(wàn)條數(shù)據(jù)用了1分鐘,對(duì)比發(fā)現(xiàn),批量提交的效率遠(yuǎn)大于單個(gè)提交的效率
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SQL實(shí)戰(zhàn)演練之網(wǎng)上商城數(shù)據(jù)庫(kù)用戶信息數(shù)據(jù)操作
一直認(rèn)為,扎實(shí)的SQL功底是一名數(shù)據(jù)分析師的安身立命之本,甚至可以稱得上是所有數(shù)據(jù)從業(yè)者的基本功。當(dāng)然,這里的SQL絕不單單是寫幾條查詢語(yǔ)句那么簡(jiǎn)單,接下來(lái)請(qǐng)跟著小編通過案例項(xiàng)目進(jìn)一步提高SQL的能力吧2021-10-10mysql學(xué)習(xí)筆記之完整的select語(yǔ)句用法實(shí)例詳解
這篇文章主要介紹了mysql學(xué)習(xí)筆記之完整的select語(yǔ)句用法,結(jié)合實(shí)例形式詳細(xì)分析了mysql select語(yǔ)句各種常見參數(shù)、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04MySQL 5.7增強(qiáng)版Semisync Replication性能優(yōu)化
這篇文章主要介紹了MySQL 5.7增強(qiáng)版Semisync Replication性能優(yōu)化,本文著重講解支持發(fā)送binlog和接受ack的異步化、支持在事務(wù)commit前等待ACK兩項(xiàng)內(nèi)容,需要的朋友可以參考下2015-05-05mysql 導(dǎo)出select語(yǔ)句結(jié)果到excel文件遇到問題及解決方法
這篇文章主要介紹了mysql 導(dǎo)出select語(yǔ)句結(jié)果到excel文件遇到問題及解決方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09MySql分表、分庫(kù)、分片和分區(qū)知識(shí)點(diǎn)介紹
數(shù)據(jù)庫(kù)的數(shù)據(jù)量達(dá)到一定程度之后,為避免帶來(lái)系統(tǒng)性能上的瓶頸。需要進(jìn)行數(shù)據(jù)的處理,采用的手段是分區(qū)、分片、分庫(kù)、分表,這里就為大家介紹一下,需要的朋友可以參考下2020-02-02