有关MySQL索引的一点补充
时间:2021-07-01 10:21:17
帮助过:27人阅读
like ‘%xx‘
select * from tb1
where name
like ‘%cn‘;
--未使用索引
select * from tb1
where name
like ‘cn
%‘; --使用索引
- 使用函数
--未使用索引
select * from tb1 where reverse(name) = ‘Hwan
‘;
--使用索引
select * from tb1 where name = reverse(‘Hwan
‘);
- or
select * from tb1 where nid = 1 or email = ‘seven
@live.com
‘;
特别的:当or条件中有未建立索引的列才失效,以下会走索引
select * from tb1 where nid = 1 or name = ‘seven
‘;
select * from tb1 where nid = 1 or email = ‘seven
@live.com
‘ and name = ‘alex
‘
- 类型不一致
如果列是字符串类型,传入条件是必须用引号引起来,不然...
select * from tb1 where name = 999;
- !=
select * from tb1 where name != ‘alex
‘ --all
特别的:如果是主键,则还是会走索引
select * from tb1 where nid != 123 --range
- >
select * from tb1 where name > ‘alex
‘
特别的:如果是主键或索引是整数类型,则还是会走索引
select * from tb1 where nid > 123
select * from tb1 where num > 123
- order by
select email from tb1 order by name desc;
当根据索引排序时候,选择的映射如果不是索引,则不走索引
特别的:如果对主键排序,则还是走索引:
select * from tb1 order by nid desc;
- 组合索引最左前缀
如果组合索引为:(name,email)
name and email -- 使用索引
name -- 使用索引
email -- 不使用索引
有关MySQL索引的一点补充
标签:rom 排序 .com 失效 映射 range sel lex 主键