mysql存儲(chǔ)中使用while批量插入數(shù)據(jù)(批量提交和單個(gè)提交的區(qū)別)
批量提交
while 語句寫法:
? ? while '條件' do ? ? ? ? ? ? 循環(huán)體語句; ? ? 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萬條數(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萬條數(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萬條數(shù)據(jù)用了30秒,單個(gè)提交1萬條數(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í)處理索引、分頁以及數(shù)據(jù)類型時(shí)可用到的地方,需要的朋友可以參考下2015-05-05
MySQL Community Server壓縮包安裝配置方法
這篇文章主要為大家詳細(xì)介紹了MySQL Community Server壓縮包安裝配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問題分析
這篇文章主要介紹了解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問題分析,本文通過圖文并茂的形式給大家介紹的非常詳細(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ù)庫重復(fù)寫入問題分析解決
這篇文章主要介紹了系統(tǒng)高吞吐量下的數(shù)據(jù)庫重復(fù)寫入問題分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04

