patternsqlMinor
Why is PostgreSQL 9.5 not using my newest index for ORDER BY, even though it uses similar indices just fine?
Viewed 0 times
postgresqlindiceswhyorderthoughfinejustnewestusessimilar
Problem
(Follow up from this post: Why is my PostgreSQL expression index not being used when I ORDER BY in a subquery?)
PostgreSQL 9.5.
I can't divulge full details, but
(From the previous post you know that I was trying to avoid creating this additional column, and just use an expression index--adding two
All are btree except the
The following queries all take only 12ms and use just an
But if I try to use my new
...it takes 2.7s and uses
My "cheating" method is the closest I seem to be able to get to making use of the index:
This takes 12ms and uses
This is after a
I thought it strange that my expression index was not being used in the other question. Now it's just a plain old column (I haven't even added what would be the necessary triggers to actually go this route.)
Why isn't my newest index being used, when the other three work "just fine"? (As pointed out in the comments at https://dba.stackexchange.com/a/183290/
PostgreSQL 9.5.
I can't divulge full details, but
table has 22 columns and 5 indexes:- primary key ('pk'),
text(btree)
- another
text(btree)
- a
timestamp with time zone(btree)
- a
tsvector(gin)
- my latest one, a
bigint(btree)
(From the previous post you know that I was trying to avoid creating this additional column, and just use an expression index--adding two
integer columns together--without success. The bigint column here probably could have been just 'integer' but I made a mistake creating it; it took about an hour to add the column, populate it, and reindex, so I'm hoping this isn't relevant but mentioning it just in case it is.)All are btree except the
tsvector.The following queries all take only 12ms and use just an
Index Scan:SELECT pk FROM table ORDER BY pk DESC LIMIT 10
SELECT pk FROM table ORDER BY text_column DESC LIMIT 10
SELECT pk FROM table ORDER BY timestamp_column DESC LIMIT 10
But if I try to use my new
bigint index for ORDER BY:SELECT pk FROM table ORDER BY bigint_column DESC LIMIT 10...it takes 2.7s and uses
Limit -> Sort -> Seq Scan.My "cheating" method is the closest I seem to be able to get to making use of the index:
SELECT pk
FROM table
WHERE bigint_column > 1000000
ORDER BY bigint_column DESC LIMIT 10This takes 12ms and uses
Limit -> Sort -> Bitmap Heap Scan (bigint_column > 1000000) -> Bitmap Index Scan (bigint_column > 1000000).This is after a
VACUUM ANALYZE after adding the index.I thought it strange that my expression index was not being used in the other question. Now it's just a plain old column (I haven't even added what would be the necessary triggers to actually go this route.)
Why isn't my newest index being used, when the other three work "just fine"? (As pointed out in the comments at https://dba.stackexchange.com/a/183290/
Solution
In
You could either rebuild the index, or (since you know the column is not null) you can add
Note that
PostgreSQL, an index which is DESC NULLS LAST cannot be used to satisfy an ORDER BY which is DESC NULLS FIRST (which includes ordering by simply DESC because that implies NULLS FIRST). This is the case even if the column is defined to be NOT NULL.You could either rebuild the index, or (since you know the column is not null) you can add
NULLS LAST to your query's ORDER BY to make it match the existing index.Note that
PostgreSQL does know how to follow an index backwards, so a default index (which is implicitly ASC NULLS LAST) would also be able to satisfy your DESC NULLS FIRST query. Because of this, it is rarely important to specify DESC in an index, but it can be important to specify which end the NULLS sort to.Context
StackExchange Database Administrators Q#183478, answer score: 7
Revisions (0)
No revisions yet.