Why don't use index on x when ORDER BY x, y?

From: Vlad Arkhipov <arhipov(at)dc(dot)baikal(dot)ru>
To: pgsql-performance <pgsql-performance(at)postgresql(dot)org>
Subject: Why don't use index on x when ORDER BY x, y?
Date: 2014-11-24 11:02:18
Message-ID: 5473103A.5040004@dc.baikal.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello,

I wonder why Postgres does not use index in the query below? It is a
quite common use-case when you want to sort records by an arbitrary set
of columns but do not want to create a lot of compound indexes for all
possible combinations of them. It seems that if, for instance, your
query's ORDER BY is x, y, z then any of these indexes could be used to
improve the performance: (x); (x, y); (x, y, z).

create temp table t as
select s as x, s % 10 as y, s % 100 as z
from generate_series(1, 1000000) s;

analyze t;
create index idx1 on t (y);

select *
from t
order by y desc, x
limit 10;

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Robert Klemme 2014-11-24 13:39:11 Re: Why don't use index on x when ORDER BY x, y?
Previous Message Johann Spies 2014-11-24 07:01:56 Re: pgtune + configurations with 9.3