pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dror Matalon <dror@zapatec.com>
To: Postgresql Performance <pgsql-performance@postgresql.org>
Subject: Re: Speeding up Aggregates
Date: Thu, 9 Oct 2003 17:44:46 -0700
Message-ID: <20031010004446.GN2979@rlx11.zapatec.com> (raw)
In-Reply-To: <87ekxm9fjf.fsf@stark.dyndns.tv>
References: <20031003202120.GO87525@rlx11.zapatec.com>
	<1065217488.91586.3.camel@jester>
	<20031003215347.GQ87525@rlx11.zapatec.com>
	<1065219028.91586.8.camel@jester>
	<87llrvbx0f.fsf@stark.dyndns.tv>
	<20031008181819.GJ2979@rlx11.zapatec.com>
	<87ekxm9fjf.fsf@stark.dyndns.tv>

On Thu, Oct 09, 2003 at 07:07:00PM -0400, Greg Stark wrote:
> Dror Matalon <dror@zapatec.com> writes:
> 
> > Actually what finally sovled the problem is repeating the 
> > dtstamp > last_viewed
> > in the sub select
> 
> That will at least convince the optimizer to use an index range lookup. But it
> still will have to scan every record that matches channel==$1, link==$2, and
> dtstamp>$3.
> 
> The trick of using limit 1 will be faster still as it only has to retrieve a
> single record using the index. But you have to be sure to convince it to use

How is doing order by limit 1 faster than doing max()? Seems like the
optimizer will need to sort or scan the data set either way. That part
didn't actually make a difference in my specific case.


> the index and the way to do that is to list exactly the same columns in the
> ORDER BY as are in the index definition. 
> 
> Even if some of the leading columns are redundant because they'll be constant
> for all of the records retrieved. The optimizer doesn't know to ignore those.

The main problem in my case was that the optimizer was doing the max()
on all 700 rows, rather than the filtered rows. It's not until I put the
"dtstamp> last_viewed" in the sub select as well as in the main query
that it realized that it can first filter the 696 rows out and then to
the max() on the 4 rows that satisfied this constraint. 

That was the big saving.

Hope this all makes sense,

Dror
> 
> > > (This is the thing i pointed out previously in
> > > <87el6ckrlu.fsf@stark.dyndns.tv> on Feb 13th 2003 on pgsql-general)
> 
> -- 
> greg
> 

-- 
Dror Matalon
Zapatec Inc 
1700 MLK Way
Berkeley, CA 94709
http://www.zapatec.com



view thread (22+ messages)  latest in thread

Message-ID: <20031010004446.GN2979@rlx11.zapatec.com>
Permalink:  ../20031010004446.GN2979@rlx11.zapatec.com/
Also on:    postgresql.org/message-id/20031010004446.GN2979@rlx11.zapatec.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: dror@zapatec.com
  Subject: Re: Speeding up Aggregates
  In-Reply-To: <20031010004446.GN2979@rlx11.zapatec.com>
  References: <20031003202120.GO87525@rlx11.zapatec.com> <1065217488.91586.3.camel@jester> <20031003215347.GQ87525@rlx11.zapatec.com> <1065219028.91586.8.camel@jester> <87llrvbx0f.fsf@stark.dyndns.tv> <20031008181819.GJ2979@rlx11.zapatec.com> <87ekxm9fjf.fsf@stark.dyndns.tv> <20031010004446.GN2979@rlx11.zapatec.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox