Fast table indexing for range lookup

你。 提交于 2019-12-10 19:13:58

问题


I have a Postgres table with about 4.5 million rows. The columns are basically just

low BIGINT,
high BIGINT,
data1,
data2, 
...

When you query this table, you have a long integer, and want to find the data corresponding to the range between low and high that includes that value. What would be the best way to index this table for fast lookups?


回答1:


A multi-column index with reversed sort order:

CREATE INDEX tbl_low_high_idx on tbl(low, high DESC);

This way, the index can be scanned forward to where low is high enough, then take all rows until high is too low - all in one scan. That's the main reason why sort order is implemented for indexes to begin with: to combine different sort orders in a multi-column index with different order. Basically, a b-tree index can be traversed in both directions at practically the same speed, so ASC / DESC would hardly be needed for single-column indexes.


You may also be interested in the new range types of PostgreSQL 9.2. Can be indexed with a GiST index like this:

CREATE INDEX tbl_idx ON tbl USING gist (low_high);

Should make a query of this form perform very fast:

SELECT * FROM tbl WHERE my_value <@ low_high;

<@ being the "element is contained by" operator.



来源:https://stackoverflow.com/questions/12189297/fast-table-indexing-for-range-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!