If I create a Postgres index what's the difference between ASC and DESC?
Does it mean ASC is be开发者_如何学Gotter for queries such as
PRICE > 40
and DESC is better for queries such as
PRICE < 40?
The where condition might not be affected, but the ORDER BY is definitely affected
PostgreSQL Index Ordering Documentation
PostgreSQL (or any other DB engine, for that matter) will read your index in either way fine. You'll get an index scan or a reverse index scan.
The problem is when you've got a multi-column scan. In this case:
index on (foo, bar)
will work for foo asc, bar asc
, as well as foo desc, bar desc
. On some databases this will not work on foo desc, bar asc
(reverse scans foo, ignores bar), or foo asc, bar desc
(scans foo, ignores bar).
Descending can give you a boost if the column(s) contain sequencial data that is referenced by "recent" values -- date columns, ID columns , etc.
Generally speaking, the tables/indexes would have to be really large for it to make a difference, though.
It won't affect what gets returned, just how.
精彩评论