[英]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.