From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bruno BAGUETTE <pgsql-ml@baguette.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: Increase performance of a UNION query that thakes 655.07 msec to be runned ?
Date: Fri, 06 Feb 2004 10:28:24 -0500
Message-ID: <29924.1076081304@sss.pgh.pa.us> (raw)
In-Reply-To: <!~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAAJOHsHo2Nik+nfSVIsv82jwEAAAAA@baguette.net>
References: <!~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAAJOHsHo2Nik+nfSVIsv82jwEAAAAA@baguette.net>
"Bruno BAGUETTE" <pgsql-ml@baguette.net> writes:
> Do you see a way to get better performances with this query which takes
> currently 655.07 msec to be done.
> levure=> explain analyze SELECT distinct lower(substr(l_name, 1, 1)) AS
> initiale FROM people
> levure-> UNION
> levure-> SELECT distinct lower(substr(org_name, 1, 1)) AS initiale FROM
> organizations
> levure-> ORDER BY initiale;
This is inherently a bit inefficient since the UNION implies a DISTINCT
step, thus partially repeating the DISTINCT work done inside each SELECT.
It would likely be a tad faster to drop the DISTINCTs from the
subselects and rely on UNION to do the filtering. However, you're still
gonna have a big SORT/UNIQUE step.
As of PG 7.4 you could probably get a performance win by converting the
thing to use GROUP BY instead of DISTINCT or UNION:
select initiale from (
select lower(substr(l_name,1,1)) as initiale from people
union all
select lower(substr(org_name,1,1)) as initiale from organizations
) ss
group by initiale order by initiale;
This should use a HashAggregate to do the unique-ification. I think
that will be faster than Sort/Unique.
regards, tom lane
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: tgl@sss.pgh.pa.us, pgsql-ml@baguette.net
Subject: Re: Increase performance of a UNION query that thakes 655.07 msec to be runned ?
In-Reply-To: <29924.1076081304@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