[英]Which index to create in Postgres?
我有一张桌子:
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 中应用哪种类型的索引才能最有效?
对于这些查询,您需要以下索引: (rec_id, va_id)
。
你真的是说select *
吗? 如果您能够将select
子句中的列数减少到几列,那么您可能还希望将它们添加到索引中。
例如,如下查询:
select col1, col2 from table1 where rec_id = 180000002 and va_id = 100;
将有利于 index (rec_id, va_id, col1, col2)
:这使得索引覆盖,数据库可以通过仅查看索引来完全执行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.