天天看點

mysql進階(六)

1.插入1000w資料

涉及到的表:

create table dept(
	id int unsigned primary key  auto_increment,
	deptno mediumint not null default 0,
	deptname varchar(20) not null default "",
	loc varchar(13) not null default""
	) engine=innodb default charset=gbk;


 create table emp(
	id int unsigned primary key  auto_increment,
	empno mediumint not null default 0,
	ename 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,
	deptno mediumint unsigned not null default 0
	) engine=innodb default charset=gbk;           

 建立函數:當建立函數時報錯:this function has none of deterministic ……

由于開啟過慢查詢日志,因為 bin-log,必須為function指定一個參數。

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

生成随機數函數:

delimiter $$
create function rand_string(n int) returns varchar(255)
begin
declare chars_str varchar(100) 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));
set i = i+1;
end while;
return return_str;
end $$
delimiter ;           

 生成随機數字函數:注意 rand)num( )括号中空格

delimiter $$
create function rand_num( ) returns int(5)
begin
declare i int default 0;
set i = floor(100+rand()*10);
return i;
end $$
delimiter ;           

建立存儲過程:

-- 建立存儲過程
delimiter $$
create procedure insert_emp(in start int(10),in max_num int(10))
begin 
declare i int default 0;
# set autocommit =0 把autocommit設定成0
set autocommit = 0;
repeat
set i = i+1;
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values ((start+i),rand_string(6),'SALEMAN',001,curdate(),2000,400,rand_num());
until i = max_num
end repeat;
commit;
end $$
delimiter ;           
delimiter $$
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 dept(deptno,dname,loc) values((start+i),rand_string(10),rand_string(8));
until i = max_num
end repeat;
commit;
end $$
delimiter ;           

2.show profiles

是mysql提供可以用來分析目前會話中語句執行的資源消耗情況。可以用于SQL的調優的測量。

參數預設關閉,并儲存最近15次的運作結果

①檢視是否支援profile

show variables like '%profiling%';           

②開啟功能,預設是關閉的,使用前需要開啟

select * from emp group by id%10 limit 150000;           

診斷SQL的指令示範 :show profile ,cpu,block io for query query_id;

3.全局查詢日志

配置啟用:

編碼配置: