SQL优化的一般步骤

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


    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字段和用法
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容