PostgreSQL索引与顺序

huangapple go评论54阅读模式
英文:

postgresql index with order

问题

在 PostgreSQL 中,是否有办法在字段上创建索引,并确保索引返回按 id 降序排列的结果?

谢谢

英文:

is there any way to create with postgresql an index on a field and make sure index returns results ordered by id DESC?

Thanks

答案1

得分: 1

Indexes store their entries in ascending order. 如果您想知道索引是否可以以降序存储其条目,答案是

您可以通过在创建索引时包括选项ASC、DESC、NULLS FIRST和/或NULLS LAST来添加有序索引;例如:

CREATE INDEX test2_info_nulls_low ON test2 (info NULLS FIRST);
CREATE INDEX test3_desc_index ON test3 (Col DESC NULLS LAST);

这意味着对列Col中的索引进行反向扫描会生成满足ORDER BY Col DESC的输出。

文档在这里

英文:

Indexes store their entries in ascending order, If you are asking about if indexes can store their entries in descending order then the answer is YES.

You can add an ordered index by including the options ASC, DESC, NULLS FIRST, and/or NULLS LAST when creating the index; for example:

CREATE INDEX test2_info_nulls_low ON test2 (info NULLS FIRST);
CREATE INDEX test3_desc_index ON test3 (Col DESC NULLS LAST);

This means that a backward scan of an index in column Col produces output satisfying ORDER BY Col DESC

Docs here

huangapple
  • 本文由 发表于 2023年5月10日 22:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定