mysql中如何进行联合索引优化

今天就跟大家聊聊有关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

看完上述内容,你们对mysql中如何进行联合索引优化有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

本文名称:mysql中如何进行联合索引优化
分享链接:https://www.cdcxhl.com/article10/ishego.html

成都网站建设公司_创新互联,为您提供面包屑导航定制网站关键词优化用户体验动态网站微信小程序

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营