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 1mSGuy-0001vY-5F for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 10:53:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSGux-0003M8-4H for pgsql-general@arkaria.postgresql.org; Mon, 20 Sep 2021 10:52:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSGuw-0003M0-RW for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 10:52:58 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mSGuu-00076v-Kx for pgsql-general@lists.postgresql.org; Mon, 20 Sep 2021 10:52:58 +0000 Received: by mail-ed1-x52b.google.com with SMTP id h17so59720001edj.6 for ; Mon, 20 Sep 2021 03:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=IohRfTAiNi4wI0ijbSmsaCxuAels60uZabIU5ZLJbCk=; b=ROCv3GwmLpg9HWhSz4TK5D8fTXdawgpUmxtiHDBsYfseK7TAmM9lUJX38/nDkyJjeE mcaAvN0RVsWEHI2fBj0fDWM4dNgBMFVH3itNaLRdM6RMauP6wVDXWVY3UeTNaZNFmT5G CUAkNzDVw9eftrSXKTd6tv4/1yNpRLbWbNzKpVGYJj0xEHRzYCe58ypS8q9N9qvmVttw g4pqbgzHyMv+ZlZeWHxrnMfYtTlG+05qmekr5cbyxJ04fnr4sX8AeFJiWAXIXVkWzBpi OfUSEFsnEe2piYGfZ3StGEN9XNqzWKoYSgfcyc/eHuvW3LruimUcJ4Z9pOFLE8Ff/Z9q G98Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=IohRfTAiNi4wI0ijbSmsaCxuAels60uZabIU5ZLJbCk=; b=xwjXHcjOmIeRWPGElLJVEHQSVcSob9yygv3oggF6vuAbId2l9IZJK+p2nXIM5iG7bq XZKYshOMuGO4+fmtJHceSLEW4NzrQqYoMHPALf0En4/sRxbEpCvgGxrkPndM1jsl8ULd 15BjBnmqdfvlGnBR2tLmMDE606ZbiVNSpEZGkXd7PR5aYKsksigWP8DfyyRjDfIP+Z8l ofAh4ye39PIi7G4QbIefQl9sUXgenbQ322fngsYwrPL6dARPmMyVNwaEKkvJdwBW6DNt P+CFMUBZOjo4ECav8uneNfnQ/v/SaBa1TD5n8an5xFc+FGnw0YVRCWeUAt4Mc/p5YCSV td1g== X-Gm-Message-State: AOAM532+vQYnwnXNMyIHbPULD+uw/BR31cVt3tLjwGC3rJxNylvkn9No oDnxC9UMYMNoE1re/t1H5t0NhpMy9Z0= X-Google-Smtp-Source: ABdhPJzrMD3zh9y1qVDRDsfYVTentfC2fm3bbDbtGN8uY/mEex+YY95PKqHMVN3xaf8HD77hcvuL5g== X-Received: by 2002:a50:e188:: with SMTP id k8mr7515926edl.119.1632135175765; Mon, 20 Sep 2021 03:52:55 -0700 (PDT) Received: from localhost.localdomain ([128.0.187.50]) by smtp.gmail.com with ESMTPSA id i6sm5904949ejd.57.2021.09.20.03.52.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Sep 2021 03:52:55 -0700 (PDT) Subject: Re: jsonb Indexing To: Julien Rouhaud Cc: "pgsql-generallists.postgresql.org" References: <3c162c26-e940-e49e-a465-6d484b0ca3a9@gmail.com> From: ramikvl@gmail.com Message-ID: <8a721cb8-9f61-a09e-cae9-4050b5ff72f5@gmail.com> Date: Mon, 20 Sep 2021 12:52:54 +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: 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 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?