pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Meinel <john@johnmeinel.com>
To: Jaime Casanova <systemguards@yahoo.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Sequential Scan with LIMIT
Date: Tue, 26 Oct 2004 17:21:35 -0500
Message-ID: <417ECDEF.8030108@johnmeinel.com> (raw)
In-Reply-To: <20041026201906.30736.qmail@web50001.mail.yahoo.com>
References: <20041026201906.30736.qmail@web50001.mail.yahoo.com>

Jaime Casanova wrote:
[...]
>>
>>I'm not sure. They all return the same information.
> 
> 
> of course, both queries will return the same but
> that's just because you forced it.
> 
> LIMIT and DISTINCT are different things so they behave
> and are plenned different.
> 
> 
> 
>>What's also weird is stuff like:
>>SELECT DISTINCT(NULL) FROM mytable WHERE col =
>>'myval' LIMIT 1;
> 
> 
> why do you want to do such a thing?
> 
> regards,
> Jaime Casanova
> 

I was trying to see if selecting a constant would change things.
I could have done SELECT DISTINCT(1) or just SELECT 1 FROM ...
The idea of the query is that if 'myval' exists in the table, return 
something different than if 'myval' does not exist. If you are writing a 
function, you can use:

SELECT something...
IF FOUND THEN
   do a
ELSE
   do b
END IF;

The whole point of this exercise was just to find what the cheapest 
query is when you want to test for the existence of a value in a column. 
The only thing I've found for my column is:

SET enable_seq_scan TO off;
SELECT col FROM mytable WHERE col = 'myval' LIMIT 1;
SET enable_seq_scan TO on;

My column is not distributed well (larger numbers occur later in the 
dataset, but may occur many times.) In total there are something like 
500,000 rows, the number 555647 occurs 100,000 times, but not until row 
300,000 or so.

The analyzer looks at the data and says "1/5th of the time it is 555647, 
so I can just do a sequential scan as the odds are I don't have to look 
for very long, then I don't have to load the index". It turns out this 
is very bad, where with an index you just have to do 2 page loads, 
instead of reading 300,000 rows.

Obviously this isn't a general-case solution. But if you have a 
situation similar to mine, it might be useful.

(That's one thing with DB tuning. It seems to be very situation 
dependent, and it's hard to plan without a real dataset.)

John
=:->

Attachments:

  signature.asc (application/pgp-signature, 252B) download

view thread (13+ messages)  latest in thread

Message-ID: <417ECDEF.8030108@johnmeinel.com>
Permalink:  ../417ECDEF.8030108@johnmeinel.com/
Also on:    postgresql.org/message-id/417ECDEF.8030108@johnmeinel.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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: john@johnmeinel.com, systemguards@yahoo.com
  Subject: Re: Sequential Scan with LIMIT
  In-Reply-To: <417ECDEF.8030108@johnmeinel.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