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 1nywjA-0002mR-9t for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2022 14:32:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nywj8-000792-U0 for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2022 14:32:06 +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 1nywj8-00078s-K7 for pgsql-performance@lists.postgresql.org; Wed, 08 Jun 2022 14:32:06 +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 1nywj6-0005W9-8F for pgsql-performance@lists.postgresql.org; Wed, 08 Jun 2022 14:32:05 +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 258EW30L1623003; Wed, 8 Jun 2022 10:32:03 -0400 From: Tom Lane To: Marcin Krupowicz cc: pgsql-performance@lists.postgresql.org Subject: Re: Adding non-selective key to jsonb query @> reduces performance? In-reply-to: References: Comments: In-reply-to Marcin Krupowicz message dated "Wed, 08 Jun 2022 11:55:44 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1623001.1654698723.1@sss.pgh.pa.us> Date: Wed, 08 Jun 2022 10:32:03 -0400 Message-ID: <1623002.1654698723@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Marcin Krupowicz writes: > However this one, is slow: > Q2 > select count(*) from tbl where row @> '{"SELECTIVE_COL": > "SearchValue", "DATE": "20220606", "NON_SELECTIVE_COL": "Abc"}'::jsonb > It takes 17ms > Note that the only difference is adding one more - not very unique - > key. If in Q2 I replaced NON_SELECTIVE_COL with another selective > column, it's becoming fast again. This doesn't surprise me a whole lot based on what I know of GIN. It's going to store sets of TIDs associated with each key or value mentioned in the data, and then a query will have to AND the sets of TIDs for keys/values mentioned in the query. That will take longer when some of those sets are big. It might be worth experimenting with an index built using the non-default jsonb_path_ops opclass [1]. I'm not sure if that'd be faster for this scenario, but it seems worth trying. regards, tom lane [1] https://www.postgresql.org/docs/current/datatype-json.html#JSON-INDEXING