X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2FF069FB20A for ; Wed, 16 Aug 2006 19:52:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 17594-07 for ; Wed, 16 Aug 2006 19:51:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id A77069FB1EE for ; Wed, 16 Aug 2006 19:51:58 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k7GMpvA1029247; Wed, 16 Aug 2006 18:51:57 -0400 (EDT) To: "Peter Hardman" cc: pgsql-performance@postgresql.org Subject: Re: PostgreSQL runs a query much slower than BDE and MySQL In-reply-to: <44E35A5D.19933.35F376@peter.ssbg.zetnet.co.uk> References: <44E35A5D.19933.35F376@peter.ssbg.zetnet.co.uk> Comments: In-reply-to "Peter Hardman" message dated "Wed, 16 Aug 2006 17:48:13 +0100" Date: Wed, 16 Aug 2006 18:51:57 -0400 Message-ID: <29246.1155768717@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.183 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200608/273 X-Sequence-Number: 20458 "Peter Hardman" writes: > I'm in the process of migrating a Paradox 7/BDE 5.01 database from single-user > Paradox to a web based interface to either MySQL or PostgreSQL. > The query I run is: > /* Select all sheep who's most recent transfer was into the subject flock */ > SELECT DISTINCT f1.regn_no, f1.transfer_date as date_in > FROM SHEEP_FLOCK f1 JOIN > /* The last transfer date for each sheep */ > (SELECT f.regn_no, MAX(f.transfer_date) as last_xfer_date > FROM SHEEP_FLOCK f > GROUP BY f.regn_no) f2 > ON f1.regn_no = f2.regn_no > WHERE f1.flock_no = '1359' > AND f1.transfer_date = f2.last_xfer_date This seems pretty closely related to this recent thread: http://archives.postgresql.org/pgsql-performance/2006-08/msg00220.php in which the OP is doing a very similar kind of query in almost exactly the same way. I can't help thinking that there's probably a better way to phrase this type of query in SQL, though it's not jumping out at me what that is. What I find interesting though is that it sounds like both MSSQL and Paradox know something we don't about how to optimize it. PG doesn't have any idea how to do the above query without forming the full output of the sub-select, but I suspect that the commercial DBs know a shortcut; perhaps they are able to automatically derive a restriction in the subquery similar to what you did by hand. Does Paradox have anything comparable to EXPLAIN that would give a hint about the query plan they are using? Also, just as in the other thread, I'm thinking that a seqscan+hash aggregate would be a better idea than this bit: > -> GroupAggregate (cost=0.00..3924.91 rows=33676 width=13) (actual time=0.324..473.131 rows=38815 loops=1) > -> Index Scan using sheep_flock_pkey on sheep_flock f (cost=0.00..3094.95 rows=81802 width=13) (actual time=0.295..232.156) Possibly you need to raise work_mem to get it to consider the hash aggregation method. BTW, are you *sure* you are testing PG 8.1? The "Subquery Scan f2" plan node looks unnecessary to me, and I'd have expected 8.1 to drop it out. 8.0 and before would have left it in the plan though. This doesn't make all that much difference performance-wise in itself, but it does make me wonder what you are testing. regards, tom lane