Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ehXGK-0007Rk-Bc for pgsql-performance@arkaria.postgresql.org; Fri, 02 Feb 2018 09:04:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ehXGJ-00082U-Is for pgsql-performance@arkaria.postgresql.org; Fri, 02 Feb 2018 09:03:59 +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_2) (envelope-from ) id 1ehXEs-000303-6c for pgsql-performance@lists.postgresql.org; Fri, 02 Feb 2018 09:02:30 +0000 Received: from smtp-4.sys.kth.se ([2001:6b0:1:1300:250:56ff:fea6:2de3]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ehXEj-0000D5-16 for pgsql-performance@lists.postgresql.org; Fri, 02 Feb 2018 09:02:28 +0000 Received: from smtp-4.sys.kth.se (localhost.localdomain [127.0.0.1]) by smtp-4.sys.kth.se (Postfix) with ESMTP id 71504274E for ; Fri, 2 Feb 2018 10:02:14 +0100 (CET) X-Virus-Scanned: by amavisd-new at kth.se Received: from smtp-4.sys.kth.se ([127.0.0.1]) by smtp-4.sys.kth.se (smtp-4.sys.kth.se [127.0.0.1]) (amavisd-new, port 10024) with LMTP id p2NDVQ_f6ZGN for ; Fri, 2 Feb 2018 10:02:08 +0100 (CET) X-KTH-Auth: eskil [2001:6b0:1:11f0:6e62:6dff:fe6a:ffa3] DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kth.se; s=default; t=1517562128; bh=Nh0PgK+GxSH6Fg4MiiMZrwlE85F1tKyQGIBl4vrQxfQ=; h=Subject:From:To:Date:In-Reply-To:References; b=X7PGRdY9juFOhyODczYGfIFfX9WBvsUh9JpkBd1YTpxqlL5T5ioDyi5puAEWcgvv4 TP26T79+ehPOlcYNoQaUkB3Pe/Sb+ATC/6vAiJRM9zlxQ18VvKPBqpGnmmQxKwaSrr pPZ0K7UsEehgGtRPOSs1uq8x5F2Nz8WAMaFk+eoo= X-KTH-mail-from: eskil@kth.se X-KTH-rcpt-to: pgsql-performance@lists.postgresql.org Received: from tiny.ite.kth.se (tiny.ite.kth.se [IPv6:2001:6b0:1:11f0:6e62:6dff:fe6a:ffa3]) by smtp-4.sys.kth.se (Postfix) with ESMTPSA id 29E2926F2 for ; Fri, 2 Feb 2018 10:02:07 +0100 (CET) Message-ID: <1517562127.315.1.camel@kth.se> Subject: Re: SV: bad plan using nested loops From: Johan Fredriksson To: pgsql-performance@lists.postgresql.org Date: Fri, 02 Feb 2018 10:02:07 +0100 In-Reply-To: <1517517264764.92820@kth.se> References: <1517481727.23675.3.camel@kth.se> ,<23735.1517497241@sss.pgh.pa.us> <1517517264764.92820@kth.se> Organization: KTH Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.22.6 (3.22.6-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk tor 2018-02-01 klockan 20:34 +0000 skrev Johan Fredriksson: > > Johan Fredriksson writes: > > > Bad plan: https://explain.depesz.com/s/avtZ > > > Good plan: https://explain.depesz.com/s/SJSt > > > Any suggestions on how to make the planner make better decisions > > > for > > > this query? > > > > Core of the problem looks to be the misestimation here: > > > >        Index Only Scan using shredder_cgm1 on > > public.cachedgroupmembers cachedgroupmembers_4 > > (cost=0.43..2.33 rows=79 width=8) (actual time=0.020..0.903 > > rows=1492 loops=804) > >          Output: cachedgroupmembers_4.memberid, > > cachedgroupmembers_4.groupid, > > cachedgroupmembers_4.disabled > >          Index Cond: ((cachedgroupmembers_4.memberid = > > principals_1.id) AND > > (cachedgroupmembers_4.disabled = 0)) > >          Heap Fetches: 5018 > > > > Probably, memberid and disabled are correlated but the planner > > doesn't > > know that, so it thinks the index condition is way more selective > > than it > > actually is.  In PG 10, you could very possibly fix that by > > installing > > extended statistics on that pair of columns.  See > > > > https://www.postgresql.org/docs/current/static/planner-stats.html#P > > LANNER-STATS-EXTENDED > > I'm not sure what you mean by correlated, but there are only a > handful (164 when I check it) disabled groupmembers out of total 7.5 > million. > I'll give CREATE STATISTICS on those columns a shot and see if it > gets any better. It looks like you are right, Tom. There actually exists full correlation between memberid, groupid and disabled. rt4=# SELECT stxname, stxkeys, stxdependencies FROM pg_statistic_ext;  stxname  | stxkeys |   stxdependencies     -----------+---------+----------------------  cgm_stat2 | 2 6     | {"2 => 6": 1.000000}  cgm_stat1 | 3 6     | {"3 => 6": 1.000000} (2 rows) However, this does not help the planner. It still picks the bad plan. / Eskil