mysql倒排的优化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql倒排的优化相关的知识,希望对你有一定的参考价值。

    今天数据库负载就直线上升,数据库连接数撑爆。把语句抓出来一看,罪魁祸首是一条很简单的语句:SELECT * FROM eload_promotion_code WHERE 1 AND exp_time<1478782591 AND cishu=0 order by id desc limit 454660,20; 二话不说先把这个语句kill了,然后慢慢看怎么优化。


先看一下这个表的索引:

>show index from eload_promotion_code\G

*************************** 1. row ***************************

        Table: eload_promotion_code

   Non_unique: 0

     Key_name: PRIMARY

 Seq_in_index: 1

  Column_name: id

    Collation: A

  Cardinality: 921642

     Sub_part: NULL

       Packed: NULL

         Null: 

   Index_type: BTREE

      Comment: 

Index_comment: 

*************************** 2. row ***************************

        Table: eload_promotion_code

   Non_unique: 1

     Key_name: idx_cishu_exp

 Seq_in_index: 1

  Column_name: cishu

    Collation: A

  Cardinality: 15

     Sub_part: NULL

       Packed: NULL

         Null: 

   Index_type: BTREE

      Comment: 

Index_comment: 

*************************** 3. row ***************************

        Table: eload_promotion_code

   Non_unique: 1

     Key_name: idx_cishu_exp

 Seq_in_index: 2

  Column_name: exp_time

    Collation: A

  Cardinality: 921642

     Sub_part: NULL

       Packed: NULL

         Null: 

   Index_type: BTREE

      Comment: 

Index_comment: 


可以看到id为主键,idx_cishu_exp为(cishu,exp_time)的唯一索引


看一下这个语句的执行计划,可以看到排序没有用到索引

explain SELECT * FROM eload_promotion_code WHERE 1 AND exp_time<1478782591 AND cishu=0 order by id desc limit 454660,20\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: eload_promotion_code

         type: ref

possible_keys: idx_cishu_exp

          key: idx_cishu_exp

      key_len: 4

          ref: const

         rows: 460854

        Extra: Using where; Using filesort

1 row in set (0.00 sec)


将select * 换成select id后再看执行计划,可以用索引覆盖

>explain select id FROM eload_promotion_code WHERE 1 AND exp_time<1478782591 AND cishu=0 order by id desc limit 454660,20 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: eload_promotion_code

         type: range

possible_keys: idx_cishu_exp

          key: idx_cishu_exp

      key_len: 8

          ref: NULL

         rows: 460862

        Extra: Using where; Using index; Using filesort

1 row in set (0.00 sec)


好吧,这个语句有救了,采用延时关联先取出id,然后根据id获取原表所需要的行,改写后的语句来了:select * from eload_promotion_code inner join (select id from eload_promotion_code where exp_time<1478782591 AND cishu=0 order by id desc limit 454660,20) as x on eload_promotion_code.id=x.id;

执行一下,0.3s出结果。

这样就算完了。


本文出自 “一直在路上” 博客,请务必保留此出处http://chenql.blog.51cto.com/8732050/1871575

以上是关于mysql倒排的优化的主要内容,如果未能解决你的问题,请参考以下文章

ES倒排索引中的优化原理 inverted index

广告倒排服务极致优化

正排倒排,并不是 MySQL 的排序的全部!

druid相关的时间序列数据库——也用到了倒排相关的优化技术

大数据技术之_05_Hadoop学习_04_MapReduce_Hadoop企业优化(重中之重)+HDFS小文件优化方法+MapReduce扩展案例+倒排索引案例(多job串联)+TopN案例+找博客

InfluxDB 内存消耗分析及性能优化