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 1ehLZA-0006rR-PN for pgsql-performance@arkaria.postgresql.org; Thu, 01 Feb 2018 20:34:41 +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 1ehLZ9-0001lL-Es for pgsql-performance@arkaria.postgresql.org; Thu, 01 Feb 2018 20:34:39 +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 1ehLZ9-0001l8-1h for pgsql-performance@lists.postgresql.org; Thu, 01 Feb 2018 20:34:39 +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 1ehLZ3-0000rs-LP for pgsql-performance@lists.postgresql.org; Thu, 01 Feb 2018 20:34:37 +0000 Received: from smtp-4.sys.kth.se (localhost.localdomain [127.0.0.1]) by smtp-4.sys.kth.se (Postfix) with ESMTP id B70441F9D; Thu, 1 Feb 2018 21:34:28 +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 t6kmlhcmbXQC; Thu, 1 Feb 2018 21:34:27 +0100 (CET) Received: from exdb02.ug.kth.se (unknown [192.168.32.112]) by smtp-4.sys.kth.se (Postfix) with ESMTPS id E6B06DEC; Thu, 1 Feb 2018 21:34:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kth.se; s=default; t=1517517267; bh=iGv3ZvwEK46Lv0fC7hj+KbumjZSVbICSKn/T1ralwwQ=; h=From:To:CC:Subject:Date:References:In-Reply-To; b=MCF5Z9ItdXZdKAXJ3wBCZEnD6P88f8Twl8JP2GDOX68vFe/Unhxhq29aD9ckxI0yE uSBwr1EA38fvHg6rTUaN+eXCQLpGMHC3wph557xp19vdPCqJuQ+4LDZH5mT9UWzfmv WhkAxfRm6/CvogJ+PObpUYsv13i8Nfh+5q6X/hBc= Received: from exdb03.ug.kth.se (192.168.32.113) by exdb02.ug.kth.se (192.168.32.112) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Thu, 1 Feb 2018 21:34:25 +0100 Received: from exdb03.ug.kth.se (192.168.32.113) by exdb03.ug.kth.se (192.168.32.113) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Thu, 1 Feb 2018 21:34:25 +0100 Received: from exdb03.ug.kth.se ([192.168.32.113]) by exdb03.ug.kth.se ([192.168.32.113]) with mapi id 15.00.1347.000; Thu, 1 Feb 2018 21:34:25 +0100 From: Johan Fredriksson To: Tom Lane CC: "pgsql-performance@lists.postgresql.org" Subject: SV: bad plan using nested loops Thread-Topic: bad plan using nested loops Thread-Index: AQHTm0lhwDr/VkjvJUWizG4PpAMD+aOPk4SAgABpk+c= Date: Thu, 1 Feb 2018 20:34:24 +0000 Message-ID: <1517517264764.92820@kth.se> References: <1517481727.23675.3.camel@kth.se>,<23735.1517497241@sss.pgh.pa.us> In-Reply-To: <23735.1517497241@sss.pgh.pa.us> Accept-Language: sv-SE, en-US Content-Language: sv-SE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [37.247.4.194] Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =0A= > Johan Fredriksson writes:=0A= > > Bad plan: https://explain.depesz.com/s/avtZ=0A= > > Good plan: https://explain.depesz.com/s/SJSt=0A= > > Any suggestions on how to make the planner make better decisions for=0A= > > this query?=0A= > =0A= > Core of the problem looks to be the misestimation here:=0A= > =0A= > Index Only Scan using shredder_cgm1 on public.cachedgroupmembers c= achedgroupmembers_4=0A= > (cost=3D0.43..2.33 rows=3D79 width=3D8) (actual time=3D0.020..0.903 rows= =3D1492 loops=3D804)=0A= > Output: cachedgroupmembers_4.memberid, cachedgroupmembers_4.grou= pid,=0A= > cachedgroupmembers_4.disabled=0A= > Index Cond: ((cachedgroupmembers_4.memberid =3D principals_1.id)= AND=0A= > (cachedgroupmembers_4.disabled =3D 0))=0A= > Heap Fetches: 5018=0A= >=0A= > Probably, memberid and disabled are correlated but the planner doesn't=0A= > know that, so it thinks the index condition is way more selective than it= =0A= > actually is. In PG 10, you could very possibly fix that by installing=0A= > extended statistics on that pair of columns. See=0A= > =0A= > https://www.postgresql.org/docs/current/static/planner-stats.html#PLANNER= -STATS-EXTENDED=0A= =0A= 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.=0A= I'll give CREATE STATISTICS on those columns a shot and see if it gets any = better.=0A= =0A= / Eskil=0A=