Re: partitioned table and ORDER BY indexed_field DESC LIMIT 1

From: "Luke Lonergan" <llonergan(at)greenplum(dot)com>
To: "Heikki Linnakangas" <heikki(at)enterprisedb(dot)com>, "Anton" <anton200(at)gmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: partitioned table and ORDER BY indexed_field DESC LIMIT 1
Date: 2007-08-24 16:20:28
Message-ID: C2F4535C.3CA2B%llonergan@greenplum.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

We just fixed this - I'll post a patch, but I don't have time to verify
against HEAD.

- Luke

On 8/24/07 3:38 AM, "Heikki Linnakangas" <heikki(at)enterprisedb(dot)com> wrote:

> Anton wrote:
>>>> =# explain SELECT * FROM n_traf ORDER BY date_time DESC LIMIT 1;
>>>> QUERY PLAN
>>> ----------------------------------------------------------------------------
>>> -----------------------------
>>>> Limit (cost=824637.69..824637.69 rows=1 width=32)
>>>> -> Sort (cost=824637.69..838746.44 rows=5643499 width=32)
>>>> Sort Key: public.n_traf.date_time
>>>> -> Result (cost=0.00..100877.99 rows=5643499 width=32)
>>>> -> Append (cost= 0.00..100877.99 rows=5643499 width=32)
>>>> -> Seq Scan on n_traf (cost=0.00..22.30
>>>> rows=1230 width=32)
>>>> -> Seq Scan on n_traf_y2007m01 n_traf
>>>> (cost=0.00..22.30 rows=1230 width=32)
>> ...
>>>> -> Seq Scan on n_traf_y2007m12 n_traf
>>>> (cost=0.00..22.30 rows=1230 width=32)
>>>> (18 rows)
>>>>
>>>> Why it no uses indexes at all?
>>>> -------------------------------------------
>>> I'm no expert but I'd guess that the the planner doesn't know which
>>> partition holds the latest time so it has to read them all.
>>
>> Agree. But why it not uses indexes when it reading them?
>
> The planner isn't smart enough to push the "ORDER BY ... LIMIT ..."
> below the append node. Therefore it needs to fetch all rows from all the
> tables, and the fastest way to do that is a seq scan.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Luke Lonergan 2007-08-24 16:25:53 Re: partitioned table and ORDER BY indexed_field DESC LIMIT 1
Previous Message Manuel Sugawara 2007-08-24 16:19:46 Re: Segmentation fault using digest from pg_crypto

Browse pgsql-performance by date

  From Date Subject
Next Message Luke Lonergan 2007-08-24 16:25:53 Re: partitioned table and ORDER BY indexed_field DESC LIMIT 1
Previous Message Tom Lane 2007-08-24 16:15:25 Re: When/if to Reindex