Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A6884632BE0 for ; Wed, 28 Jul 2010 07:27:56 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 50448-10 for ; Wed, 28 Jul 2010 10:27:48 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sargon.lncsa.com (mail.lncsa.com [213.215.28.11]) by mail.postgresql.org (Postfix) with ESMTP id 29E65632A4A for ; Wed, 28 Jul 2010 07:27:49 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 709E63015C29 for ; Wed, 28 Jul 2010 12:27:47 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id p7J1M6sWxfhO for ; Wed, 28 Jul 2010 12:27:47 +0200 (CEST) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id 52D8F3013B88 for ; Wed, 28 Jul 2010 12:27:47 +0200 (CEST) Received: from galba.apartia.fr (galba.apartia.fr [10.0.3.119]) by zenon.apartia.fr (Postfix) with ESMTP id 13EB95B93F748 for ; Wed, 28 Jul 2010 12:27:44 +0200 (CEST) Received: by galba.apartia.fr (Postfix, from userid 1000) id 374333589801; Wed, 28 Jul 2010 12:27:44 +0200 (CEST) Date: Wed, 28 Jul 2010 12:27:44 +0200 From: Louis-David Mitterrand To: pgsql-performance@postgresql.org Subject: 158x query improvement when removing 2 (noop) WHERE conditions Message-ID: <20100728102744.GA18521@apartia.fr> Mail-Followup-To: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201007/428 X-Sequence-Number: 39817 Hi, On a hunch I removed two (legacy) WHERE conditions from the following query I obtained a 158x speed improvement. Yet these condiditions do not filter anything. Does that make any sense? The EXPLAIN ANALYSE output is attached with, first the fast version and then the slow one. I'd like to understand what is at play here to explain such a dramatic difference. This is with pg 8.4.4. Thanks, select p3.price as first_price, p4.price as second_price, p5.price as third_price, t.id_cabin_category, t.id_currency, t.id_alert_cruise, t.id_cruise, t.created_by, t.cabin_name, t.cabin_cat_code, t.cabin_type_name, cr.login, cr.email, fx.currency_symbol, fx.currency_code, c.saildate, ct.id_cruise_type, ct.package_name, s.id_ship, s.ship_name from (select first_value(max(p.id_price)) over w as first_id_price, nth_value(max(p.id_price),2) over w as second_id_price, p.id_cabin_category, p.id_currency, p.created_on > ac.modified_on as is_new_price, ac.id_alert_cruise, ac.id_cruise, ac.cabin_name, ac.created_by, ac.cabin_cat_code, ac.cabin_type_name from alert_to_category ac join price p on (ac.id_cabin_category=p.id_cabin_category and p.id_cruise=ac.id_cruise and (p.id_currency=ac.id_currency or ac.id_currency is null)) -- XXX: removing these speeds up query by 158x ! -- where (ac.created_by=0 or nullif(0, 0) is null) -- and (p.id_cruise=0 or nullif(0, 0) is null) group by ac.id_cruise,ac.created_by,ac.id_alert_cruise,ac.cabin_name, ac.cabin_cat_code, ac.cabin_type_name, p.id_cabin_category,p.id_currency,p.id_cruise, p.created_on > ac.modified_on window w as (partition by p.id_currency,p.id_cabin_category,p.id_cruise order by p.created_on > ac.modified_on desc rows between unbounded preceding and unbounded following) order by p.id_cabin_category,p.id_currency) as t join cruiser cr on (t.created_by=cr.id_cruiser) join cruise c using (id_cruise) join cruise_type ct using (id_cruise_type) join ship s using (id_ship) join currency fx using (id_currency) join price p3 on (t.first_id_price=p3.id_price) left join price p4 on (t.second_id_price=p4.id_price) left join price p5 on (p5.id_price=(select id_price from price where id_cruise=p3.id_cruise and id_cabin_category=p3.id_cabin_category and id_currency=p3.id_currency and id_price < t.second_id_price order by id_price desc limit 1)) where t.is_new_price is true and p3.price <> p4.price; -- http://www.cruisefish.net