欢迎投稿

今日深度:

数据库调优教程(二)慢查询数据准备,数据库

数据库调优教程(二)慢查询数据准备,数据库调优


一、           发现慢查询


上一讲我们谈论了慢查询的定义,这一讲我们来创建一张大表,为慢查询做数据准备。

2.      慢查询数据准备


要想发现慢查询,首先要使慢查询发生。在一张普通数量级的表格中是不能发生慢查询的,除非你对于慢查询的定义时一个毫秒。因此我们必须手动创建一张大数量级的表,这里选择创建一张40万数量级的表(同学们也可以创建百万级的,如果你们的电脑很厉害。但是一般情况下,十万级的数据就可以看出慢查询了)。

1)    创建数据库

[plain] view plaincopy
  1. Create database bigTable default character set GBK;  

2)    创建表

#部门表#


[plain] view plaincopy
  1. CREATE TABLE dept(  
  2. id int unsigned primary key auto_increment,  
  3. deptno MEDIUMINT   UNSIGNED  NOT NULL  DEFAULT 0,   
  4. dname VARCHAR(20)  NOT NULL  DEFAULT "",  
  5. loc VARCHAR(13) NOT NULL DEFAULT ""  
  6. ) ENGINE=INNODB DEFAULT CHARSET=GBK ;  


#雇员表#

[plain] view plaincopy
  1. CREATE TABLE emp  
  2. (  
  3. id int unsigned primary key auto_increment,  
  4. empno  MEDIUMINT UNSIGNED  NOT NULL  DEFAULT 0, /*编号*/  
  5. ename VARCHAR(20) NOT NULL DEFAULT "", /*名字*/  
  6. job VARCHAR(9) NOT NULL DEFAULT "",/*工作*/  
  7. mgr MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,/*上级编号*/  
  8. hiredate DATE NOT NULL,/*入职时间*/  
  9. sal DECIMAL(7,2)  NOT NULL,/*薪水*/  
  10. comm DECIMAL(7,2) NOT NULL,/*红利*/  
  11. deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0 /*部门编号*/  
  12. )ENGINE=INNODB DEFAULT CHARSET=GBK ;  


3)    创建函数

函数用于随机产生数据,保证每条数据都不同

#函数1 创建#

#创建函数. 用于随机产生字符串。该函数接收一个整数


[plain] view plaincopy
  1. delimiter $$#定义一个新的命令结束符合  
  2. create function rand_string(n INT)   
  3. returns varchar(255) #该函数会返回一个字符串  
  4. begin   
  5. #chars_str定义一个变量 chars_str,类型是 varchar(100),默认值'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';  
  6.  declare chars_str varchar(100) default  
  7.    'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';  
  8.  declare return_str varchar(255) default '';  
  9.  declare i int default 0;  
  10.  while i < n do   
  11.    set return_str =concat(return_str,substring(chars_str,floor(1+rand()*52),1));  
  12.    set i = i + 1;  
  13.    end while;  
  14.   return return_str;  
  15.   end $$  


#函数2创建#

#用于随机产生部门编号

[plain] view plaincopy
  1. create function rand_num( )  
  2. returns int(5)  
  3. begin   
  4.  declare i int default 0;  
  5.  set i = floor(10+rand()*500);  
  6. return i;  
  7.   end $$  


4)    创建存储过程

 

#存储过程一#

#该存储过程用于往emp表中插入大量数据

[plain] view plaincopy
  1. create procedure insert_emp(in start int(10),in max_num int(10))  
  2. begin  
  3. declare i int default 0;   
  4. #set autocommit =0 把autocommit设置成0  
  5.  set autocommit = 0;    
  6.  repeat  
  7.  set i = i + 1;  
  8.  insert into emp (empno, ename ,job ,mgr ,hiredate ,sal ,comm ,deptno ) values ((start+i) ,rand_string(6),'SALESMAN',0001,curdate(),2000,400,rand_num());  
  9.   until i = max_num  
  10.  end repeat;  
  11.    commit;  
  12.  end $$  


执行存储过程,往emp表添加40万条数据

[plain] view plaincopy
  1. call insert_emp(100001,400000);  

查询,发现Emp表插入了40万条记录



#存储过程二#

#往dept表添加随机数据

[plain] view plaincopy
  1. create procedure insert_dept(in start int(10),in max_num int(10))  
  2. begin  
  3. declare i int default 0;   
  4.  set autocommit = 0;    
  5.  repeat  
  6.  set i = i + 1;  
  7.  insert into dept (deptno ,dname,loc  ) values ((start+i) ,rand_string(10),rand_string(8));  
  8.   until i = max_num  
  9.  end repeat;  
  10.    commit;  
  11.  end $$  


执行存储过程二

[plain] view plaincopy
  1. delimiter ;  
  2. call insert_dept(100,10);  


至此,数据准备完成。我们创建了大表emp。


www.htsjk.Com true http://www.htsjk.com/shujukunews/7446.html NewsArticle 数据库调优教程(二)慢查询数据准备,数据库调优 一、发现慢查询 上一讲我们谈论了慢查询的定义,这一讲我们来创建一张大表,为慢查询做数据准备。 2.慢查询数据准备 要想发现...
评论暂时关闭