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 1mSHkW-0003tg-3B for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 11:46:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSHkU-0002la-5H for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 11:46:14 +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 1mSHkT-0002lS-PI for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 11:46:13 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mSHkQ-0004rP-W1 for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 11:46:12 +0000 Received: by mail-ed1-x535.google.com with SMTP id eg28so37347909edb.1 for ; Mon, 20 Sep 2021 04:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=mdEc02u72dsRWnTc948zTTtoqg9BwoXrsa5iTOENlKY=; b=XAhY5jWnfxzHyVJ+f4FucKTab5ZBnDGKtuo+aZoW3DcIL3NL6ASE0fHoRfEwvyx/j5 QX7g6snN6GFnDJYm8UVqGkUv4De+XPZ5wbuE1/zAus5/DF+TefncjN7rQPWCuaHc2iPU HohLmKhNlxzYltGr4mHppn3Ae6SjevVzMN/tfVcPv2/GchoO/dZEHjVDY2mjsnFPH5Pc 7Ss+5VcIcFzqbje4eA3nO3qxEKYkm9G6AS4v1IreNs0/cb3GqL4HWymCa1Eo4TLLLAmG brxx1PYpzLU4M/7td3JI6Fc7FbAXpXfqS2bhDMNv1vePpxk5V6rJTmyx2uTYgDg84JnP HBpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=mdEc02u72dsRWnTc948zTTtoqg9BwoXrsa5iTOENlKY=; b=wCuDrMiDDEsLlDWmWcWIpPAdHdHQO29df/pjjtUlDEY9p8IGx29dcWTbDFQgWv9xIP 9hEf+zYsUrebS1DXJZVymEP9yxWBKaxAOj1IxeE0DCoCZhgbjW+XYDn3Sc+tiji9E+/b C61/rB3AhUWgDPZ4Evkcoq036rmOBx6QO4LWjv1hQy36RQD17kgmnWM9D13ZTrZ1azOZ jEFfUVfu+Vu8NVcCAaRrov+0GLZiXgepEG8tg67rHAESrkx8sP97285YGVnlextkzdEz DV1ajKNB2M5FBMh3QNFY8hxm1EwNP5f0vPPWeqUnuQKwP5JX25DkaIMN2g+1xK2NwO35 3CEQ== X-Gm-Message-State: AOAM532q13oolM9ULL+8Ea4NfE6CqnEXxxTuSlD/FE4fVI/seOEYc5/5 8zNtSQuZDBf9yNfjYSSNvVgzdlhXq3M= X-Google-Smtp-Source: ABdhPJyCR5qtpf43g/hWh3HmfXuZLatgxAdk/CuvP96MPrLsK96J9LUrrEyfI5I7GHdfvjGJMLel3g== X-Received: by 2002:a17:906:180a:: with SMTP id v10mr28071821eje.112.1632138368671; Mon, 20 Sep 2021 04:46:08 -0700 (PDT) Received: from localhost.localdomain ([128.0.187.50]) by smtp.gmail.com with ESMTPSA id d9sm6846104ede.31.2021.09.20.04.46.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Sep 2021 04:46:08 -0700 (PDT) Subject: Re: jsonb Indexing From: ramikvl@gmail.com To: Julien Rouhaud Cc: "pgsql-generallists.postgresql.org" References: <3c162c26-e940-e49e-a465-6d484b0ca3a9@gmail.com> <8a721cb8-9f61-a09e-cae9-4050b5ff72f5@gmail.com> Message-ID: Date: Mon, 20 Sep 2021 13:46:07 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <8a721cb8-9f61-a09e-cae9-4050b5ff72f5@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 9/20/21 12:52 PM, 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? Strangely enough when I re-created the index it's working, now. I probably made a mistake. Thank you.