要在Postgres中创建哪个索引?

问题描述 投票:0回答:1

我有一张桌子:

row_id          attr_id    attr_val
180000001       100        test1
180000001       101        test2
180000001       102        test3
180000001       103        test4
180000001       104        test5
180000002       100        test6
180000002       101        test7
180000002       102        test8
180000002       103        test9
180000002       104        test10

它有超过50亿行,表大小约为1.4 TB。我通常运行以下查询:

select * from table1 where rec_id = 180000002;

select * from table1 where rec_id = 180000002 and va_id = 100;

考虑到空间和用例,我应该在Postgres中使用哪种类型的索引才能最有效?

sql postgresql where-clause query-performance
1个回答
0
投票

对于这些查询,您需要以下索引:(rec_id, va_id)

真的是指select *吗?如果您能够将select子句中的列数减少到仅几列,那么您可能也希望将它们添加到索引中。

例如,查询类似:

select col1, col2 from table1 where rec_id = 180000002 and va_id = 100;

有益的索引(rec_id, va_id, col1, col2):这将使索引为[[covering,这意味着数据库仅通过查看索引就可以完全执行它。

© www.soinside.com 2019 - 2024. All rights reserved.