只是为了更好地了解在哪些情况下索引正确使用我想列举可能的情况。我们假设我们有一个“a”,“b”,“c”,“d”列的表格。postgres:当使用索引
我们创建(A,B,C,d)的索引:
create index my_index on table my_table (a, b, c, d)
是当用于
1)
where a=% and b=% and c=% and d=%
2)
where a=% and b=%
3)
where a=%
4)
where b=% and c=% and d=%
5)
where c=% order by b
6)
where a=% and b=% and c=% order by case when d is not null then d else c end
7) 假设现在我们有7个点,但该指数更列仅限于(a,b,c,d)
where a=% and b=% and c=% and d=% and e=% and f=% and g=%
它也取决于值的分布,如果只有2个可能的值,并且有成千上万或数百万记录,其中a =%可能不会导致索引的使用。 – aschoerk
@aschoerk。 。 。好点子。我澄清了答案。 –
对不起,我添加了一个7)案例编辑 – 91DarioDev