SQL优化的一般步骤

  1. 准备基础数据和服务器配置
    本地MySQL配置信息:我使用的是5.5.48,一个比较旧的版本了.


    SQL优化的一般步骤_第1张图片
    2017-08-01 10-39-12屏幕截图.png
create table a (id int auto_increment,seller_id bigint,seller_name  varchar(100)  collate utf8_bin ,gmt_create varchar(30),primary key(id));  
insert into a (seller_id,seller_name,gmt_create) values (100000,'uniqla','2017-01-01');
insert into a (seller_id,seller_name,gmt_create) values (100001,'uniqlb','2017-02-01');
insert into a (seller_id,seller_name,gmt_create) values (100002,'uniqlc','2017-03-01');
insert into a (seller_id,seller_name,gmt_create) values (100003,'uniqld','2017-04-01');
insert into a (seller_id,seller_name,gmt_create) values (100004,'uniqle','2017-05-01');
insert into a (seller_id,seller_name,gmt_create) values (100005,'uniqlf','2017-06-01');
insert into a (seller_id,seller_name,gmt_create) values (100006,'uniqlg','2017-07-01');
insert into a (seller_id,seller_name,gmt_create) values (100007,'uniqlh','2017-08-01');
insert into a (seller_id,seller_name,gmt_create) values (100008,'uniqli','2017-09-01');
insert into a (seller_id,seller_name,gmt_create) values (100009,'uniqlj','2017-10-01');
insert into a (seller_id,seller_name,gmt_create) values (100010,'uniqlk','2017-11-01');
insert into a (seller_id,seller_name,gmt_create) values (100011,'uniqll','2017-12-01');
insert into a (seller_id,seller_name,gmt_create) values (100012,'uniqlm','2018-01-01');
insert into a (seller_id,seller_name,gmt_create) values (100013,'uniqln','2018-02-01');
insert into a (seller_id,seller_name,gmt_create) values (100014,'uniqlo','2018-03-01');
insert into a (seller_id,seller_name,gmt_create) values (100015,'uniqlp','2018-04-01');
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
insert into a (seller_id,seller_name,gmt_create) select  seller_id,seller_name,gmt_create  from a;
 
insert into a (seller_id,seller_name,gmt_create) values (100016,'uniqlq',now());    
    




create table b (id int auto_increment,seller_name varchar(100),user_id varchar(50),user_name  varchar(100),sales bigint,gmt_create varchar(30),primary key(id)); 
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqla','1','a',1,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlb','2','b',3,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlc','3','c',1,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqld','4','d',4,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqle','5','e',5,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlf','6','f',1,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlg','7','g',7,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlh','8','h',1,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqli','9','i',1,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlj','10','j',15,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlk','11','k',61,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqll','12','l',31,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlm','13','m',134,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqln','14','n',1455,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlo','15','o',166,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('niqlp','16','p',15,now());
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
insert into b (seller_name,user_id,user_name,sales,gmt_create) select     seller_name,user_id,user_name,sales,gmt_create   from  b;
 
insert into b (seller_name,user_id,user_name,sales,gmt_create) values ('uniqlq','17','s',109,now());

 



create table c (id int auto_increment,user_id varchar(50),order_id  varchar(100),state bigint,gmt_create varchar(30),primary key(id));
insert into c (user_id,order_id,state,gmt_create)  values( 21,1,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 22,2,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 33,3,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 43,4,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 54,5,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 65,6,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 75,7,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 85,8,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 95,8,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 100,8,0 ,now() );
insert into c (user_id,order_id,state,gmt_create)  values( 150,8,0 ,now() );
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;
insert into c   (user_id,order_id,state,gmt_create) select user_id,order_id,state,gmt_create  from  c;

insert into c (user_id,order_id,state,gmt_create)  values( 17,8,0 ,now() );

待优化SQL:

select a.seller_id,a.seller_name,b.user_name,c.state   from  a,b,c
where  a.seller_name=b.seller_name  and    b.user_id=c.user_id   and  c.user_id=17  and
a.gmt_create BETWEEN DATE_ADD(NOW(), INTERVAL - 600 MINUTE) AND  DATE_ADD(NOW(), INTERVAL 600 MINUTE)  order  by  a.gmt_create;
  1. 优化工具-explain
    待优化SQL的初次分析:
mysql> explain select a.seller_id,a.seller_name,b.user_name,c.state   from  a,b,c where  a.seller_name=b.seller_name  and    b.user_id=c.user_id   and  c.user_id=17  and a.gmt_create BETWEEN DATE_ADD(NOW(), INTERVAL - 600 MINUTE) AND  DATE_ADD(NOW(), INTERVAL 600 MINUTE)  order  by  a.gmt_create;
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra                                        |
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
|  1 | SIMPLE      | b     | ALL  | NULL          | NULL | NULL    | NULL |  15609 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | a     | ALL  | NULL          | NULL | NULL    | NULL |  16674 | Using where; Using join buffer               |
|  1 | SIMPLE      | c     | ALL  | NULL          | NULL | NULL    | NULL | 360681 | Using where; Using join buffer               |
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
3 rows in set (0.00 sec)

从Extra中看出主要有Using temporary,Using filesort,Using join buffer三个问题.

Using filesort
说明使用了一次额外排序,从排序的地方下手,就找到了a.gmt_create.

Using join buffer
说明mysql使用using join buffer算法来优化改sql的查询.根据SQL语句,找到where地方,增加索引.

综上分析,增加下列索引:

alter table a add index idx_seller_gmt(`gmt_create`,`seller_name`);
alter table b add index idx_seller(`seller_name`,`user_id`);
alter table c add index idx_user(`user_id`);

若想查看详细的SQL执行过程,可以按照以下步骤执行.

  • 开启
    SET profiling=1;  
  • 执行SQL
  • 查看所有的SQL执行
SHOW PROFILES;
  • 查看某个SQL的情况
> SHOW PROFILE ALL FOR QUERY 1 ;

增加索引后的SQL分析,还算不错.如果有更好的优化办法,请和我交流.

mysql> explain select a.seller_id,a.seller_name,b.user_name,c.state   from  a,b,c where  a.seller_name=b.seller_name  and    b.user_id=c.user_id   and  c.user_id=17  and a.gmt_create BETWEEN DATE_ADD(NOW(), INTERVAL - 600 MINUTE) AND  DATE_ADD(NOW(), INTERVAL 600 MINUTE)  order  by  a.gmt_create;
+----+-------------+-------+-------+----------------+----------------+---------+---------------+------+-------------+
| id | select_type | table | type  | possible_keys  | key            | key_len | ref           | rows | Extra       |
+----+-------------+-------+-------+----------------+----------------+---------+---------------+------+-------------+
|  1 | SIMPLE      | a     | range | idx_seller_gmt | idx_seller_gmt | 123     | NULL          |    1 | Using where |
|  1 | SIMPLE      | b     | ref   | idx_seller     | idx_seller     | 403     | func          |   82 | Using where |
|  1 | SIMPLE      | c     | ref   | idx_user       | idx_user       | 203     | ali.b.user_id | 1803 | Using where |
+----+-------------+-------+-------+----------------+----------------+---------+---------------+------+-------------+
3 rows in set (0.02 sec)
  1. SQL优化步骤总结
  • 找出慢SQL
  • explain分析SQL
  • 调整SQL语句或者调整表结构
  1. 参考文章
    MySQL索引原理及其慢查询优化
    explain字段和用法

你可能感兴趣的:(SQL优化的一般步骤)