天天看点

一则优化案例

昨晚收到客服mm电话,一用户反馈数据库响应非常慢,手机收到load异常报警,登上主机后发现大量sql执行非常慢,有的执行时间超过了10s

优化点一:

select * from `sitevipdb`.`game_shares_buy_list` where price>=’2.00′ order by tran_id desc limit 10;

表结构为:

create table `game_shares_buy_list` (

`tran_id` int(10) unsigned not null auto_increment,

`………..’

primary key (`tran_id`),

key `ind_username` (`username`)

) engine=innodb auto_increment=3144200 default charset=utf8;

执行计划:

[email protected] : sitevipdb 09:10:22> explain select * from `sitevipdb`.`game_shares_buy_list` where price>=’2.00′ order by tran_id desc limit 10;

+—-+————-+———————-+——-+—————+———+———+——+——+————-+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |

| 1 | simple | game_shares_buy_list | index | null | primary | 4 | null | 10 | using where |

1 row in set (0.00 sec)

分析该sql的执行计划,由于tran_id是表的主键,所以查询根据主键降序顺序扫描,这样就可以不用排序,

然后在过滤条件price>2.00的记录,看上去这个执行计划貌似非常好,如果查询扫描到了满足条件的10条记录,就会停止扫描;

但是这里有个问题,如果表中有大量的记录是不符合2.00的,意味查询就需要扫描非常多的记录,才能找到符合条件的10条:

[email protected] : sitevipdb 09:17:23> select price,count(*) as cnt from `game_shares_buy_list` group by price order by cnt desc limit 10;

+——-+——-+

| price | cnt |

| 1.75 | 39101 |

| 1.68 | 38477 |

| 1.71 | 34869 |

| 1.66 | 34849 |

| 1.72 | 34718 |

| 1.70 | 33996 |

| 1.76 | 32527 |

| 1.69 | 27189 |

| 1.61 | 25694 |

| 1.25 | 25450 |

可以看到表中有大量的记录不是2.00的,所以这个时候不能在根据主键顺序扫描,在过滤记录;

那么是否需要在price建立一个索引:

[email protected] : sitevipdb 09:09:01> select count(*) from `game_shares_buy_list` where price>’2′;

+———-+

| count(*) |

| 4087 |

[email protected] : sitevipdb 09:17:31> select count(*) from `game_shares_buy_list` ;

| 1572100 |

从上面price的数据分布可以看出,price的分布相对还是比较集中的,如果在price建立索引,mysql也有可能认为由于需要回表的记录过多,

同时需要额外的排序,而不选择在price上的索引:

[email protected] : sitevipdb 09:24:53> alter table game_shares_buy_list add index ind_game_shares_buy_list_price(price);

query ok, 0 rows affected (5.79 sec)

一则优化案例

可以看到优化器虽然注意到了我们新加的索引,但是最终还是选择了primary来扫描;

所以这个时候我们加上去的索引没有产生效果,数据库负载依然很高,如果强制走price上的索引,效果会这样:

[email protected] : sitevipdb 09:35:38> select * from `sitevipdb`.`game_shares_buy_list` where price>=’2.0′ order by tran_id desc limit 10;

。。。。。

10 rows in set (7.06 sec)

[email protected] : sitevipdb 09:36:00> select * from `sitevipdb`.`game_shares_buy_list` force index(ind_game_shares_buy_list_price) where price>=’2.0′ order by tran_id desc limit 10;

。。。。

10 rows in set (1.01 sec)

可以看到如果强制走索引,时间已经明显下降了,但是还是有些慢,能不能在快一点?其实我们需要扫描的记录只有10条,但查询在取得这10条记录的时候需要扫描大量无效的记录

一则优化案例

怎么降低这个数据:其实只要改写一下sql就可以,我们先从索引中得到满足条件的10个id,在回表进行关联:

[email protected] : sitevipdb 09:44:45> select * from game_shares_buy_list t1,

-> ( select tran_id from sitevipdb.game_shares_buy_list where price>=’2.0′ order by tran_id desc limit 10) t2

-> where t1.tran_id=t2.tran_id;

10 rows in set (0.00 sec)

可以看到执行时间已经不在秒级别了,和客户电话沟通后,很愿意这样改写sql。

—这里看到是order by tran_id是要额外排序的,索引也可以这样来建立消除排序(tran_id,price)这样可以消除排序,同时可以利用order by desc/asc +limit m,n的优化。

优化点二:

create table `game_session` (

`session_id` varchar(255) character set utf8 collate utf8_bin not null default ,

`session_expires` int(10) unsigned not null default ‘0’,

`client_ip` varchar(16) default null,

`session_data` text,

…………………….

primary key (`session_id`)

) engine=innodb default charset=utf8;

查询为select `session_data`, `session_expires` from `game_session` where session_id=’xxx’出现大量等待情况

同时该表的insert,也有等待的现象;

可以看到这个表结构设计是有些问题的,咨询了客户后,可以改为下面结构:

id int auto_increment,

`session_id` varchar(30) character set utf8 collate utf8_bin not null default ,

`session_data` varchar(200),

primary key (id),

key ind_session_id(session_id,session_data, session_expires)

-新增自增主键id作为表的主键,这样对插入的性能提升是很好的,同时也降低了表主键的大小;

-将session_data由text改为了varchar(200),咨询了客户后,这个字段可以不用大字段存储,同时有text改为了varchar,就可以冗余到索引中;

由于查询可以使用覆盖索引来完成,所以将查询的3个字段冗余到索引中,查询通过索引完成,不用回表;