資料庫壓力測試--MySQL資料庫快速插入大量資料
阿新 • • 發佈:2019-02-08
一、前言
應本次資料庫壓力測試的要求,測試資料記錄至少為千萬級,所以,問題來了,如何快速插入大量資料.
二、解決方法一
第一種解決方法是在MySQL終端中實現的,具體方法如下.
a.開啟MySQl資料庫;
mysql -u root -p
b.建立資料庫;
mysql> create database insertData;
use insertData;
create CREATE TABLE InsertTable (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;(PS:本資料庫為MySQl的記憶體資料庫)
c.建立插入資料的“函式”;
mysql> delimiter @//可實現多行命令的輸入,作用很大
create procedure insert_InsertTable(in item integer)//類似於函式引數列表
begin
declare counter int;
set counter = item;
while counter >= 1 do
insert into myisam values(counter,concat ('Record.',counter));
set counter = counter - 1;
end while;
end
@
d.呼叫該函式(比如插入10 000 000條測試資料);
delimiter @//不可省
call insert_InsertTable(100);
@
e.然後使用命令檢視是否成功插入資料;
select count(*) from InserTable;
三、解決方法二
通過使用可知,上文中的方法一顯然太過於麻煩,現在考慮將上文中的InsertTable函式寫到一個.sql檔案,直接呼叫命令就能實現將資料插入MySQL資料庫.將資料庫的建立、資料表的建立、資料的插入一步實現
具體實現如下:
a.建立.sql檔案insert_data.sql;
drop database if exists insertData;
create database insertData;
use insertData;
SET max_heap_table_size = 1024*1024*2000;//必要的配置,為MySQL分配更大的記憶體,便於儲存
CREATE TABLE InsertTable (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
delimiter @
create procedure insert_InsertTable(in item integer)
begin
declare counter int;
set counter = item;
while counter >= 1 do
insert into InsertTable values(counter,concat('Record.',counter));
set counter = counter - 1;
end while;
end
@
delimiter @
call insert_InsertTable(10000000);
@
b.呼叫.sql檔案;
mysql -u root -p <insert_data.sql
c.查資料是否插入;
select count(*) from InsertTable;
四、其他問題
4.1imiter @ @的作用
可以實現在終端執行多次命令,避免回車的影響.
4.2檢視儲存資料量命令
select count(*) from InserTable;
4.3刪除資料表中的資料命令
truncate TableName;
或者
delete from TableName;
五、待優化問題
5.1如何插入隨機數
5.2如何插入多種資料型別的資料
5.3如何插入多條欄位的資料