postgresql 排序索引

官方网站

In addition to simply finding the rows to be returned by a query, an index may be able to deliver them in a specific sorted order. This allows a query‘s ORDER BY specification to be honored without a separate sorting step. Of the index types currently supported by PostgreSQL, only B-tree can produce sorted output — the other index types return matching rows in an unspecified, implementation-dependent order.

The planner will consider satisfying an ORDER BY specification either by scanning an available index that matches the specification, or by scanning the table in physical order and doing an explicit sort. For a query that requires scanning a large fraction of the table, an explicit sort is likely to be faster than using an index because it requires less disk I/O due to following a sequential access pattern. Indexes are more useful when only a few rows need be fetched. An important special case is ORDER BY in combination with LIMIT n: an explicit sort will have to process all the data to identify the first n rows, but if there is an index matching the ORDER BY, the first n rows can be retrieved directly, without scanning the remainder at all.

By default, B-tree indexes store their entries in ascending order with nulls last. This means that a forward scan of an index on column x produces output satisfying ORDER BY x (or more verbosely, ORDER BY x ASC NULLS LAST). The index can also be scanned backward, producing output satisfying ORDER BY x DESC (or more verbosely, ORDER BY x DESC NULLS FIRST, since NULLS FIRST is the default forORDER BY DESC).

You can adjust the ordering of a B-tree index by including the options ASCDESCNULLS 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 (id DESC NULLS LAST);

An index stored in ascending order with nulls first can satisfy either ORDER BY x ASC NULLS FIRST or ORDER BY x DESC NULLS LAST depending on which direction it is scanned in.

You might wonder why bother providing all four options, when two options together with the possibility of backward scan would cover all the variants of ORDER BY. In single-column indexes the options are indeed redundant, but in multicolumn indexes they can be useful. Consider a two-column index on (x, y): this can satisfy ORDER BY x, y if we scan forward, or ORDER BY x DESC, y DESC if we scan backward. But it might be that the application frequently needs to use ORDER BY x ASC, y DESC. There is no way to get that ordering from a plain index, but it is possible if the index is defined as (x ASC, y DESC) or (x DESC, y ASC).

Obviously, indexes with non-default sort orderings are a fairly specialized feature, but sometimes they can produce tremendous speedups for certain queries. Whether it‘s worth maintaining such an index depends on how often you use queries that require a special sort ordering.

默认索引是B树索引,因为B树本来就是排序树,所以有索引的列会默认排序asc nulls last,然后我们设排序的时候只是设置asc/desc,nulls first/nulls last

时间: 2024-10-02 20:15:39

postgresql 排序索引的相关文章

浅谈PostgreSQL的索引

1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id int, info text); insert into t select generate_series(1,10000),'lottu'||generate_series(1,10000); create table t1 as select * from t; create table t2 a

(转)浅谈PostgreSQL的索引

1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id int, info text); insert into t select generate_series(1,10000),'lottu'||generate_series(1,10000); create table t1 as select * from t; create table t2 a

PostgreSQL创建索引并避免写数据锁定(并发的索引)

关于并发建立索引:http://58.58.27.50:8079/doc/html/9.3.1_zh/sql-createindex.html 写这篇blog源自一个帅哥在建索引发生了表锁的问题.先介绍一下Postgresql的建索引语法: Version:9.1 CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ name ] ON table [ USING method ]     ( { column | ( expression ) } [ COLLA

PostgreSQL的索引选型

PostgreSQL里面给全文检索或者模糊查询加索引提速的时候,一般会有两个选项,一个是GIST类型,一个是GIN类型,官网给出的参考如下: There are substantial performance differences between the two index types, so it is important to understand their characteristics. A GiST index is lossy, meaning that the index ma

生产环境修改PostgreSQL表索引对应的表空间

通过iostat命令发现某块磁盘的io使用率经常保持在100%,通过blkid命令获取linux raid存储盘符和挂载点的关系后,最后发现是挂载点上的一个数据库表空间在占用大io. 现象 [email protected]:~$ iostat -xm 3 |grep -v dm avg-cpu:  %user   %nice %system %iowait  %steal   %idle           11.68    0.00    3.82    8.63    0.00   75.

联系人列表字母排序索引(三)

也是忙忙碌碌好几天,今天又有时间了,继续这个文章的编写.今天是这篇文章的最后一部分.主要内容包括以下几点: 1.将中文名字转化成拼音,并提取首字母,进行排序. 2.实现分组列表Adapter模板. 3.将列表与索引结合在一起. pinyin4j是一个将中文转化成拼音的高效工具,我的源码中带了这个依赖包.通过这个工具,可以先获取一个中文的拼音. public static String getLetter(String name) { StringBuilder sb = new StringBu

PostgreSQL Select 索引优化

使用 gin() 创建全文索引后,虽然有走索引,但是当结果集很大时,查询效率还是很底下, SELECT keyword,avg_mon_search,competition,impressions,ctr,position,suggest_bid,click,update_time FROM keyword WHERE update_time is not null and plainto_tsquery('driver') @@ keyword_participle ORDER BY avg_

PostgreSQL查看索引的使用情况

--========================================== --查看索引的使用情况 --索引在重建或删除新建时sys.dm_db_index_usage_stats中相关的数据会被清除 --索引在重整是不会清除sys.dm_db_index_usage_stats的数据 SELECT DB_NAME(ixu.database_id) DataBase_Name , OBJECT_NAME(ixu.object_id) Table_Name , ix.name Ind

postgresql —— 查看索引

语句: SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'user_tbl' ORDER BY tablename, indexname; 233 原文地址:https://www.cnblogs.com/lemos/p/11616000.html