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 1mSHPt-0002zq-Kp for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 11:24:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSHPr-0006Qx-Ne for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 11:24:55 +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 1mSHPr-0006Qp-DM for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 11:24:55 +0000 Received: from carbon.misis.ru ([85.143.104.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSHPk-0004hd-Ld for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 11:24:54 +0000 Received: from mail.tzirechnoy.ru (unknown [10.1.250.11]) by carbon.misis.ru (Postfix) with ESMTP id B7A561FE09 for ; Mon, 20 Sep 2021 14:24:44 +0300 (MSK) Received: by mail.tzirechnoy.ru (Postfix, from userid 1000) id F3875A42A4; Mon, 20 Sep 2021 14:24:43 +0300 (MSK) Date: Mon, 20 Sep 2021 14:24:43 +0300 From: Ilya Anfimov To: pgsql-general@lists.postgresql.org Subject: Re: jsonb Indexing Message-ID: <20210920112443.GA1401753@azor.tzirechnoy.ru> Mail-Followup-To: Ilya Anfimov , pgsql-general@lists.postgresql.org References: <3c162c26-e940-e49e-a465-6d484b0ca3a9@gmail.com> <8a721cb8-9f61-a09e-cae9-4050b5ff72f5@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <8a721cb8-9f61-a09e-cae9-4050b5ff72f5@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spamd-Result: default: False [-3.00 / 15.00]; ARC_NA(0.00)[]; BAYES_SPAM(0.00)[14.25%]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM(-3.00)[-1.000]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2] X-Rspamd-Queue-Id: B7A561FE09 X-Rspamd-Server: carbon List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Sep 20, 2021 at 12:52:54PM +0200, ramikvl@gmail.com wrote: > Hello Julien, > > On 9/17/21 4:00 PM, Julien Rouhaud wrote: > > Hi, > > > > On Fri, Sep 17, 2021 at 9:55 PM wrote: > > > I was wondering what I'm doing wrong. There are steps what I've tried: > > > > > > CREATE TABLE api ( > > > jdoc jsonb > > > ); > > > > > > INSERT INTO api (jdoc) > > > VALUES ('{ > > > "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a", > > > "name": "Angela Barton", > > > "is_active": true, > > > "company": "Magnafone", > > > "address": "178 Howard Place, Gulf, Washington, 702", > > > "registered": "2009-11-07T08:53:22 +08:00", > > > "latitude": 19.793713, > > > "longitude": 86.513373, > > > "tags": [ > > > "enim", > > > "aliquip", > > > "qui" > > > ] > > > }'); > > > > > > CREATE INDEX idxgintags ON api USING GIN ((jdoc->'tags')); > > > > > > EXPLAIN ANALYZE SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> > > > 'tags' ? 'qui'; > > > > > > And the result is > > > > > > Seq Scan on api (cost=0.00..1.02 rows=1 width=64) (actual > > > time=0.019..0.021 rows=1 loops=1) > > > Filter: ((jdoc -> 'tags'::text) ? 'qui'::text) > > > > > > Planning Time: 0.115 ms > > > > > > Execution Time: 0.047 ms > > > > > > Do you know why Index Scan on idxgintag is not used? > > Yes, because doing an index scan on a table containing a single row is > > an order or magnitude less efficient than simply doing a sequential > > scan. You should try to simulate something close to your production > > data to see something interesting. > > Thank you for the tip. I've tried to generate more data. I have 2000 rows in > the table but the query still uses sequential scan. > > Seq Scan on api  (cost=0.00..131.00 rows=2000 width=64) (actual > time=0.005..0.959 rows=2000 loops=1) >   Filter: ((jdoc -> 'tags'::text) ? 'qui'::text) > Planning Time: 0.064 ms > Execution Time: 1.027 ms > > Any thoughts? The planner expects index selectivity around 1 (all the rows to be selected). btw, it was right (all the rows were selected). So, trying to select something by the index is just wasting time, compared to seq scan. > >