官术网_书友最值得收藏!

  • Mastering PostgreSQL 9.6
  • Hans Jurgen Schonig
  • 274字
  • 2021-07-09 19:57:16

Clustering tables

In PostgreSQL, there is a command called CLUSTER that allows us to rewrite a table in a desired order. It is possible to point to an index and store data in the same order as the index:

test=# \h CLUSTER 
Command: CLUSTER
Description: cluster a table according to an index
Syntax:
CLUSTER [VERBOSE] table_name [ USING index_name ]
CLUSTER [VERBOSE]

The CLUSTER command has been around for many years and serves its purpose well. But, there are some things to consider before blindly running it on a production system:

  • The CLUSTER command will lock the table while it is running. You cannot insert or modify data while CLUSTER is running. This might not be acceptable on a production system.
  • Data can only be organized according to one index. You cannot order a table by postal code, name, ID, birthday, and so on, at the same time. It means that CLUSTER will make sense if there is a search criteria, which is used most of the time.
  • Keep in mind that the example outlined in this book is more of a worst case scenario. In reality, the performance difference between a clustered and a non-clustered table will depend on the workload, the amount of data retrieved, cache hit rates, and a lot more.
  • The clustered state of a table will not be maintained as changes are made to a table during normal operations. Correlation might deteriorate as time goes by.

Here is an example of how to run the CLUSTER command:

test=# CLUSTER t_random USING idx_random;
CLUSTER

Depending on the size of the table, the time needed to cluster will vary.

主站蜘蛛池模板: 邛崃市| 晋中市| 上饶县| 上高县| 都安| 汉川市| 清河县| 万年县| 炉霍县| 抚远县| 嘉禾县| 格尔木市| 攀枝花市| 县级市| 招远市| 内丘县| 将乐县| 卓尼县| 化州市| 墨竹工卡县| 南召县| 镇坪县| 增城市| 铜川市| 平顶山市| 松江区| 庐江县| 萝北县| 德钦县| 措勤县| 牟定县| 定西市| 丰原市| 建水县| 确山县| 芮城县| 新龙县| 乐平市| 安平县| 嵩明县| 如东县|