X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id A929BD1D3A4 for ; Fri, 6 Feb 2004 15:28:27 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 38691-10 for ; Fri, 6 Feb 2004 11:28:26 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 37F30D1CA65 for ; Fri, 6 Feb 2004 11:28:22 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i16FSOJv029925; Fri, 6 Feb 2004 10:28:24 -0500 (EST) To: "Bruno BAGUETTE" Cc: pgsql-performance@postgresql.org Subject: Re: Increase performance of a UNION query that thakes 655.07 msec to be runned ? In-reply-to: References: Comments: In-reply-to "Bruno BAGUETTE" message dated "Fri, 06 Feb 2004 09:58:44 +0100" Date: Fri, 06 Feb 2004 10:28:24 -0500 Message-ID: <29924.1076081304@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/91 X-Sequence-Number: 5615 "Bruno BAGUETTE" 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