Re: Indexing on JSONB field not working

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Zhihong Zhang <zhihong(at)gmail(dot)com>
Cc: pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Indexing on JSONB field not working
Date: 2019-12-21 02:32:16
Message-ID: CAMkU=1zGu2wBkPQmF0cS_mowcdLNhevP0D6xot2SrfEqwnMTXw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Dec 20, 2019 at 7:39 PM Zhihong Zhang <zhihong(at)gmail(dot)com> wrote:

> A few clarifications,
>
> 1. The index is very effective. If I treat it as text, the index works.
> With index, the result returns in 1 second. Otherwise, it’s 5 minutes.
>

An index is over a datatype. You can't just "treat an index like text".
Either it is an index over text, or it is not. It you cast an expression
to text while defining the index, then that is a different index than if
you defined it with a cast to double precision.

> 2. Removing limit doesn’t change the behavior.
>

I have no problem getting your index to be used with or without the LIMIT ,
as long as the expression "(_doc #> '{floatValue}'::text[])::double
precision < 3.0" is selective enough that it appears to be worth using an
index for it. For example:

insert into assets (id,_doc) select x,
jsonb_build_object('floatValue',random()*1000) from
generate_series(1,1000000) f(x);

We can't comment on how two queries might differ, then we have only seen
one of them.

Cheers,

Jeff

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2019-12-21 02:41:52 Re: BUG #16161: pg_ctl stop fails sometimes (on Windows)
Previous Message Michael Paquier 2019-12-21 02:26:32 Re: BUG #16161: pg_ctl stop fails sometimes (on Windows)