关于postgres中的聚集索引

发布于 2024-10-14 09:12:35 字数 96 浏览 4 评论 0原文

我正在使用 psql 访问 postgres 数据库。在查看表的元数据时,有什么办法可以看出表的索引是否是聚集索引?

我听说表的主键会自动关联聚集索引,是真的吗?

I'm using psql to access a postgres database. When viewing the metadata of a table, is there any way to see whether an index of a table is a clustered index?

I heard that the PRIMARY KEY of a table is automatically associated with a clustered index, is it true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

染年凉城似染瑾 2024-10-21 09:12:35

请注意,PostgreSQL 使用术语“聚集索引”来使用与 SQL Server 大致相似但又非常不同的内容。

如果某个特定索引已被指定为表的聚簇索引,则 psql 的 \d 命令将指示聚簇索引,例如,

Indexes:
    "timezone_description_pkey" PRIMARY KEY, btree (timezone) CLUSTER

PostgreSQL 默认情况下不指定索引作为聚簇索引。即使如此指定,它也不会自动排列表数据以与聚集索引相关:必须使用 CLUSTER 命令来重新组织表数据。

Note that PostgreSQL uses the term "clustered index" to use something vaguely similar and yet very different to SQL Server.

If a particular index has been nominated as the clustering index for a table, then psql's \d command will indicate the clustered index, e.g.,

Indexes:
    "timezone_description_pkey" PRIMARY KEY, btree (timezone) CLUSTER

PostgreSQL does not nominate indices as clustering indices by default. Nor does it automatically arrange table data to correlate with the clustered index even when so nominated: the CLUSTER command has to be used to reorganise the table data.

恰似旧人归 2024-10-21 09:12:35

在 PostgreSQL 中,聚集属性保存在相应索引的元数据中,而不是关系本身。它是 pg_index 目录中的 indisclustered 属性。但请注意,postgres 中的集群关系是一次性操作:即使该属性为 true,对表的更新也不会保持数据的排序性质。迄今为止,数据集群的自动维护仍然是一个流行的TODO项目。

聚集集成索引之间经常会产生混淆,特别是因为流行的教科书使用了相互冲突的名称,而且 postgres 和 SQL Server 手册中的术语也不同(命名为只有两个)。当我谈论集成索引(也称为主索引主索引)时,我指的是其中关系数据包含在索引的叶子,而不是外部辅助索引,其中叶子包含指向表记录的索引条目。前一种类型必然总是聚集的。不幸的是 postgres 仅支持后一种类型。无论如何,集成(主)索引总是聚集的这一事实可能会引起“表的主键自动与聚集索引相关联”的信念。这两种说法听起来相似,但又有所不同。

In PostgreSQL the clustered attribute is held in the metadata of the corresponding index, rather than the relation itself. It is the indisclustered attribute in pg_index catalogue. Note, however, that clustering relations within postgres is a one-time action: even if the attribute is true, updates to the table do not maintain the sorted nature of the data. To date, automatic maintenance of data clustering remains a popular TODO item.

There is often confusion between clustered and integrated indexes, particularly since the popular textbooks use conflicting names, and the terminology is different again in the manuals of postgres and SQL server (to name just two). When I talk about an integrated index (also called a main index or primary index) I mean one in which the relation data is contained in the leaves of the index, as opposed an external or secondary index in which the leaves contain index entries that point to the table records. The former type is necessarily always clustered. Unfortunately postgres only supports the latter type. Anyhow, the fact that an integrated (primary) index is always clustered may have given rise to the belief that "a PRIMARY KEY of a table is automatically associated with a clustered index". The two statements sound similar, but are different.

半城柳色半声笛 2024-10-21 09:12:35

PostgreSQL 不像 Microsoft SQL Server 那样直接实现 CLUSTER 索引。

参考此博客:

在 PostgreSQL 中,我们有一个类似于 Cluster Index 的 CLUSTER 命令。

创建表主键或任何其他索引后,您可以通过指定索引名称来执行 CLUSTER 命令,以实现表数据的物理顺序。

当表被聚集时,它会根据索引信息进行物理重新排序。集群是一种一次性操作:当表随后更新时,更改不会集群。也就是说,不会尝试根据索引顺序存储新的或更新的行。

集群语法:

第一次必须使用索引名称执行CLUSTER。

CLUSTER table_name USING index_name;

对表进行聚类:

一旦您执行了带有索引的CLUSTER,下次您应该只执行CLUSTER TABLE,因为它知道哪个索引已经定义为CLUSTER。

CLUSTER table_name;

PostgreSQL does not have direct implementation of CLUSTER index like Microsoft SQL Server.

Reference Taken from this Blog:

In PostgreSQL, we have one CLUSTER command which is similar to Cluster Index.

Once you create your table primary key or any other Index, you can execute the CLUSTER command by specifying that Index name to achieve the physical order of the Table Data.

When a table is clustered, it is physically reordered based on the index information. Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered. That is, no attempt is made to store new or updated rows according to their index order.

Syntax of Cluster:

First time you must execute CLUSTER using the Index Name.

CLUSTER table_name USING index_name;

Cluster the table:

Once you have executed CLUSTER with Index, next time you should execute only CLUSTER TABLE because It knows that which index already defined as CLUSTER.

CLUSTER table_name;
温柔一刀 2024-10-21 09:12:35

有什么方法可以查看表的索引是否是聚集索引

PostgreSQL 没有聚集索引,因此您将无法看到它们。

听说表的主键会自动关联聚集索引,是真的吗?

不,这不是真的(见上文)

您可以沿着索引手动聚集表,但这是没有任何内容会自动维护(例如使用 SQL Server 的聚集索引)。

有关更多详细信息,请参阅 CLUSTER 命令的说明手动的。

is there any way to see whether an index of a table is a clustered index

PostgreSQL does not have a clustered index, so you won't be able to see them.

I heard that the PRIMARY KEY of a table is automatically associated with a clustered index, is it true?

No, that's not true (see above)

You can manually cluster a table along an index, but this is nothing that will be maintained automatically (as e.g. with SQL Server's clustered indexes).

For more details, see the description of the CLUSTER command in the manual.

∝单色的世界 2024-10-21 09:12:35

簇索引

簇索引意味着告诉数据库在磁盘上存储实际上彼此接近的接近值。它们可以唯一地标识 SQL 表中的行。每个表只能有一个聚集索引。聚簇索引可以覆盖多个列。默认情况下,具有主键的列已经具有聚集索引。

字典

字典本身就是一个带有聚集索引的表。因为所有数据在物理上都是按字母顺序存储的。


非聚集索引

非聚集索引就像一本书的简单索引。它们仅用于快速检索数据。不确定是否有独特的数据。非聚集索引包含非聚集索引键及其相应的数据位置指针。例如,书籍的内容索引包含主题或章节的关键字及其页面位置。

图书内容索引

图书的内容表保存内容名称及其页面位置。不确定数据是否唯一。因为相同的段落或文本行或单词可以放置多次。


PostgreSQL 索引

PostgreSQL 自动为表的PRIMARY KEY 和每个UNIQUE 约束创建索引。在 PostgreSQL 终端中登录数据库并输入 \d table_name。所有存储的索引都将被可视化。如果存在聚集索引,那么它也会被识别。

创建表

CREATE TABLE IF NOT EXISTS profile(
    uid serial NOT NULL UNIQUE PRIMARY KEY,
    username varchar(30) NOT NULL UNIQUE,
    phone varchar(11) NOT NULL UNIQUE,
    age smallint CHECK(age>12),
    address text NULL
);

3时会自动创建索引。所有这些索引都是非聚集索引

"profile_pkey" PRIMARY KEY, btree (uid)
"profile_phone_key" UNIQUE CONSTRAINT, btree (phone)
"profile_username_key" UNIQUE CONSTRAINT, btree (username)

用uid和username创建我们自己的索引

CREATE INDEX profile_index ON profile(uid, username);

这实际上创建了一个非聚集索引。要使其成为集群,请运行下一部分。

将非聚集索引转换为聚集索引

ALTER TABLE profile CLUSTER ON profile_index;

使用\d profile检查表。它将是这样的:

 表“public.profile”
  专栏 |类型 |整理 |可空 |默认
----------+------------------------+------------+--- -------+----------------------------------------
 uid |整数 | |不为空 | nextval('profile_uid_seq'::regclass)
 用户名 |字符变化(30) | |不为空 |
 电话 |字符变化(11) | |不为空 |
 年龄 |小整数 | | |
 地址 |文字| | |
索引:
    “profile_pkey”主键,btree(uid)
    “profile_phone_key”唯一约束,btree(电话)
    “profile_username_key”唯一约束,btree(用户名)
    “profile_index”btree(uid,用户名)集群
检查约束:
    “profile_age_check”检查(年龄 > 12)

请注意,profile_index 现在是“CLUSTER”

现在,重新对表进行集群,以便该表可以遵循集群索引角色

CLUSTER profile;

Cluster Indexing

A cluster index means telling the database to store the close values actually close to one another on the disk. They can uniquely identify the rows in the SQL table. Every table can have exactly one one clustered index. A cluster index can cover more than one column. By default, a column with a primary key already has a clustered index.

dictionary

A dictionary itself is a table with clustered index. Because all the data is physically stored in alphabetical order.


Non-Cluster Indexing

Non-clustered indexing is like simple indexing of a book. They are just used for fast retrieval of data. Not sure to have unique data. A non-clustered index contains the non-clustered index keys and their corresponding data location pointer. For example, a book's content index contains the key of a topic or chapter and the page location of that.

book content index

A book's content table holds the content name and its page location. It is not sure that the data is unique. Because same paragraph or text line or word can be placed many times.


PostgreSQL Indexing

PostgreSQL automatically creates indexes for PRIMARY KEY and every UNIQUE constraints of a table. Login to a database in PostgreSQL terminal and type \d table_name. All stored indexes will be visualized. If there is a clustered index then it will also be identified.

Creating a table

CREATE TABLE IF NOT EXISTS profile(
    uid serial NOT NULL UNIQUE PRIMARY KEY,
    username varchar(30) NOT NULL UNIQUE,
    phone varchar(11) NOT NULL UNIQUE,
    age smallint CHECK(age>12),
    address text NULL
);

3 index will be created automatically. All these indexes are non clustered

"profile_pkey" PRIMARY KEY, btree (uid)
"profile_phone_key" UNIQUE CONSTRAINT, btree (phone)
"profile_username_key" UNIQUE CONSTRAINT, btree (username)

Create our own index with uid and username

CREATE INDEX profile_index ON profile(uid, username);

This actually creates a non-clustered index. To make it clustered, run the next part.

Transform a non-clustered index into a clustered one

ALTER TABLE profile CLUSTER ON profile_index;

Check the table with \d profile. It will be like this:

                                     Table "public.profile"
  Column  |         Type          | Collation | Nullable |               Default
----------+-----------------------+-----------+----------+--------------------------------------
 uid      | integer               |           | not null | nextval('profile_uid_seq'::regclass)
 username | character varying(30) |           | not null |
 phone    | character varying(11) |           | not null |
 age      | smallint              |           |          |
 address  | text                  |           |          |
Indexes:
    "profile_pkey" PRIMARY KEY, btree (uid)
    "profile_phone_key" UNIQUE CONSTRAINT, btree (phone)
    "profile_username_key" UNIQUE CONSTRAINT, btree (username)
    "profile_index" btree (uid, username) CLUSTER
Check constraints:
    "profile_age_check" CHECK (age > 12)

Notice that the profile_index is now "CLUSTER"

Now, re-cluster the table so that the table can follow the cluster index role

CLUSTER profile;
隔岸观火 2024-10-21 09:12:35

如果您想知道给定的表是否使用 SQL 进行集群化,可以使用以下查询来显示正在使用的索引(在 Postgres 版本 9.5 和 9.6 中测试):

SELECT
  i.relname AS index_for_cluster
FROM
  pg_index AS idx
JOIN
  pg_class AS i
ON
  i.oid = idx.indexrelid
WHERE
  idx.indisclustered
  AND idx.indrelid::regclass = 'your_table_name'::regclass;

If you want to know if a given table is CLUSTERed using SQL, you can use the following query to show the index being used (tested in Postgres versions 9.5 and 9.6):

SELECT
  i.relname AS index_for_cluster
FROM
  pg_index AS idx
JOIN
  pg_class AS i
ON
  i.oid = idx.indexrelid
WHERE
  idx.indisclustered
  AND idx.indrelid::regclass = 'your_table_name'::regclass;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文