简体   繁体   English

什么时候应该使用 PostgreSQL 索引包含?

[英]When should I use PostgreSQL Index Include?

Assume that I have a query like below:假设我有如下查询:

Select 
t2.date,
max(t1.title),
sum(t2.spend)
...
from table1 t1
left join table t2 on t1.id = t2.t1_id
where t1.sup_id = 1
group by t2.date
order by t2.date

PostgreSQL 11+ supports index include. PostgreSQL 11+ 支持索引包括。 But I couldn't understand when we should use it.但我不明白我们什么时候应该使用它。 Could you please show it on this example or another simple example?你能在这个例子或另一个简单的例子中展示它吗?

From documentation:从文档:

The optional INCLUDE clause specifies a list of columns which will be included in the index as non-key columns.可选的 INCLUDE 子句指定将作为非键列包含在索引中的列列表。 A non-key column cannot be used in an index scan search qualification, and it is disregarded for purposes of any uniqueness or exclusion constraint enforced by the index.非键列不能用于索引扫描搜索限定,并且出于索引强制执行的任何唯一性或排除约束的目的而忽略它。 However, an index-only scan can return the contents of non-key columns without having to visit the index's table, since they are available directly from the index entry.但是,仅索引扫描可以返回非键列的内容,而不必访问索引的表,因为它们可以直接从索引条目中获得。 Thus, addition of non-key columns allows index-only scans to be used for queries that otherwise could not use them.因此,添加非键列允许将仅索引扫描用于无法使用它们的查询。

In your case imagine you have an index on sup_id.在您的情况下,假设您在 sup_id 上有一个索引。 If you only select sup_id in your query postgres will do an index only scan, but if you select other columns along with id, such as title and date, postgres will have to visit the index table and select these columns, which is slower than an index-only scan. If you only select sup_id in your query postgres will do an index only scan, but if you select other columns along with id, such as title and date, postgres will have to visit the index table and select these columns, which is slower than an仅索引扫描。
So if you want your select query to be faster you include the columns you want in your index.因此,如果您希望 select 查询更快,您可以在索引中包含所需的列。 Beware that this will increase the size of the index considerably.请注意,这将大大增加索引的大小。 Only use it if you have a small number of columns in your select query.仅当 select 查询中有少量列时才使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL为什么/何时应该使用ECPG - PostgreSQL why/when should I use ECPG 对于具有 3 个值的列,我应该在 postgresql 中使用哪种索引 - What kind of index should I use in postgresql for a column with 3 values Rails 4 / postgresql索引-我应该使用可以包含无限多个值的datetime列作为索引过滤器吗? - Rails 4/ postgresql index - Should I use as index filter a datetime column which can have infinite number of values? 什么时候应该使用postgresql 10声明性分区功能? - When should I use the postgresql 10 declarative partitioning function? 在postgresql上查询JSON arrays应该用lateral吗? - Should I use lateral when querying JSON arrays on postgresql? 升级 postgresql 版本时是否应该使用 airflow 升级版? - Should I use airflow upgradedb when upgrading the postgresql version? 何时在PostgreSQL中使用JSONB - When we should use JSONB in PostgreSQL 我应该使用哪个postgresql包? - Which postgresql package should I use? 我应该如何在 PostgreSQL 中使用 COALESCE function? - How should I use the COALESCE function in PostgreSQL? PostgreSQL:当将一个整数转换为非整数类型以强制PostgreSQL中的浮点除法时,我应该使用哪种数字类型? - PostgreSQL: When casting an integer to a non-integer type to force floating point division in PostgreSQL, which number type should I use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM