X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 70B0C5281C for ; Thu, 26 May 2005 12:22:06 -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 54687-10 for ; Thu, 26 May 2005 15:22:05 +0000 (GMT) Received: from colo.samason.me.uk (unknown [69.55.228.22]) by svr1.postgresql.org (Postfix) with ESMTP id B428652818 for ; Thu, 26 May 2005 12:22:03 -0300 (ADT) Received: from colo.samason.me.uk (colo [69.55.228.22]) by colo.samason.me.uk (8.12.11/8.12.11) with ESMTP id j4QFM3aY002022 for ; Thu, 26 May 2005 16:22:03 +0100 (BST) (envelope-from sam@colo.samason.me.uk) Received: (from sam@localhost) by colo.samason.me.uk (8.12.11/8.12.11/Submit) id j4QFM31c002021 for pgsql-performance@postgresql.org; Thu, 26 May 2005 16:22:03 +0100 (BST) (envelope-from sam) Date: Thu, 26 May 2005 16:22:03 +0100 From: Sam Mason To: pgsql-performance@postgresql.org Subject: Optimising queries involving unions Message-ID: <20050526152203.GI15205@colo.samason.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/395 X-Sequence-Number: 12559 Hi, I've got a query that I think the query optimiser should be able to work it's magic on but it doesn't! I've had a look around and asked on the IRC channel and found that the current code doesn't attempt to optimise for what I'm asking it to do at the moment. Here's a bad example: SELECT u.txt FROM smalltable t, ( SELECT id, txt FROM largetable1 UNION ALL SELECT id, txt FROM largetable2) u WHERE t.id = u.id AND t.foo = 'bar'; I was hoping that "smalltable" would get moved up into the union, but it doesn't at the moment and the database does a LOT of extra work. In this case, I can manually do quite a couple of transforms to move things around and it does the right thing: SELECT txt FROM ( SELECT l.id as lid, r.id as rid, r.foo, l.txt FROM largetable1 l, smalltable r UNION ALL SELECT l.id as lid, r.id as rid, r.foo, l.txt FROM largetable1 l, smalltable r) WHERE foo = 'bar'; AND lid = rid The optimiser is intelligent enough to move the where clauses up into the union and end end up with a reasonably optimal query. Unfortunatly, in real life, the query is much larger and reorganising everything manually isn't really feasible! Is this a good place to ask about this or is it more in the realm of the hackers mailing list? Thanks, Sam