postgresql中二级索引的定义是什么?

What is the definition of secondary index in postgresql?

来自https://www.postgresql.org/docs/9.6/static/indexes-index-only-scans.html

All indexes in PostgreSQL are secondary indexes, meaning that each index is stored separately from the table's main data area (which is called the table's heap in PostgreSQL terminology).

在postgresql中,二级索引是否定义为与table的主数据区分开存储的索引?

如果不是,它的定义是什么,为什么引文提到不是定义的那个?

同理,主索引的定义是什么?

postgresql 中的概念与Oracle database中的概念相同吗?

谢谢。

似乎二级索引这个词没有正式的定义。在引用的段落中,作者想强调索引与数据 table 分开存储(物理上,这是两个文件)。通常将二级索引理解为一级索引以外的索引。但是,在 Postgres 中,table 的主键也基于与堆分开存储的索引。这些意义不同,我觉得不要太在意这个。

主要和次要指标的定义有些不够精确。

参考两本大学热门教材:

Fundamentals of Database Systems, Elmasri & Navathe 将它们定义为:

A primary index as an index on an ordered file where the search key is the same as the sort key

A secondary index provides a secondary means of accessing a data file for which some primary access already exists. The data file records could be ordered, unordered, or hashed.

Database Systems: The Complete Book, Garcia-Molina et. al 将它们定义为:

A primary index determines the location of the records of the data file

The secondary index is distinguished from the primary index in that a secondary index does not determine the placement of records in the data file. Rather, the secondary index tells us the current locations of records; that location may have been decided by a primary index on some other field

以上任一定义都适用的一些属性:

  • 主键可以是主索引
  • 每个 table
  • 最多可以有 1 个主索引
  • 主索引唯一确定记录在物理存储中的保存位置。
  • 所有其他索引都归类为次要索引。

但是,如果记录在数据文件中的位置不是由任何字段决定的,那么就无法构建主索引。

因此,对于已排序的文件,讨论主索引(即排序所基于的字段列表)是有意义的。我找不到其他可以构建主索引的物理文件结构示例。

Postgresql 使用堆结构作为记录的物理存储。堆没有排序(双关语警告:它们是排序的)。因此,即使是主键也是使用二级索引实现的,因此Postgresql中的所有索引都是二级索引。

其他 RDBMS 系统执行实现支持主索引的存储格式:


Postgres 文档中的语言不够精确。

All indexes in PostgreSQL are secondary indexes

这是真的。

meaning that each index is stored separately from the table's main data area

这不是为什么所有索引在 Postgresql 中都是次要的。主索引也可以与 table 的主数据区分开存储。