More Performance Questions

From: Gordan Bobic <gordan(at)bobich(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: More Performance Questions
Date: 2001-11-07 16:22:48
Message-ID: 200111071622.fA7GMnM05089@sentinel.bobich.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi.

Speed-wise, is there a signifficant performance difference between doing
complex queries in the following forms?

Form 1:
(
SELECT Master.*
FROM Master,
MasterFTI
WHERE MasterFTI.ID = Master.ID AND
MasterFTI.String = 'string1'
)
UNION
(
SELECT Master.*
FROM Master,
MasterFTI
WHERE MasterFTI.ID = Master.ID AND
MasterFTI.String = 'string2'
)
...
UNION
...;

Form 2:
SELECT DISTINCT Master.*
FROM Master,
MasterFTI
WHERE MasterFTI.ID = Master.ID AND
(
MasterFTI.String = 'string1' OR
MasterFTI.String = 'string2' OR
...
);

The reason am asking is because I don't know how the back end splits and
executes these queries. Are the UNION/INTERSECT/EXCEPT queries each executed
separately in sequence? Or does the optimizer do some magic and transform
them in a more efficient way that doesn't require multiple passes?

And is the overhead of running multiple UNION queries greater than the
overhead of doing a DISTINCT? I need to sort the records anyway, so the fact
that DISTINCT does a SORT is a bonus in this case.

In an extreme case my dynamically constructed queries (from a CGI) can have
as many as 50 terms in them, which using the UNION method, equates to 50
queries being run (if that is the way it all gets executed). Is there likely
to be a sizeable improvement in using the other method? The reason I am
asking before trying is because I'd like to avoid re-writing my custom->SQL
parser again.

I was hoping that someone with a bit more background knowledge into how
PostgreSQL works could shed some light on it...

Regards.

Gordan

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Michel POURE 2001-11-07 16:34:27 Re: functions vs embedded SQL
Previous Message Bryan White 2001-11-07 14:59:00 Re: a query ...