query optimisation

From: Abdul-wahid Paterson <aw(at)lintrix(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: query optimisation
Date: 2003-03-15 01:09:10
Message-ID: 1047690549.31880.5002.camel@yusuf.lintrix.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I have a query of the form:

select a.id, e.id from tablea a, tableb b, tablec c, tabled d, tablee e
where a.fk=b.id and b.fk=c.id and c.fk=d.id and d.fk=e.id;

In fact, my statement is a bit more complex as it is joined across 7
tables, has an aggregate function and a CASE statement.

My problem is that it executes to slow and I need to optimise it. Is
there any documentation anywhere that can help me work out how to
optimise the statement? My statement has a few other conditions that
need to be met on the joining tables. Does changing the order of the
conditional statement affect the way the statement is optimised?

I am actually adopting someone else's work. In their code I found about
7-8 different SQL statements wrapped up in a couple of pages of
programming code that were used to get the desired result. The code
seemed to me long and convoluted and I was sure that it could be
whittled down to one SQL statement. However, it now take longer to run
as one SQL statement than it did when the external program was doing the
sorting and merging of data. Surely it should be the case, that in the
worst case, Postgres should still be more efficient then letting an
external program do the work? Are there occasions when it is better to
do the calculations outside of Postgres?

Thanks for any help.

Abdul-Wahid

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike Mascari 2003-03-15 01:18:24 Re: query optimisation
Previous Message Fernando Papa 2003-03-14 23:20:35 Re: LIMIT/OFFSET doesn't work on PL/PGSQL