Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is6p6-0003JD-N6 for pgsql-performance@arkaria.postgresql.org; Thu, 16 Jan 2020 15:12:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is6o6-0001iM-KO for pgsql-performance@arkaria.postgresql.org; Thu, 16 Jan 2020 15:11:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is6o6-0001iF-7v for pgsql-performance@lists.postgresql.org; Thu, 16 Jan 2020 15:11:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is6nz-0000s6-07 for pgsql-performance@lists.postgresql.org; Thu, 16 Jan 2020 15:11:36 +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 00GFBQca019060; Thu, 16 Jan 2020 10:11:26 -0500 From: Tom Lane To: Cosmin Prund cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad query plan decision when using multiple column index - postgresql uses only first column then filters In-reply-to: References: Comments: In-reply-to Cosmin Prund message dated "Thu, 16 Jan 2020 16:06:06 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19058.1579187486.1@sss.pgh.pa.us> Date: Thu, 16 Jan 2020 10:11:26 -0500 Message-ID: <19059.1579187486@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Cosmin Prund writes: > explain analyze > select R, C, V from LBD > where Ver = 92 and Id in (10,11) > Index Scan using "IX_LBD_Ver_Id" on "LBD" (cost=0.56..2.37 rows=1 > width=13) (actual time=0.063..857.725 rows=2 loops=1) > Index Cond: ("Ver" = 92) > Filter: ("Id" = ANY ('{10,11}'::integer[])) > Rows Removed by Filter: 1869178 > Planning time: 0.170 ms > Execution time: 857.767 ms > The IX_LBD_Ver_Id index is on two columns (Ver, Id) - it's not in "Ver" > alone! Seems like an odd choice of plan, then, but you haven't provided any detail that would let anyone guess why it's not using the second index column. For starters it would be good to show the exact table and index schema (eg via \d+ in psql). Also, does explicitly ANALYZE'ing the table change anything? https://wiki.postgresql.org/wiki/Slow_Query_Questions regards, tom lane