資料庫調優教程(二)慢查詢資料準備,資料庫調優

來源:互聯網
上載者:User

資料庫調優教程(二)慢查詢資料準備,資料庫調優

一、           發現慢查詢


上一講我們談論了慢查詢的定義,這一講我們來建立一張大表,為慢查詢做資料準備。

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。


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.