英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论