4 ms·
You can cluster a table by an index: https://www.postgresql.org/docs/9.6/static/sql-cluster.html https://www.postgresql.org/docs/9.6/static/sql-cluster.html Or
by Boxxed 9y ago
You can cluster a table by an index: https://www.postgresql.org/docs/9.6/static/sql-cluster.html https://www.postgresql.org/docs/9.6/static/sql-cluster.html
Or am I not understanding what you're asking for?
- grzm 9y agoIndexes in PostgreSQL require lookups into the table to access the data values in the rows. Index organized tables have indexes which include the data values in the index itself, removing the need for the lookup in the table itself. Here's some more detail: https://news.ycombinator.com/item?id=10451095 https://news.ycombinator.com/item?id=10451095
- bjt 9y agoPerhaps I'm missing something, but your description doesn't seem consistent with my understanding of the index-only scan feature that's been in PG since 9.2 https://wiki.postgresql.org/wiki/Index-only_scans https://wiki.postgresql.org/wiki/Index-only_scans
- grzm 9y agoYes, you're right about index-only scans, in the sense that the data in the indexed columns can be used in some cases. As I understand it, indexed organized tables goes further than that in that row data for non-indexed columns is also included.
- deleted 9y ago[deleted]
- dhd415 9y agoThat kind of clustering is a point-in-time operation. Subsequent inserts and updates aren't stored in clustered order. When the ordering is guaranteed, the query planner can be more aggressive in optimizing certain kinds of queries. A real clustered index could also be used as an implicit covering index as grzm mentions.
- jeltz 9y agoPostgreSQL can do all of the same query optimizations. The main difference is that for primary key scans PostgreSQL will have an additional layer of indirection meaning more work needs to be done when scanning on primary key and potentially more disk seeks. On the flip side PostgreSQL's approach is good when you query secondary indexes since these can point directly to the heap rather than to a primary key, removing the cost of long primary keys and allowing for scanning the heap in physical order after some kinds of secondary key lookups. It is also cheaper to sequentially scan heap compared to sequentially scanning a clustered index. There are also some difference s on the write side too, but the gist of it is that both models have their own strengths and weaknesses.
- dhd415 9y agoI don't see how pgsql could perform such optimizations as eliminating a sorting step when ordering results by column(s) in a clustered index if there is no guarantee that new or updated data will also be ordered by the clustered index. I agree that both models have their pros and cons. While I am a big fan of pgsql, I do also appreciate those databases that offer both heaps and clustered indexes that I can mix and match in my schema as best suits my workload.
- jeltz 9y agoPostgreSQL can use that optimization since an index scan in PostgreSQL still returns all tuples from the heap in index order, not in heap order. During an index scan PostgreSQL traverses the B-tree in index order and for each matching tuple found in the index it fetches the data from the heap (which is a quite cheap O(1) operation).
- riku_iki 9y agoAlso this operation imposes full read/write lock on table.