pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: dgreer@websightsolutions.com
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Trouble with massive select statement.
Date: Tue, 22 Jun 1999 11:25:10 -0400
Message-ID: <19353.930065110@sss.pgh.pa.us> (raw)
In-Reply-To: <99062207125208.13060@george>

Darren Greer <dgreer@websightsolutions.com> writes:
> Now comes the other twist.  I already have a select statement (select
> first_name, email from listeners l, listeners_data ld where l.username
> = ld.username and $filter;).  The $filter is a bunch of and
> statements that limit the data coming from the table listeners.  I
> need to get the select statement you told me to work as a filter on
> the data that this select statement grabs.  I cant see an easy way to
> do this without making this thing incredibly slow having to chech
> every user.  Any thoughts?

This isn't very clear to me, but a couple of thoughts:

1. Rather than a subselect that gets re-executed for every tuple,
consider a temporary table:
	SELECT * FROM my_table INTO temp_table WHERE .... ;
	SELECT * FROM temp_table WHERE ... ;
	DROP TABLE temp_table ;
This is currently the only way to achieve results that require multiple
levels of grouping.

2. The system knows how to exploit indexes to avoid scanning all of a
table, if you have WHERE conditions of the right form.  For example,
given an index on test_date, "WHERE test_date > '05-14-1999'" won't
bother to scan tuples older than the specified date, and a two-way
constraint like
	WHERE test_date > '05-14-1999' AND test_date < '05-21-1999'
is even more effective.  So, look at the filter conditions you expect
to use and consider making an index or two.  (Don't go overboard making
indexes, since you pay for them in extra work in table updates...)

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <19353.930065110@sss.pgh.pa.us>
Permalink:  ../19353.930065110@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/19353.930065110@sss.pgh.pa.us

 · 

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-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, dgreer@websightsolutions.com
  Subject: Re: [SQL] Trouble with massive select statement.
  In-Reply-To: <19353.930065110@sss.pgh.pa.us>
  References: <99062207125208.13060@george> <19353.930065110@sss.pgh.pa.us>

* 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