Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dSGBB-0005Ml-9Z for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Jul 2017 05:15:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dSGBA-0008Rf-6g for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Jul 2017 05:15:16 +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 1dSGB9-0008R3-Eu for pgsql-bugs@postgresql.org; Tue, 04 Jul 2017 05:15:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dSGB6-0000Rl-MR for pgsql-bugs@postgresql.org; Tue, 04 Jul 2017 05:15:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v645F9rY015013; Tue, 4 Jul 2017 01:15:09 -0400 From: Tom Lane To: Pavel Tavoda cc: Pavel Stehule , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14729: Between operator is slow when same value used for low and high margin In-reply-to: References: <20170703100923.1448.48433@wrigleys.postgresql.org> Comments: In-reply-to Pavel Tavoda message dated "Mon, 03 Jul 2017 16:39:01 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15011.1499145309.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 04 Jul 2017 01:15:09 -0400 Message-ID: <15012.1499145309@sss.pgh.pa.us> 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 Pavel Tavoda writes: > -> Seq Scan on contractportfolio cp (cost=3D0.00..109171.90 row= s=3D1 width=3D8) (actual time=3D0.009..486.918 rows=3D20426 loops=3D1) > Filter: ((validfor >=3D '2017-05-31'::date) AND (validfor <= =3D '2017-05-31'::date)) > STUNNING!!!! Yup, it's certainly that factor-of-20K rowcount misestimate that is killing you here. Given that this estimate isn't too bad: > -> Seq Scan on contractportfolio cp (cost=3D0.00..101496.91 row= s=3D20197 width=3D8) (actual time=3D0.009..463.063 rows=3D20426 loops=3D1) > Filter: (validfor =3D '2017-05-31'::date) I do not think your problem is one of out-of-date statistics. Rather, the issue is just that we're bad at narrow range estimates. I did some work on that today[1] but it won't show up in a released PG version before next year. In the meantime, I believe that the existing code would arrive at a plausible answer if the value being checked were present in the column's pg_stats.most_common_vals list. Maybe you could fix this by increasing the statistics target for the column or the whole table (and re-analyzing it, of course). regards, tom lane [1] https://www.postgresql.org/message-id/12232.1499140410%40sss.pgh.pa.us --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs