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 1nj18e-00079m-2b for pgsql-performance@arkaria.postgresql.org; Mon, 25 Apr 2022 16:00:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nj18c-0002YH-Kn for pgsql-performance@arkaria.postgresql.org; Mon, 25 Apr 2022 16:00:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nj18c-0002Xa-BL for pgsql-performance@lists.postgresql.org; Mon, 25 Apr 2022 16:00:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nj18X-0005tn-U2 for pgsql-performance@postgresql.org; Mon, 25 Apr 2022 16:00:33 +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 23PG0L8O087995; Mon, 25 Apr 2022 12:00:21 -0400 From: Tom Lane To: Emil Iggland cc: pgsql-performance@postgresql.org Subject: Re: Performance differential when 0 values present vs when 1 values present. Planner return 52k rows when 0 expected. In-reply-to: <4eacd5f7-1f8d-1e0d-ad9f-61f6c5ab706e@metrima.com> References: <4eacd5f7-1f8d-1e0d-ad9f-61f6c5ab706e@metrima.com> Comments: In-reply-to Emil Iggland message dated "Fri, 22 Apr 2022 16:53:48 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <87993.1650902421.1@sss.pgh.pa.us> Date: Mon, 25 Apr 2022 12:00:21 -0400 Message-ID: <87994.1650902421@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Emil Iggland writes: > The query that is giving us issues is the following, channel 752433 has > NO values, 752431 has values. > (Channel 752433 only has valueseries 752434) > select * from datavalue > where dataview in ( select id from valueseries where channel = > %channel_idx%) > ORDER BY VALUETIMESTAMP DESC > FETCH FIRST ROW only; > Running explain analyze shows strange numbers, 52'000 rows are being > returned but there are no rows there. > For channel 752433 > -> Index Scan Backward using > datavalue_2022_03_valuetimestamp_dataview_idx on datavalue_2022_03 > datavalue_6 (cost=0.42..7166.19 rows=119673 width=226) (actual > time=0.008..32.831 rows=119601 loops=1) You've got the wrong column order (for this query anyway) in that index. It'd work a lot better if dataview were the first column; or at least, it wouldn't tempt the planner to try this unstably- performing plan. It's trying to use the index ordering to satisfy the ORDER BY, which works great as long as it finds a dataview match in some reasonably recent index entry. Otherwise, it's going to crawl the whole index to discover that there's no match. regards, tom lane