pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Shaheed Haque <shaheedhaque@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: JSONB index not in use, but is TOAST the real cause of slow query?
Date: Sun, 29 May 2022 10:58:52 -0400
Message-ID: <3457452.1653836332@sss.pgh.pa.us> (raw)
In-Reply-To: <CAHAc2jeCoX=Z9Lzd4b21KdzoozRK8qS-zO6-oC5H9e-Cm5b5aQ@mail.gmail.com>
References: <CAHAc2jf6Yp2uddfKQyGp=byvOuUzYpsHoPjyKXnUhS5+=Acuww@mail.gmail.com>
	<3110295.1653763316@sss.pgh.pa.us>
	<CAHAc2jcGDouJad=f4U6E7KOT1AwHLonjcG7eoUD00p7x2qizEw@mail.gmail.com>
	<3127818.1653767952@sss.pgh.pa.us>
	<CAHAc2jeCoX=Z9Lzd4b21KdzoozRK8qS-zO6-oC5H9e-Cm5b5aQ@mail.gmail.com>

Shaheed Haque <shaheedhaque@gmail.com> 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





view thread (9+ messages)  latest in thread

Message-ID: <3457452.1653836332@sss.pgh.pa.us>
Permalink:  ../3457452.1653836332@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3457452.1653836332@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, shaheedhaque@gmail.com
  Subject: Re: JSONB index not in use, but is TOAST the real cause of slow query?
  In-Reply-To: <3457452.1653836332@sss.pgh.pa.us>
  References: <CAHAc2jf6Yp2uddfKQyGp=byvOuUzYpsHoPjyKXnUhS5+=Acuww@mail.gmail.com> <3110295.1653763316@sss.pgh.pa.us> <CAHAc2jcGDouJad=f4U6E7KOT1AwHLonjcG7eoUD00p7x2qizEw@mail.gmail.com> <3127818.1653767952@sss.pgh.pa.us> <CAHAc2jeCoX=Z9Lzd4b21KdzoozRK8qS-zO6-oC5H9e-Cm5b5aQ@mail.gmail.com> <3457452.1653836332@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox