From: Дмитрий Воронин <carriingfate92@yandex.ru>
To: Steve Midgley <science@misuse.org>
Cc: pgsql-sql@lists.postgresql.org <pgsql-sql@lists.postgresql.org>
Subject: Re:
Date: Wed, 02 Feb 2022 08:38:18 +0300
Message-ID: <18223021643779668@sas8-ed615920eca2.qloud-c.yandex.net> (raw)
In-Reply-To: <CAJexoSKv=Jrm7TWsWJGQiChWvcWWmCr9ufx-E83+EV__ZfSYfg@mail.gmail.com>
References: <506811643704589@mail.yandex.ru>
<CAJexoSKv=Jrm7TWsWJGQiChWvcWWmCr9ufx-E83+EV__ZfSYfg@mail.gmail.com>
Hi, Steve.<br /><br />19:15, 1 февраля 2022 г., Steve Midgley <science@misuse.org>:<br /><blockquote class="210e7a848e8fcb45wmi-quote"><div dir="ltr"><div class="f13ca48719c8a60033905b23b39675agmail_quote"><div class="334d7d341e3233c5b27ca91297445127gmail_attr" dir="ltr">On Tue, Feb 1, 2022 at 12:42 AM Дмитрий Воронин <<a href="mailto:carriingfate92@yandex.ru">carriingfate92@yandex.ru</a>> wrote:<br /></div><blockquote class="f13ca48719c8a60033905b23b39675agmail_quote" style="border-left-color:rgb( 204 , 204 , 204 );border-left-style:solid;border-left-width:1px;margin:0px 0px 0px 0.8ex;padding-left:1ex"><div>Hi all,</div><div> </div><div>I'm using PostgreSQL 13.</div><div> </div><div>I have a table:</div><div> </div><div>CREATE TABLE test(docid integer, jsonb attrs);</div><div> </div><div>So, attrs contains data like</div><div> </div><div>...</div><div>"dates": ["<span class="1f1ea193f6735cf0wmi-callto">2019-10-02</span>", "<span class="1f1ea193f6735cf0wmi-callto">2018-02-03</span>"]</div><div>...</div><div> </div><div>So, I want to SELECT all docids, which dates in range:</div><div> </div><div><div>SELECT attrs FROM document_resinfo WHERE attrs @? '$.dates[*].datetime() ? (@ >= "<span class="1f1ea193f6735cf0wmi-callto">2020-10-02</span>".datetime())';</div><div> </div><div>How can I create index on attrs field to query docids with other date? Thanks.</div></div><div> </div></blockquote><div><br /></div><div>Have you tried just putting a default index on that column? I think it should work fine.</div><div><br /></div><div>CREATE INDEX attrs_idx ON test (attrs)</div></div></div></blockquote><div><br /></div><div>Yes, I do. This speeds up search on first-levels keys on queries like:</div><div><br /></div><div>SELECT docid FROM test WHERE attrs @> '{"kw": ["a", "b"]}'</div><br /><blockquote class="210e7a848e8fcb45wmi-quote"><div dir="ltr"><div class="f13ca48719c8a60033905b23b39675agmail_quote"><div>IIRC, jsonb can be indexed like any other column and you get significant performance benefits when using the index. Also IIRC, you can index "deeper" into jsonb if you only want to index part of the jsonb structure - which is more efficient, so you don't index a bunch of elements that you never search.</div></div></div></blockquote><div><br /></div>Yes, I known that.<br /><blockquote class="210e7a848e8fcb45wmi-quote"><div dir="ltr"><div class="f13ca48719c8a60033905b23b39675agmail_quote"><div></div><div>Have you tried this approach? What problems are you experiencing?</div><div></div></div></div></blockquote><div><br /></div><div>Yes, I already create another index on dates key. I think because jsonpath type does not have good index support, I will give sequential scan on this table. </div><div><br /></div><div>So, searching on dates field will be often and I want speed up by indexing but I don't known how.</div><br /><blockquote class="210e7a848e8fcb45wmi-quote"><div dir="ltr"><div class="f13ca48719c8a60033905b23b39675agmail_quote"><div>Steve</div></div></div>
</blockquote><br /><br />Best regards, Dmitry Voronin
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: carriingfate92@yandex.ru, science@misuse.org, pgsql-sql@lists.postgresql.org
Subject: Re:
In-Reply-To: <18223021643779668@sas8-ed615920eca2.qloud-c.yandex.net>
* 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