Skip site navigation (1) Skip section navigation (2)

Re: Increase performance of a UNION query that thakes 655.07 msec to be runned ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Bruno BAGUETTE" <pgsql-ml(at)baguette(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Increase performance of a UNION query that thakes 655.07 msec to be runned ?
Date: 2004-02-06 15:28:24
Message-ID: 29924.1076081304@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-performance
"Bruno BAGUETTE" <pgsql-ml(at)baguette(dot)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

In response to

Responses

pgsql-performance by date

Next:From: Stephan SzaboDate: 2004-02-06 15:52:24
Subject: Re: Increase performance of a UNION query that thakes
Previous:From: Tom LaneDate: 2004-02-06 14:55:28
Subject: Re: Seq scan on zero-parameters function

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group