[英]Indexing in Postgres with common columns
Suppose I have 4 attributes in my Postgres db a,b,c,d.假设我的 Postgres db a、b、c、d 中有 4 个属性。
I want to create an index on a and b, these are the two options I can go ahead with我想在 a 和 b 上创建索引,这是我可以继续使用的两个选项
Will creating index in the 2nd form optimize the performance of db?以第二种形式创建索引会优化数据库的性能吗? And how does indexing work in that case?在这种情况下,索引如何工作?
An index can only be efficiently used by a query if a prefix of its columns is applied to the query's WHERE
-condition.只有将其列的前缀应用于查询的WHERE
条件时,查询才能有效地使用索引。 The order of the columns in the CREATE INDEX
-statement matters. CREATE INDEX
语句中列的顺序很重要。 Suppose we have a table假设我们有一张桌子
CREATE TABLE my_table(
a INT,
b INT,
c INT,
d INT
);
and indexes和索引
CREATE INDEX idx_a ON my_table (a);
CREATE INDEX idx_b ON my_table (b);
CREATE INDEX idx_ca ON my_table (c, a);
CREATE INDEX idx_cb ON my_table (c, b);
then query然后查询
SELECT * FROM my_table WHERE a = 42;
will only use idx_a
(while all other indexes won't fit to this query),只会使用idx_a
(而所有其他索引都不适合此查询),
SELECT * FROM my_table WHERE b = 42;
will only use idx_b
(while all other indexes won't fit to this query),只会使用idx_b
(而所有其他索引都不适合此查询),
SELECT * FROM my_table WHERE a = 42 and c = 23;
will use idx_ca
( idx_a
would fit, too, but it has probably less performance),将使用idx_ca
( idx_a
也适合,但性能可能较差),
SELECT * FROM my_table WHERE b = 42 and c = 23;
will use idx_cb
( idx_b
would fit, too, but it has probably less performance) and将使用idx_cb
( idx_b
也适合,但它的性能可能较低)和
SELECT * FROM my_table WHERE c = 23;
can eather use idx_ca
or idx_cb
(the optimizer will decide by statistical information).可以使用idx_ca
或idx_cb
(优化器将根据统计信息决定)。
So it depends on your queries.所以这取决于你的查询。 If they always include a condition on c
then idx_ca
will presumably have better query performance in comparison to idx_a
.如果它们总是在c
上包含一个条件,那么idx_ca
与idx_a
相比可能具有更好的查询性能。
Of course you shouldn't create indexes on suspicion that actually aren't needed.当然,您不应该创建实际上不需要的怀疑索引。 In Postgres
you can use EXPLAIN
( docs ) to see how the optimizer makes use of the indexes you have defined.在Postgres
中,您可以使用EXPLAIN
( docs ) 查看优化器如何使用您定义的索引。
As already said, it really depends ot the queries on the table.如前所述,它实际上取决于表上的查询。
Postgres can in certain situation use several index for a query. Postgres 在某些情况下可以使用多个索引进行查询。 See this very good answer: https://dba.stackexchange.com/a/91269/216879看到这个很好的答案: https ://dba.stackexchange.com/a/91269/216879
If it is possible You can create the 4 indexes, and check after some times which of your index are not used by querying the pg_stat_user_indexes
table.如果可能,您可以创建 4 个索引,并在一段时间后通过查询pg_stat_user_indexes
表来检查哪些索引未使用。
More litterature about index utilization from the offical documentation: https://www.postgresql.org/docs/current/indexes-bitmap-scans.html#:~:text=Fortunately%2C%20PostgreSQL%20has%20the%20ability,conditions%20across%20several%20index%20scans .更多关于官方文档中索引利用率的文献: https ://www.postgresql.org/docs/current/indexes-bitmap-scans.html#:~:text=Fortunately%2C%20PostgreSQL%20has%20the%20ability,conditions %20across%20several%20index%20scans 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.