无论什么数据库,索引都是优化的一种途径,借鉴杨老师的这篇文章《技术分享 | 优化案例》,了解学习下。
前言

select * from tab where a=1 and b=2;
场景 1
场景 2
create table x(id int not null auto_increment primary key, a int ,b int,key idx(a,b));insert into x(a,b) values(1,8),(1,6),(1,3),(2,1),(2,2),(2,4),(3,7),(3,9);
mysql> select * from x order by a, b;+----+------+------+| id | a | b |+----+------+------+| 3 | 1 | 3 || 2 | 1 | 6 || 1 | 1 | 8 || 4 | 2 | 1 || 5 | 2 | 2 || 6 | 2 | 4 || 7 | 3 | 7 || 8 | 3 | 9 |+----+------+------+8 rows in set (0.00 sec)
mysql> select * from x where a=2 order by b;+----+------+------+| id | a | b |+----+------+------+| 4 | 2 | 1 || 5 | 2 | 2 || 6 | 2 | 4 |+----+------+------+3 rows in set (0.00 sec)mysql> desc select * from x where a=2 order by b \G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: xpartitions: NULLtype: refpossible_keys: idxkey: idxkey_len: 5ref: constrows: 3filtered: 100.00Extra: Using index1 row in set, 1 warning (0.00 sec)
mysql> select * from x where a>=1 and a<3 order by b;+----+------+------+| id | a | b |+----+------+------+| 4 | 2 | 1 || 5 | 2 | 2 || 3 | 1 | 3 || 6 | 2 | 4 || 2 | 1 | 6 || 1 | 1 | 8 |+----+------+------+6 rows in set (0.00 sec)mysql> desc select * from x where a>=1 and a<3 order by b \G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: xpartitions: NULLtype: rangepossible_keys: idxkey: idxkey_len: 5ref: NULLrows: 6filtered: 100.00Extra: Using where; Using index; Using filesort1 row in set, 1 warning (0.01 sec)

如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",或者直接转发pyq,

文章转载自bisal的个人杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




