mysql 联合索引优化

 explain select Fid from t_cyou_view_records where Fanchormasterid = 20237 and Froleid = 15081 and Fouttime is null order by Fentertime desc limit 1;
+----+-------------+---------------------+------+---------------+------+---------+------+---------+-----------------------------+
| id | select_type | table               | type | possible_keys | key  | key_len | ref  | rows    | Extra                       |
+----+-------------+---------------------+------+---------------+------+---------+------+---------+-----------------------------+
|  1 | SIMPLE      | t_cyou_view_records | ALL  | NULL          | NULL | NULL    | NULL | 1709800 | Using where; Using filesort |
+----+-------------+---------------------+------+---------------+------+---------+------+---------+-----------------------------+

看了where条件中两个字段的基数,建立联合索引会有指数级提升
show index from t_cyou_view_records
    -> ;
+---------------------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+--
| Table               | Non_unique | Key_name  | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | C
+---------------------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+--
| t_cyou_view_records |          0 | PRIMARY   |            1 | Fid             | A         |     1710010 |     NULL | NULL   |      | BTREE      |  
| t_cyou_view_records |          1 | idx_tcvr  |            1 | Fanchormasterid | A         |         132 |     NULL | NULL   | YES  | BTREE      |  
| t_cyou_view_records |          1 | idx_tcvrf |            1 | Froleid         | A         |       58965 |     NULL | NULL   | YES  | BTREE      |  
+---------------------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+--
alter table t_cyou_view_records add index idx_tcvrf(Froleid,Fanchormasterid);
Query OK, 0 rows affected (7.79 sec)
Records: 0  Duplicates: 0  Warnings: 0

explain select Fid from t_cyou_view_records where Fanchormasterid = 20237 and Froleid = 15081 and Fouttime is null order by Fentertime desc limit 1;
+----+-------------+---------------------+------+---------------+-----------+---------+-------------+------+-----------------------------+
| id | select_type | table               | type | possible_keys | key       | key_len | ref         | rows | Extra                       |
+----+-------------+---------------------+------+---------------+-----------+---------+-------------+------+-----------------------------+
|  1 | SIMPLE      | t_cyou_view_records | ref  | idx_tcvrf     | idx_tcvrf | 18      | const,const |    3 | Using where; Using filesort |
+----+-------------+---------------------+------+---------------+-----------+---------+-------------+------+-----------------------------+

扫描行数降低为3

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26390465/viewspace-2113000/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26390465/viewspace-2113000/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值