Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B593E9FA376 for ; Wed, 27 Sep 2006 09:34:04 -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 63338-09 for ; Wed, 27 Sep 2006 09:33:57 -0300 (ADT) X-Greylist: delayed 00:21:30.666698 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 124839FB326 for ; Wed, 27 Sep 2006 09:33:56 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mailservice.tudelft.nl (mailservice.tudelft.nl [130.161.131.5]) by svr4.postgresql.org (Postfix) with ESMTP id 22B1D5AF04F for ; Wed, 27 Sep 2006 12:12:24 +0000 (GMT) Received: from localhost (localhost [127.0.0.1]) by rav.antivirus (Postfix) with ESMTP id 8B559232EC5 for ; Wed, 27 Sep 2006 14:12:20 +0200 (CEST) X-Virus-Scanned: amavisd-new at tudelft.nl Received: from mailservice.tudelft.nl ([127.0.0.1]) by localhost (mx0.tudelft.nl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id eZXoQk6OolHV for ; Wed, 27 Sep 2006 14:12:19 +0200 (CEST) Received: from smtp-a.tudelft.nl (smtp-a.tudelft.nl [130.161.129.18]) by mx2.tudelft.nl (Postfix) with ESMTP id ABE38232E8E for ; Wed, 27 Sep 2006 14:12:19 +0200 (CEST) Received: from [192.168.0.10] (unknown [213.201.143.114]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp-a.tudelft.nl (Postfix) with ESMTP id 6EDFFF1B39 for ; Wed, 27 Sep 2006 14:12:19 +0200 (CEST) Message-ID: <451A6AA2.9060003@oli.tudelft.nl> Date: Wed, 27 Sep 2006 14:12:18 +0200 From: Jochem van Dieten User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Forcing the use of particular execution plans References: <00501c6e1ff$c80022c0$0340050a@sdg> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/350 X-Sequence-Number: 21036 Tim Truman wrote: > Query: > SELECT count(*) as count FROM > ( > SELECT * > FROM transaction t, merchant m > WHERE t.merchant_id = m.id > AND m.id = 198 > AND t.transaction_date >= '20050101' > AND t.transaction_date <= '20060925' > AND credit_card_no LIKE '1111%111' > > UNION ALL > SELECT * > FROM transaction t, merchant m > WHERE t.merchant_id = m.id > AND m.parent_merchant_id = 198 > AND t.transaction_date >= '20050101' > AND t.transaction_date <= '20060925' > AND credit_card_no LIKE '1111%111' > ) AS foobar > Actually, I think the best course of action is to rewrite the query to a faster alternative. What you can try is: SELECT SUM(count) AS count FROM ( SELECT count(*) AS count FROM transaction t, merchant m WHERE t.merchant_id = m.id AND m.id = 198 AND t.transaction_date >= '20050101' AND t.transaction_date <= '20060925' AND credit_card_no LIKE '1111%111' UNION ALL SELECT count(*) AS count FROM transaction t, merchant m WHERE t.merchant_id = m.id AND m.parent_merchant_id = 198 AND t.transaction_date >= '20050101' AND t.transaction_date <= '20060925' AND credit_card_no LIKE '1111%111' ) AS foobar; The next optimization is to merge the 2 subqueries into one. If you schema is such that m.id can not be the same as m.parent_merchant_id I think your query can be reduced to: SELECT count(*) AS count FROM transaction t, merchant m WHERE t.merchant_id = m.id AND ( m.id = 198 OR m.parent_merchant_id = 198 ) AND t.transaction_date >= '20050101' AND t.transaction_date <= '20060925' AND credit_card_no LIKE '1111%111' If m.id can be the same as m.parent_merchant_id you need something like: SELECT SUM( CASE WHEN m.id = m.parent_merchant_id THEN 2 ELSE 1 END ) AS count FROM transaction t, merchant m WHERE t.merchant_id = m.id AND ( m.id = 198 OR m.parent_merchant_id = 198 ) AND t.transaction_date >= '20050101' AND t.transaction_date <= '20060925' AND credit_card_no LIKE '1111%111' Jochem