ITPub博客

首页 > 数据库 > Oracle > Oracle,MySQL selectivity计算公式

Oracle,MySQL selectivity计算公式

原创 Oracle 作者:dingjun123 时间:2026-05-16 22:54:24 0 删除 编辑

Oracle 选择率估算规则总结

有统计信息的情况

1. 等值条件

  • 索引列等值条件 :

    • ix_sel = 1/num_distinct 或 density

    • ix_sel_with_filters = 0.01

2. 范围条件(包括 LIKE)

  • 索引列范围条件 :

    • ix_sel = 0.009

    • ix_sel_with_filters = 0.05

    • 注意 :可能导致回表后的cardinality估算行数大于索引扫描行数

3. 绑定变量范围条件

  • 无peeking情况 :

    • sel = 0.009

    • ix_sel = 0.009

    • ix_sel_with_filters = 0.05

4. 多个AND条件

  • 索引列多个范围AND条件 :

    • ix_sel = 0.009/2 = 0.0045

    • ix_sel_with_filters = (sel_1)* (sel_2) = 0.05* 0.05

5. OR条件

  • 索引列OR条件 :

    • 等值条件:1/num_distinct

    • 范围条件:0.009

    • 回表 ix_sel_with_filters = (sel_1)+(sel_2)-(sel_1*sel_2)

6. 函数条件

  • 函数等值条件 : sel = 0.01

  • 函数范围条件 : sel = 0.05

7. 非索引列

  • 非索引列范围条件 : sel = 0.05

8. 回表选择率

  • sel = ix_sel_with_filters * condition_filters

  • 无额外条件:= ix_sel_with_filters

  • 回表非函数等值条件:max(1/num_distinct, density) * adjust_sel

  • 回表函数等值条件:0.01

  • 其他:0.05

无统计信息的情况

1. 等值条件

  • 有索引的等值条件 :

    • ix_sel = 0.004

    • ix_sel_with_filters = 0.01

  • 无索引的等值条件 : sel = 0.01
    (包括绑定变量无具体值、函数条件的等值查询)

2. 范围条件

  • 非索引列范围查询 : sel = 0.05

  • 索引列范围条件 :

    • ix_sel = 0.009

    • ix_sel_with_filters = 0.05

3. 不等条件

  • 列无统计信息的<>条件 : sel = 5%

Oracle vs MySQL 默认选择率对比

场景 Oracle 默认选择率 MySQL 默认选择率
等值条件 很小(0.004, 0.009等) 较大(10%)
范围条件(>,<) 0.05(非索引) 33%
LIKE条件 0.05 11%
BETWEEN条件 0.05 11%
不等条件(<>) 5% 0.9

关键差异

  1. Oracle偏向索引 :

    • 有索引的等值条件选择率很小(0.004, 0.009)

    • 鼓励使用索引访问路径

  2. MySQL默认选择率较大 :

    • 可能导致全表扫描

    • 需要建索引或收集直方图来改善

    • 缺少动态采样、扩展统计信息等高级功能

  3. 绑定变量影响 :

    • Oracle在没有peeking时使用固定选择率

    • 可能导致执行计划不稳定

  4. 多条件组合 :

    • AND条件:选择率相乘(独立假设)

    • OR条件:选择率相加再减去乘积


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7655508/viewspace-3146066/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-05-07

  • 博文量
    45
  • 访问量
    90562