Re: index usage for min() vs. "order by asc limit 1"

From: Ben Chobot <bench(at)silentmedia(dot)com>
To: Steve Atkins <steve(at)blighty(dot)com>
Cc: PostgreSQL Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: index usage for min() vs. "order by asc limit 1"
Date: 2011-11-18 01:23:38
Message-ID: 1F701652-23B3-4350-84D8-809254B6C6B0@silentmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Nov 17, 2011, at 5:20 PM, Steve Atkins wrote:
>
> I don't think you want the group by in that first query.

Heh, I tried to simply the example, but in reality that = becomes an in clause of multiple values. So the group by is needed.

>>
>>
>> postgres=# explain analyze select min(id) from delayed_jobs where strand='sis_batch:account:15' group by strand;
>> QUERY PLAN
>> --------------------------------------------------------------------------------------------------------------------------
>> GroupAggregate (cost=0.00..8918.59 rows=66 width=29) (actual time=226.759..226.760 rows=1 loops=1)
>> -> Seq Scan on delayed_jobs (cost=0.00..8553.30 rows=72927 width=29) (actual time=0.014..169.941 rows=72268 loops=1)
>> Filter: ((strand)::text = 'sis_batch:account:15'::text)
>> Total runtime: 226.817 ms
>> (4 rows)
>>
>> postgres=# explain analyze select id from delayed_jobs where strand='sis_batch:account:15' order by id limit 1;
>> QUERY PLAN
>> ---------------------------------------------------------------------------------------------------------------------------------------------------------
>> Limit (cost=0.00..0.33 rows=1 width=8) (actual time=0.097..0.098 rows=1 loops=1)
>> -> Index Scan using index_delayed_jobs_on_strand on delayed_jobs (cost=0.00..24181.74 rows=72927 width=8) (actual time=0.095..0.095 rows=1 loops=1)
>> Index Cond: ((strand)::text = 'sis_batch:account:15'::text)
>> Total runtime: 0.129 ms
>> (4 rows)
>>
>>
>> --
>> Sent via pgsql-performance mailing list (pgsql-performance(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-performance
>
>
> --
> Sent via pgsql-performance mailing list (pgsql-performance(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-performance

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message CSS 2011-11-18 03:44:46 Re: SSD options, small database, ZFS
Previous Message Steve Atkins 2011-11-18 01:20:34 Re: index usage for min() vs. "order by asc limit 1"