Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a9zl6-0005nn-3F for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Dec 2015 18:28:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a9zl5-0007lP-Hz for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Dec 2015 18:28:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9zkT-00074W-6l for pgsql-bugs@postgresql.org; Fri, 18 Dec 2015 18:27:25 +0000 Received: from ns1.hjp.at ([212.17.106.132] helo=zeno.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9zkL-0002rW-34 for pgsql-bugs@postgresql.org; Fri, 18 Dec 2015 18:27:23 +0000 Received: by zeno.hjp.at (Postfix, from userid 1000) id 5325E404D; Fri, 18 Dec 2015 19:27:13 +0100 (CET) Date: Fri, 18 Dec 2015 19:27:13 +0100 From: "Peter J. Holzer" To: pgsql-bugs@postgresql.org Subject: Re: BUG #13817: Query planner strange choose while select/count small part of big table - complete Message-ID: <20151218182712.GD362@hjp.at> References: <20151214114527.11345.70373@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="brEuL7wsLY8+TuWz" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --brEuL7wsLY8+TuWz Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2015-12-17 16:19:24 +0100, Marcin Sie=C5=84ko wrote: > I've done it already. I've also already check rewrited query. No changes.= But i > have a idea. There is generated base and there are "normal" shipment_orde= r_item > with 3 - 100 maybe 10000 shipment_order_sub_item but one has 3 992 102. C= ould > it be a problem? A very skewed distribution can be a problem, yes. If the planner doesn't know whether you will select a shipment_order_item with 3 subitems or one with 3 million subitems, it can only use the average number of subitems - which may be bad for either case. But your plan looks worse than a single outlier of 3992102. The nested loop semi join in line 3 of http://explain.depesz.com/s/Rus (I'm using that as the reference because it's easier to read than your line-wrapped plan and because the lines are numbered and easier to reference) is expected to return 1 row. But the nested loop at line 2 is expected to return 3992515 rows. Unless I'm missing something this means that planner thinks that there are *on average* 3992515 subitems per item.=20 Can you post the output of these queries? select * from pg_stats where tablename=3D'shipment_order_sub_item' and attname=3D'shipment_order_i= tem_id'; select count(*) from shipment_order_sub_item; select count(distinct(shipment_order_item_id)) =66rom shipment_order_sub_item; > If yes that is my bad to ask You because I'belive there will not occur in > production and counting could bye match faster. Yeah, if you have unrealistic test data, it will produce unrealistic results and you can probably ignore them. Although in my experience it is usually the other way around: The test data has a nice, regular distribution and the production data is skewed a lot more than anyone expected. hp --=20 _ | Peter J. Holzer | I want to forget all about both belts and |_|_) | | suspenders; instead, I want to buy pants=20 | | | hjp@hjp.at | that actually fit. __/ | http://www.hjp.at/ | -- http://noncombatant.org/ --brEuL7wsLY8+TuWz Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIVAwUBVnRP//IOSFES/ihdAQKQeg/+K/Hxj02Zfzkdl9HlEp3s8uLPxtW7CuLA ZKZuI3gF7tGy/xIuhLY6SbaOnALq0Eax2aqnAPUFrNSGdGrQJNybtmG25gIj4uFz KrVr/U3vMEMQRufdN5ljqfI2Up/40EWv6z7GfRNLkn066PhEnT+dYl4ALM3VBCfl EhPhwOBOtAnWeMps70WG7KMykBAoOh25KxoOnPmfeJTwROGCmhAYVaDuChNGemzC uA0ZXnBwjOFCWORtd/z1ORJFseBoYCab32XzhQf2wjjr3u0gF4CN6GSfbhJhNOld wcMY9SVkm6s+Ke7hF6jey33zepWzYTxbNRRZzM10ifg5de/qd6EEa2HczxlY5Cwg FX7NWfQAqXXmHO9L6nZ/16vhde52qwHmJXGOgtnXBlK7Smqg9QrmzO7Z7c7/k/HI JmaAPCGwqyR8XWWjCMCpxEi/2xP6UpiUiO74p/70bVx63xC9dK0E6DjJp4TFx3oy 07p/hJhdAeBlXeFPvA4EqYgc9xtz/YxqbNgFR1OyqbbXxbkgzdBj5yG1iLRlf22P 8/Pb0bHjDYKnZItKq4gQWUtMvmsvi+J9fz1Mbaep3Ubffmxj7aAfJ63xNGjYcXAH X29VUkmH6t1jnBKQ1xO+5fhtl2lGPd97MGoCOj11iN6toab4TYBua9imbrc4nN/p emtCFSf5K2Y= =ZVOv -----END PGP SIGNATURE----- --brEuL7wsLY8+TuWz--