Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyDST-0005rw-07 for pgsql-performance@arkaria.postgresql.org; Mon, 06 Jun 2022 14:11:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nyDSQ-0002tw-PY for pgsql-performance@arkaria.postgresql.org; Mon, 06 Jun 2022 14:11:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyDSQ-0002tm-FL for pgsql-performance@lists.postgresql.org; Mon, 06 Jun 2022 14:11:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyDSL-0007QB-B8 for pgsql-performance@postgresql.org; Mon, 06 Jun 2022 14:11:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 256EBg1I894425; Mon, 6 Jun 2022 10:11:42 -0400 From: Tom Lane To: Justin Pryzby cc: Mayank Kandari , pgsql-performance@postgresql.org Subject: Re: Query is taking too long i intermittent In-reply-to: <20220606103504.GS29853@telsasoft.com> References: <20220606103504.GS29853@telsasoft.com> Comments: In-reply-to Justin Pryzby message dated "Mon, 06 Jun 2022 05:35:04 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <894423.1654524702.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 06 Jun 2022 10:11:42 -0400 Message-ID: <894424.1654524702@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Justin Pryzby writes: > On Mon, Jun 06, 2022 at 03:28:43PM +0530, Mayank Kandari wrote: >> SELECT event_id FROM event WHERE (event_sec > time.seconds) OR >> ((event_sec=3Dtime.seconds) AND (event_usec>=3Dtime.useconds) ORDER BY >> event_sec, event_usec LIMIT 1 > I think it'd be better if the column was a float storing the fractional = number > of seconds. Currently, it may be hard for the planner to estimate rowco= unts if > the conditions are not independent. I don't know if it's related to thi= s > problem, though. Also, even if you can't change the data representation, there's a more idiomatic way to do that in SQL: use a row comparison. SELECT ... WHERE row(event_sec, event_usec) >=3D row(time.seconds, time.useconds) ... I doubt this is notably more execution-efficient, but if you're getting a bad rowcount estimate it should help with that. It's easier to read too. regards, tom lane