开发者学堂课程【MySQL 高级应用 - 索引和锁:索引优化9】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/598/detail/8619
索引优化9
目录
一、字符串不加单引号索引失效口
二、少用 or ,用它来连接时会索引失效口
一.字符串不加单引号索引失效口
操作如下:
mysq1> se1ect" from staffs where name=2000;
id | NAME| age| pos| add_ .t ime
3| 2000| 23| dev| 2016-12-09 09:31 :34
1 row in set, 1 warning (0.00 sec)
mysq1> explain se1ect
from staffs where name= '2000 ' ;
id| se1ect. _type
table
Itype| possib1e_ keys
|key
|key_1en| ref
rowsI Extra
| 1 | SIMPLE
staffs | ref| idx_ staffs_ nameAgePos| idx_ staffs_ nameAgePos| 74
constI
1| Using where
row in set (0.00 sec )
mysq1> exp1ain se1ect* from staffs where name=2000;
id | select. _type
table
| type| possib1e_ keys
| key| key_ 1en | ref| rows
Extra
1 | SIMPLE
staffsI ALL| idx_ staffs_ name AgePos
NULL
NULL
INULL
3|
Using where
1 row in set (0.00 sec )
" mysq1>I
(绝对不要乱记单引号,不要在底层数据跑的时候发生了隐式的类型转换,这样会导致全表扫描,性能拉下)
二.少用 or ,用它来连接时会索引失效口
Mysql >explain select . from staffs where name-'Ju1y' or nane= ' z3
select. typeI tableI type
possible keys
key I key_lenI refI rows
Extra SIMPLE
staffsI ALLI idx staffs_ nameAgePos I NULLI NULLI NULLI 51 using wherel
row in set (0.00 sec)
uysq1> select * from staffs where name-'Ju1y' or name='z3' ;
NAME Age pos
add time z3_ manager
2016-02-14 23:01:33
July dev
2016-02-14 23:01 :35
rows in set (0.00 sec)