单点查询

select * from table_name where key = ?

IN 查询

select * from table_name where key IN (?,?,?,...)

Ref access

使用索引读取匹配的行记录

  • eq_ref: 使用唯一索引,返回一行
  • ref: 非唯一索引,可能返回多行

Table scan

Range scan

Range 优化

  1. 多个范围查询,mysql只会用到一个索引
  2. 目标: 最小范围,通过key1过滤出来的少还是通过key2过滤出来的少

Range Access

可以使用第二个索引:

select * from t1 where a IN (10,11,13) and (b=2 or b = 4)
这条语句会怎么运行?

select * from t1 where a > 10 and a < 12 and (b=2 or b=4)

不能使用第二 个索引

最左前缀

  1. key (a,b,c)
  2. key 顺序 (选择性低的放前面)

Loose Index scan

参考资料

  1. MySQL Optimizer Overview
  2. mysql-optimizer-cost-model

results matching ""

    No results matching ""