您好,登錄后才能下訂單哦!
本篇內容介紹了“mysql存儲中怎么使用while批量插入數據”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
while 語句寫法:
while '條件' do 循環體語句; 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 ;
查看、刪除存儲過程:
mysql> show procedure status like 'test_insert'; mysql> show create procedure test_insert\G; mysql> drop procedure if exists test_insert;
創建表
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萬條數據
mysql> call test_insert(1000000); Query OK, 0 rows affected (31.86 sec)
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萬條數據
mysql> call test_insert(10000); Query OK, 1 row affected (1 min 8.52 sec)
打開另一個窗口查看
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)
“mysql存儲中怎么使用while批量插入數據”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。