针对多列索引,如何确定哪一列位于第一列?这就要用到我前面提到的索引的选择性。通常根据经验法则:将选择性最高的列放到索引最前列。

由此引入了一个问题,计算选择性。举个栗子:

select * from payment where staff_id = 2 and customer_id = 268   那么是应该创建一个(staff_id, customer_id)索引,还是应该颠倒顺序呢?我们可以通过以下方法来进行计算和比较,然后得出结论。执行以下sql语句:

select count(DISTINCT staff_id)/count(*) as staff_id_selectivity,count(DISTINCT customer_id)/count(*),count(*)  from payment;  结果如下:

staff_id_selectivity:0.0001

customer_id_selectivity:0.0373

count(*):16049

customer_id的选择性更高,因此应将其放到第一列。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2022-01-30
  • 2021-06-24
  • 2021-05-11
  • 2021-11-28
  • 2021-09-04
猜你喜欢
  • 2021-09-27
  • 2022-01-22
  • 2019-09-06
  • 2021-07-02
  • 2021-10-07
  • 2022-02-22
  • 2022-12-23
相关资源
相似解决方案