Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 33F701337C20 for ; Thu, 14 Apr 2011 05:12:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 08464-05-4 for ; Thu, 14 Apr 2011 08:12:29 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from vidle.i.cz (vidle.i.cz [78.31.27.2]) by mail.postgresql.org (Postfix) with ESMTP id 75ADB1337BE4 for ; Thu, 14 Apr 2011 05:11:57 -0300 (ADT) Received: from ns1.i.cz (ns1.i.cz [192.168.24.101]) by vidle.i.cz (Postfix) with ESMTPS id 2578AAD062 for ; Thu, 14 Apr 2011 10:11:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by ns1.i.cz (Postfix) with ESMTP id 4BB86FE86 for ; Thu, 14 Apr 2011 10:11:56 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at ns1.i.cz Received: from ns1.i.cz ([127.0.0.1]) by localhost (ns1.i.cz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qYDJd6e8FJjO for ; Thu, 14 Apr 2011 10:11:56 +0200 (CEST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by Authenticated User (ns1.i.cz) with ESMTPSA id 75D65FE8A Received: from zito by bobek.localdomain with local (Exim 4.75) (envelope-from ) id 1QAHea-0003SG-AZ for pgsql-performance@postgresql.org; Thu, 14 Apr 2011 10:11:52 +0200 Date: Thu, 14 Apr 2011 10:11:52 +0200 From: =?iso-8859-1?Q?V=E1clav_Ovs=EDk?= To: pgsql-performance@postgresql.org Subject: Re: poor execution plan because column dependence Message-ID: <20110414081152.GB11328@bobek.localdomain> References: <20110412232343.GA15966@bobek.localdomain> <20568.1302655935@sss.pgh.pa.us> <20110413082139.GB24050@bobek.localdomain> <6361.1302711846@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6361.1302711846@sss.pgh.pa.us> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/210 X-Sequence-Number: 43272 On Wed, Apr 13, 2011 at 12:24:06PM -0400, Tom Lane wrote: > > Interesting the original index tickets5 is still used for > > int4eq(main.effectiveid, main.id), no need to build a different. > > Well, no, it won't be. This hack is entirely dependent on the fact that > the optimizer mostly works with operator expressions, and is blind to > the fact that the underlying functions are really the same thing. > (Which is something I'd like to see fixed someday, but in the meantime > it gives you an escape hatch.) If you use the int4eq() construct in a > context where you'd like to see it transformed into an index qual, it > won't be. For this particular case that doesn't matter because there's > no use in using an index for that clause anyway. But you'll need to be > very careful that your changes in the query generator don't result in > using int4eq() in any contexts other than the "main.EffectiveId=main.id" > check. Sorry I'm not certain understand your paragraph completely... I perfectly understand the fact that change from A = B into int4eq(A, B) stopped bad estimate and execution plan is corrected, but that can change someday in the future. I'm not certain about your sentence touching int4eq() and index. The execution plan as show in my previous mail contains information about using index tickets5: ... -> Index Scan using tickets5 on tickets main (cost=0.00..4.38 rows=1 width=162) (actual time=0.006..0.006 rows=0 loops=15593) Index Cond: (main.id = transactions_1.objectid) Filter: (((main.status)::text <> 'deleted'::text) AND (main.lastupdated > '2008-12-31 23:00:00'::timestamp without time zone) AND (main.created > '2005-12-31 23:00:00'::timestamp without time zone) AND int4eq(main.effectiveid, main.id) AND (main.queue = 15) AND ((main.type)::text = 'ticket'::text) AND ((main.status)::text = 'resolved'::text)) ... Filter condition contains int4eq(main.effectiveid, main.id) and tickets5 is: "tickets5" btree (id, effectiveid) That means tickets5 index was used for int4eq(main.effectiveid, main.id). Is it right? Or am I something missing? Well the index will not be used generally probably, because of selectivity of int4eq() you mention (33%). The planner thinks it is better to use seq scan then. I tried this now. I did hack for this particular case only: diff --git a/local/lib/DBIx/SearchBuilder.pm b/local/lib/DBIx/SearchBuilder.pm index f3ee1e1..9e3a6a6 100644 --- a/local/lib/DBIx/SearchBuilder.pm +++ b/local/lib/DBIx/SearchBuilder.pm @@ -1040,7 +1040,9 @@ sub _CompileGenericRestrictions { $result .= ' '. $entry . ' '; } else { - $result .= join ' ', @{$entry}{qw(field op value)}; + my $term = join ' ', @{$entry}{qw(field op value)}; + $term =~ s/^(main|Tickets_\d+)\.(EffectiveId) = (\1)\.(id)$/int4eq($1.$2, $3.$4)/i; + $result .= $term; } } $result .= ')'; It works as expected. Thanks Best Regards -- Zito