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ǔ)過(guò)程:
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)文章
幾個(gè)常見的MySQL的可優(yōu)化點(diǎn)歸納總結(jié)
這篇文章主要介紹了幾個(gè)常見的MySQL的可優(yōu)化點(diǎn)歸納總結(jié),包括在編程時(shí)處理索引、分頁(yè)以及數(shù)據(jù)類型時(shí)可用到的地方,需要的朋友可以參考下2015-05-05
MySQL Community Server壓縮包安裝配置方法
這篇文章主要為大家詳細(xì)介紹了MySQL Community Server壓縮包安裝配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
My Sql 1067錯(cuò)誤與編碼問(wèn)題的解決方案
My Sql 大部分都是用綠色版(解壓版) 然后注冊(cè)服務(wù)簡(jiǎn)單方便,但是配置文件也很讓人糾結(jié),下面小編給大家?guī)?lái)了My Sql 1067錯(cuò)誤與編碼問(wèn)題的解決方案,感興趣的朋友參考下吧2016-11-11
解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問(wèn)題分析
這篇文章主要介紹了解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問(wèn)題分析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
MySQL使用show?effective?grants查看權(quán)限官方解讀
這篇文章主要為大家介紹了MySQL使用show?effective?grants查看權(quán)限,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Mysql從5.6.14安全升級(jí)至mysql5.6.25的方法
這篇文章主要介紹了Mysql從5.6.14安全升級(jí)至mysql5.6.25的方法,本教程講的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-08-08
系統(tǒng)高吞吐量下的數(shù)據(jù)庫(kù)重復(fù)寫入問(wèn)題分析解決
這篇文章主要介紹了系統(tǒng)高吞吐量下的數(shù)據(jù)庫(kù)重復(fù)寫入問(wèn)題分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
教您修復(fù)mysql數(shù)據(jù)庫(kù)的方法
你可能在使用MySQL過(guò)程中,各種意外導(dǎo)致數(shù)據(jù)庫(kù)表的損壞,而且這些數(shù)據(jù)往往是最新的數(shù)據(jù),通常不可能在備份數(shù)據(jù)中找到。本章將繼上篇文章中檢查出表的問(wèn)題后,告訴你如何修復(fù)表2014-05-05
淺談MySQL數(shù)據(jù)庫(kù)表鎖了怎么解鎖
在使用 MySQL 數(shù)據(jù)庫(kù)時(shí),有時(shí)候會(huì)發(fā)生某個(gè)表被鎖住的情況,這可能會(huì)導(dǎo)致其他用戶無(wú)法對(duì)該表進(jìn)行讀寫操作,影響系統(tǒng)的正常運(yùn)行,本文主要介紹了淺談MySQL數(shù)據(jù)庫(kù)表鎖了怎么解鎖,感興趣的可以了解一下2023-10-10

