Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YyU0P-0006u3-Iz for pgsql-performance@arkaria.postgresql.org; Fri, 29 May 2015 23:48:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YyU0O-0007hC-D0 for pgsql-performance@arkaria.postgresql.org; Fri, 29 May 2015 23:48:00 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YyU0M-0007h1-7c for pgsql-performance@postgresql.org; Fri, 29 May 2015 23:47:58 +0000 Received: from mail-wi0-f181.google.com ([209.85.212.181]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YyU0I-0000U0-NC for pgsql-performance@postgresql.org; Fri, 29 May 2015 23:47:56 +0000 Received: by wifw1 with SMTP id w1so41732440wif.0 for ; Fri, 29 May 2015 16:47:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=s4bC4/0Hkf5x5swF0sFXUj+2f3us/zlVRmUXMCwUqwQ=; b=E9/DJGQtuwymZowbtyeUY91u5E6QpurvrFF1DO70h/Upgt9frCb8blKvNYYkC5Td/x v2+x4uWD8Lm+cWxXMZZmMEX3a34KD/WhzNkE8As/5D4FutawYH6BL9PC65Ssgg264H6W Gt+/7z0AAig/CuKbysPzJWCoJFcYJjwEju2iYYHYX0Mbar74TF1adQFdSxJdWTOdMV9o NUUCGhF/W1soz54jGOfdmSfYDKpW46rG0NRBY9lnWwLGLhQN529J1VSxM3u9BnhUz1iz HuTaAwtvPBuiH5zz67ZbwpPMM1f2bSXRVEoIsSStpDz77r6ndWC1G8horYAwWCpu8pok IjXg== X-Gm-Message-State: ALoCoQng7SeF5Cp04T0LXVYWoFpNkiV/JgfiYXzz42WK8k4qB2pghzQH4nW+sN2qUoloE2MTNsiwMGs8uasL2iUsjEWuL3DpS70ZKohoFr+1FKAp68E5Pw/V80oY3KIZ4K0vOgvuh11FOXjHQTsnF5QiR2wqiy//EFUj+TWTCNkTBuIGqvN+t8xO/OHdBWVJEiUZZI81eit2DczR/8vR1YVD/Dr8jwoiig== X-Received: by 10.194.84.100 with SMTP id x4mr20197043wjy.69.1432943272407; Fri, 29 May 2015 16:47:52 -0700 (PDT) Received: from [10.137.2.12] (ip-78-45-136-74.net.upcbroadband.cz. [78.45.136.74]) by mx.google.com with ESMTPSA id um5sm10425547wjc.1.2015.05.29.16.47.50 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 May 2015 16:47:51 -0700 (PDT) Message-ID: <5568FAA5.3080807@2ndquadrant.com> Date: Sat, 30 May 2015 01:47:49 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Different plan for very similar queries References: <20150529085544.GA15813@hjp.at> <20150529095117.GB15813@hjp.at> In-Reply-To: <20150529095117.GB15813@hjp.at> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Hi, On 05/29/15 11:51, Peter J. Holzer wrote: > A couple of additional observations: > > The total cost of both queries is quite similar, so random variations > might push into one direction or the other. Indeed, after dropping > and recreating indexes (I tried GIN indexes as suggested by Heikki on > [1]) and calling analyze after each change, I have now reached a > state where both queries use the fast plan. I don't think bitmap indexes are particularly good match for this use case. The queries need to check an existence of a few records, and btree indexes are great for that - the first plan is very fast. Why exactly does the second query use a much slower plan I'm not sure. I believe I've found an issue in planning semi joins (reported to pgsql-hackers a few minutes ago), but may be wrong and the code is OK. Can you try forcing the same plan for the second query, using "enable" flags? E.g. SET enable_mergejoin = off; will disable the merge join, and push the optimizer towards a different join type. You may have to disable a few more node types until you get the same plan as for the first query, i.e. nestloop semi join -> index scan -> index scan See this for more info: http://www.postgresql.org/docs/9.1/static/runtime-config-query.html Also, have you tuned the PostgreSQL configuration? How? Can you provide the dataset? Not necessarily all the columns, it should be sufficient to provide the columns used in the join/where clauses: term -> facttablename, columnname, term facttable_stat_fta4 -> einheit, berechnungsart That'd make reproducing the problem much easier. > In the first case the query planner seems to add the cost of the two > index scans to get the total cost, despite the fact that for a semi > join the second index scan can be aborted after the first hit (so > either the cost of the second scan should be a lot less than > 384457.80 or it needs to be divided by a large factor for the semi > join). > > In the second case the cost of the second index scan (2545748.85) is > either completely ignored or divided by a large factor: It doesn't > seem to contribute much to the total cost. I believe this is a consequence of the semi join semantics, because the explain plan contains "total" costs and row counts, as if the whole relation was scanned (in this case all the 43M rows), but the optimizer only propagates fraction of the cost estimate (depending on how much of the relation it expects to scan). In this case it expects to scan a tiny part of the index scan, so the impact on the total cost is small. A bit confusing, yeah. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance