pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthew Wakeling <matthew@flymine.org>
To: pgsql-performance@postgresql.org
Subject: Re: limit clause breaks query planner?
Date: Thu, 4 Sep 2008 17:20:12 +0100 (BST)
Message-ID: <alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org> (raw)
In-Reply-To: <87wshrrj10.fsf@mnc.ch>
References: <48BD33B2.D078.0028.0@rentrak.com>
	<48BD33B2.D078.0028.0@rentrak.com>
	<5925.1220389248@sss.pgh.pa.us>
	<48BF195E.D078.0028.0@rentrak.com>
	<23341.1220542340@sss.pgh.pa.us>
	<alpine.DEB.1.10.0809041646340.4454@aragorn.flymine.org>
	<87wshrrj10.fsf@mnc.ch>

On Thu, 4 Sep 2008, Guillaume Cottenceau wrote:
> It seems to me that if the correlation is 0.99, and you're
> looking for less than 1% of rows, the expected rows may be at the
> beginning or at the end of the heap?

Not necessarily. Imagine for example that you have a table with 1M rows, 
and one of the fields has unique values from 1 to 1M, and the rows are 
ordered in the table by that field. So the correlation would be 1. If you 
were to SELECT from the table WHERE the field = 500000 LIMIT 1, then the 
database should be able to work out that the rows will be right in the 
middle of the table, not at the beginning or end. It should set the 
startup cost of a sequential scan to the amount of time required to 
sequential scan half of the table.

Of course, this does bring up a point - if the matching rows are 
concentrated at the end of the table, the database could perform a 
sequential scan backwards, or even a scan from the middle of the table 
onwards.

This improvement of course only actually helps if the query has a LIMIT 
clause, and presumably would muck up simultaneous sequential scans.

Matthew

-- 
Picard: I was just paid a visit from Q.
Riker:  Q! Any idea what he's up to?
Picard: No. He said he wanted to be "nice" to me.
Riker:  I'll alert the crew.



view thread (26+ messages)  latest in thread

Message-ID: <alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org>
Permalink:  ../alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org/
Also on:    postgresql.org/message-id/alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org

 · 

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: matthew@flymine.org
  Subject: Re: limit clause breaks query planner?
  In-Reply-To: <alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org>
  References: <48BD33B2.D078.0028.0@rentrak.com> <5925.1220389248@sss.pgh.pa.us> <48BF195E.D078.0028.0@rentrak.com> <23341.1220542340@sss.pgh.pa.us> <alpine.DEB.1.10.0809041646340.4454@aragorn.flymine.org> <87wshrrj10.fsf@mnc.ch> <alpine.DEB.1.10.0809041712490.4454@aragorn.flymine.org>

* 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