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 1nvKNj-000220-9f for pgsql-general@arkaria.postgresql.org; Sun, 29 May 2022 14:59:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nvKNh-0007ZE-9m for pgsql-general@arkaria.postgresql.org; Sun, 29 May 2022 14:59:01 +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 1nvKNg-0007Z5-Tv for pgsql-general@lists.postgresql.org; Sun, 29 May 2022 14:59:00 +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 1nvKNa-0002rq-8A for pgsql-general@lists.postgresql.org; Sun, 29 May 2022 14:58:59 +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 24TEwq723457453; Sun, 29 May 2022 10:58:52 -0400 From: Tom Lane To: Shaheed Haque cc: pgsql-general@lists.postgresql.org Subject: Re: JSONB index not in use, but is TOAST the real cause of slow query? In-reply-to: References: <3110295.1653763316@sss.pgh.pa.us> <3127818.1653767952@sss.pgh.pa.us> Comments: In-reply-to Shaheed Haque message dated "Sat, 28 May 2022 21:48:57 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3457451.1653836332.1@sss.pgh.pa.us> Date: Sun, 29 May 2022 10:58:52 -0400 Message-ID: <3457452.1653836332@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Shaheed Haque writes: > Unfortunately, the real query which I think should behave very > similarly is still at the several-seconds level despite using the > index. ... > -> Bitmap Heap Scan on paiyroll_payrun (cost=26.88..30.91 rows=1 > width=4) (actual time=32.488..2258.891 rows=62 loops=1) > Recheck Cond: ((company_id = 173) AND ((snapshot -> > 'employee'::text) ? '16376'::text)) > Filter: (((snapshot #> > '{employee,16376,last_run_of_employment}'::text[]) <> 'true'::jsonb) > OR ((snapshot #> '{employee,16376,pay_graph}'::text[]) <> '0'::jsonb) > OR ((snapshot #> '{employee,16376,state,employment,-1,2}'::text[]) <= > '0'::jsonb)) > Heap Blocks: exact=5 > -> BitmapAnd (cost=26.88..26.88 rows=1 width=0) (actual > time=0.038..0.039 rows=0 loops=1) > -> Bitmap Index Scan on paiyroll_payrun_company_id_ce341888 > (cost=0.00..6.56 rows=304 width=0) (actual time=0.016..0.016 rows=304 > loops=1) > Index Cond: (company_id = 173) > -> Bitmap Index Scan on idx1 (cost=0.00..20.07 rows=9 > width=0) (actual time=0.021..0.021 rows=62 loops=1) > Index Cond: ((snapshot -> 'employee'::text) ? '16376'::text) > IIUC, at the bottom, the indices are doing their thing, but a couple > of layers up, the "Bitmap Heap Scan" jumps from ~30ms to 2200ms. But I > cannot quite see why. I suppose it's the execution of that "Filter" condition, which will require perhaps as many as three fetches of the "snapshot" column. You really need to rethink that data structure. Sure, you can store tons of unorganized data in a jsonb column, but you pay for that convenience with slow access. Normalizing the bits you need frequently into a more traditional relational schema is the route to better-performing queries. regards, tom lane