Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGiKd-00081c-K5 for pgsql-general@arkaria.postgresql.org; Sat, 05 Oct 2019 11:34:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iGiKc-0001TS-6l for pgsql-general@arkaria.postgresql.org; Sat, 05 Oct 2019 11:34:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGiKb-0001T3-Sq for pgsql-general@lists.postgresql.org; Sat, 05 Oct 2019 11:34:37 +0000 Received: from n3.nabble.com ([162.255.23.22]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iGiKX-0003Ez-RB for pgsql-general@postgresql.org; Sat, 05 Oct 2019 11:34:36 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 1DD8A17AE9D92 for ; Sat, 5 Oct 2019 04:34:31 -0700 (MST) Date: Sat, 5 Oct 2019 04:34:31 -0700 (MST) From: PegoraroF10 To: pgsql-general@postgresql.org Message-ID: <1570275271079-0.post@n3.nabble.com> In-Reply-To: <1570048895867-0.post@n3.nabble.com> References: <1570027063947-0.post@n3.nabble.com> <1570048895867-0.post@n3.nabble.com> Subject: Re: Performance on JSONB select MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I think I solved my problem. I didn=C2=B4t know it was possible but I=C2=B4= ve created an almost dynamic index, because it=C2=B4ll index for status and correspond= ing datevalue of an object with that status value.=20 Postgres, I love you. =20 Just one B-Tree index with 2 fields, Status and DateTime of that respective Status object. My Json has always a status and a respective object of that status with other values. So I know if it has that status it has a Date Value or not on its correspondent object.=20 { "status": "visitadescartada", "contrato": {}, "atribuido": {"datevalue": "2019-09-05 14:47:11.149095-03"}, "trabalhando": {}, "visitaagendada": {"datevalue": "2019-09-05 15:06:24.255548-03","caption": "Agendado"}, "visitadescartada": {"datevalue": "2019-09-12 11:47:17.45782-03","caption": "Desagendado"}, "digitacaodescartada": {} } create index IndexByStatusAndDate on MyTable ( (JsonBField->>$$status$$), castimmutabletimestamp(JsonBField->(JsonBField->>$$status$$)->>'datevalue')= )=20 where (JsonBField ? $$status$$); select * from MyTable where (JsonBField ? $$status$$) and case when JsonBField->>$$status$$=3D$$cadastrodescartado$$ then castimmutabletimestamp(JsonBField->(JsonBField->>$$status$$)->>'datevalue') > castimmutabletimestamp($$2019-10-01$$) when JsonBField->>$$status$$=3D$$visitadescartada$$ then castimmutabletimestamp(JsonBField->(JsonBField->>$$status$$)->>'datevalue') > castimmutabletimestamp($$2019-10-01$$) when JsonBField->>$$status$$=3D$$contrato$$ then castimmutabletimestamp(funilvendas->(JsonBField->>$$status$$)->>'data') > castimmutabletimestamp($$2019-10-01$$) when JsonBField->>$$status$$=3D$$naoatribuido$$ then True end; And performance now is great because I=C2=B4m using both fields on index. -- Sent from: https://www.postgresql-archive.org/PostgreSQL-general-f1843780.h= tml