X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1394052A24 for ; Fri, 29 Jul 2005 12:45:39 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 72386-03 for ; Fri, 29 Jul 2005 15:45:34 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 79F3352A15 for ; Fri, 29 Jul 2005 12:45:33 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id DCB6A40DB3D; Fri, 29 Jul 2005 16:45:17 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 034AB15EDA; Fri, 29 Jul 2005 16:42:52 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17863-05; Fri, 29 Jul 2005 16:42:48 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 8F9C515ED9; Fri, 29 Jul 2005 16:42:48 +0100 (BST) Message-ID: <42EA4E78.90305@archonet.com> Date: Fri, 29 Jul 2005 16:42:48 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Olivier Hubaut Cc: pgsql-sql@postgresql.org Subject: Re: Why are these queries so different in time? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.057 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/243 X-Sequence-Number: 22221 Olivier Hubaut wrote: > Hi, I have a question about performance querying a 7.4 database. The > orginal generated query was > > SELECT DISTINCT _compound0.object_id AS "ObjectId" > FROM (4 LEFT JOINS then a couple of WHERE conditions on 2 tables) > This on take a huge time to perform, which may come to a timeout on the > front-end application that uses the database. > So, I decided to modify manually the query like this: > > SELECT DISTINCT _compound0.object_id AS "ObjectId" > FROM (Two lots of 2 x Left-joins, unioned together) > This should give the same result set, but it's really faster than the > previous one, more than one thousand time faster. > Is there a reason for this huge difference of performance? You're probably processing 1000 more rows in the first example. It's probably running the LEFT JOIN across all the tables then restricting the results in the WHERE. As it happens you're throwing away duplicates with DISTINCT and/or UNION anyway, so you never get to see the results. Try an EXPLAIN ANALYSE of the first example and see if the rows= parts indicate very large numbers of rows being processed. To make it faster I'd remove the LEFT JOINs, since your WHERE conditions seem to rule out the NULL cases anyway. -- Richard Huxton Archonet Ltd