1. 程式人生 > 其它 >MySQL批量插入資料(load data 和儲存過程方式)

MySQL批量插入資料(load data 和儲存過程方式)

技術標籤:MYSQL

文章內容來自於:尚矽谷MySQL技術高階篇

MySQL批量插入資料最簡單的就是迴圈遍歷,呼叫多次INSERT語句不就可以插入多條記錄了嗎!但是這種方法會增加伺服器的負荷,因為,執行每一次SQL,伺服器都要同樣對SQL進行分析、優化等操作。MySQL提供了另一種解決方案,就是使用一條INSERT語句來插入多條記錄。這並不是標準的SQL語法,因此只能在MySQL中使用。

文章目錄

方式一、load data infile命令

首先修改my.ini(linux是/etc/my.cnf)下secure-file-priv為你存放txt的地址:

secure-file-priv="D:/mysql_import_data/"

然後就可以使用命令匯入了

load data infile "D:\mysql_import_data\\檔名.txt" into tablefields terminated by '|' lines terminated by '\n' ;

這裡要注意 \\檔案 這裡,一定是雙斜槓,否則匯入會出錯,如果出現中文亂碼,先檢查資料庫本身編碼問題,其次檢查txt檔案編碼,都為utf8即可。

方式二、通過插入儲存過程方式插入

使用指令碼進行大資料量的批量插入,對特定情況下測試資料集的建立非常有用。
建立資料表

1、建立tb_dept_bigdata(部門表)。

create
table tb_dept_bigdata( id int unsigned primary key auto_increment, deptno mediumint unsigned not null default 0, dname varchar(20) not null default '', loc varchar(13) not null default '' )engine=innodb default charset=utf8;

2、建立tb_emp_bigdata(員工表)。

create table tb_emp_bigdata(
id int unsigned primary key auto_increment,
empno mediumint unsigned not null default 0,/*編號*/
empname varchar(20) not null default '',/*名字*/
job varchar(9) not null default '',/*工作*/
mgr mediumint unsigned not null default 0,/*上級編號*/
hiredate date not null,/*入職時間*/
sal decimal(7,2) not null,/*薪水*/
comm decimal(7,2) not null,/*紅利*/
deptno mediumint unsigned not null default 0 /*部門編號*/
)engine=innodb default charset=utf8;

3、開啟log_bin_trust_function_creators引數。

由於在建立函式時,可能會報:This function has none of DETERMINISTIC.....因此我們需開啟函式建立的信任功能。
通過下面命令檢視是否開啟:

show variables like '%log_bin_trust_function_creators%';

在這裡插入圖片描述
可通過set global log_bin_trust_function_creators=1的形式開啟該功能,也可通過在my.ini(linux中是my.cnf)中永久配置的方式開啟該功能,在[mysqld]下配置log_bin_trust_function_creators=1

3.1 建立函式,保證每條資料都不同

3.1.1 建立隨機生成字串的函式。
delimiter $$
drop function if exists rand_string;   //如果存在函式rand_string,則刪除
create function rand_string(n int) returns varchar(255) //建立函式rand_string,帶一個int引數,返回varchar對應到java就是string了
begin
declare chars_str varchar(52) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //申明字串
declare return_str varchar(255) default '';
declare i int default 0;
while i<n do
set return_str=concat(return_str,substring(chars_str,floor(1+rand()*52),1));//floor向下取整,rand()函式產生[0,1)之間隨機數,*52也就是產生[0,52)之間的隨機數。如果有引數3指定需要擷取的位數,則是從左往右開始擷取也就是從首到尾,而不是從尾到首開始。
set i=i+1;
end while;
return return_str;
end $$
3.1.2 建立隨機生成編號的函式。
delimiter $$
drop function if exists rand_num;
create function rand_num() returns int(5)
begin
declare i int default 0;
set i=floor(100+rand()*100);
return i;
end $$

3.2 建立儲存過程用於批量插入資料

3.2.1 建立往tb_dept_bigdata表中插入資料的儲存過程。
delimiter $$
drop procedure if exists insert_dept;
create procedure insert_dept(in start int(10),in max_num int(10))
begin
declare i int default 0;
set autocommit=0;
repeat
set i=i+1;
insert into tb_dept_bigdata (deptno,dname,loc) values(rand_num(),rand_string(10),rand_string(8));
until i=max_num
end repeat;
commit;
end $$
3.2.2 建立往tb_emp_bigdata表中插入資料的儲存過程。
delimiter $$
drop procedure if exists insert_emp;
create procedure insert_emp(in start int(10),in max_num int(10))
begin
declare i int default 0;
set autocommit=0;
repeat
set i=i+1;
insert into tb_emp_bigdata (empno,empname,job,mgr,hiredate,sal,comm,deptno) values((start+i),rand_string(6),'developer',0001,curdate(),2000,400,rand_num());
until i=max_num
end repeat;
commit;
end $$
3.2.3 具體執行過程批量插入資料
  • 首先執行隨機生成字串的函式。
  • 然後執行隨機生成編號的函式。
  • 使用命令檢視函式是否建立成功。

檢視函式是否建立成功,這裡我這邊linux下檢視的比較混亂,就直接使用陽哥的圖了。

show function status;

在這裡插入圖片描述
檢視儲存過程是否建立成功

show procedure status;

在這裡插入圖片描述
執行儲存過程,插入資料
a.首先執行insert_dept儲存過程。

delimiter ;     //注意中間的空格,這個就是將mysql語句結束改回分號;因為上面建立儲存過程時候改成了$$
call insert_dept(100,100); //呼叫儲存過程插入100條資料
select count(*) from tb_dept_bigdata; //檢視記錄條數

在這裡插入圖片描述
說明:deptno的範圍[100,110),因為deptno的值使用了rand_num()函式。

b.然後執行insert_emp儲存過程。

delimiter ; 
call insert_emp(100,300);
select count(*) from tb_emp_bigdata;

在這裡插入圖片描述
說明:tb_emp_bigdata表中deptno編號的範圍[100,110),使用rand_num()函式。

注:對於部門表的deptno和員工表中deptno的資料都使用了rand_num()函式進行賦值,確保兩邊的值能對應。

4、刪除函式與儲存過程

4.1 刪除函式

drop function rand_num;
drop function rand_string;

4.2 刪除儲存過程

drop procedure insert_dept;
drop procedure insert_emp;

5、總結

  • 注意mysql中函式和儲存過程的寫法。
  • 注意儲存過程的呼叫,call procedurename
  • 注意開啟對函式的信任,log_bin_trust_function_creators引數。