简体   繁体   English

postgres:使用索引时

[英]postgres: when index is used

just to better understand in which cases index is correctly used i would like to enumerate possible cases. 为了更好地了解在哪种情况下正确使用了索引,我想列举一些可能的情况。

let's assume we have a table with "a", "b", "c", "d" columns. 假设我们有一个包含“ a”,“ b”,“ c”,“ d”列的表。

we create an index on (a, b, c, d): 我们在(a,b,c,d)上创建索引:

create index my_index on table my_table (a, b, c, d)

is it used when: 是否在以下情况下使用:

1) 1)

where a=% and b=% and c=% and d=%

2) 2)

where a=% and b=%

3) 3)

where a=%

4) 4)

where b=% and c=% and d=%

5) 5)

where c=% order by b

6) 6)

where a=% and b=% and c=% order by case when d is not null then d else c end

7) let's assume now we have more column for the 7 point but the index only on (a, b, c, d) 7)假设现在我们有7点的更多列,但索引仅在(a,b,c,d)上

where a=% and b=% and c=% and d=% and e=% and f=% and g=%

MySQL has pretty good documentation explaining multi-column indexes. MySQL有很好的文档说明了多列索引。 The rules are basically the same across databases (although there is some more advanced stuff). 规则在数据库之间基本相同(尽管有一些更高级的东西)。

Of course, there are other factors -- such as what is going on in the from clause, what columns are being selected, statistics about the tables, and so on. 当然,还有其他因素-例如from子句中发生的情况,选择了哪些列,有关表的统计信息等等。

The following is basic guidance: 以下是基本指导:

1) where a=% and b=% and c=% and d=% 1) where a=% and b=% and c=% and d=%

Yes, as long as all the conditions are equality. 是的,只要所有条件都是平等的。 I don't know what happens with collation conflicts. 我不知道排序规则冲突会怎样。

2) where a=% and b=% 2)其中a =%,b =%

Yes, as long as all the conditions are equality. 是的,只要所有条件都是平等的。 I don't know what happens with collation conflicts. 我不知道排序规则冲突会怎样。

3) where a=% 3)其中a =%

Yes, as long as all the conditions are equality. 是的,只要所有条件都是平等的。 I don't know what happens with collation conflicts. 我不知道排序规则冲突会怎样。

4) where b=% and c=% and d=% 4)其中b =%,c =%和d =%

Probably not. 可能不是。 If used, the index would need to be scanned. 如果使用索引,则需要对其进行扫描。 This would be the case if the index covered the query. 如果索引覆盖了查询,就会是这种情况。

5) where c=% order by b 5)其中c = b的%阶

Probably not. 可能不是。

6) where a=% and b=% and c=% order by case when d is not null then d else c end 6)其中,当d不为null时,a =%且b =%且c =%依次排序,则d else c结束

Should be used for the where clause. 应该用于where子句。 A sort will still be needed. 仍然需要某种排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM