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 1iMYc6-0001Fh-AF for pgsql-general@arkaria.postgresql.org; Mon, 21 Oct 2019 14:24:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iMYc5-0006ch-4R for pgsql-general@arkaria.postgresql.org; Mon, 21 Oct 2019 14:24:49 +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 1iMYc4-0006ca-RI for pgsql-general@lists.postgresql.org; Mon, 21 Oct 2019 14:24:48 +0000 Received: from mout.kundenserver.de ([212.227.17.13]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iMYc2-0008Ht-8l for pgsql-general@postgresql.org; Mon, 21 Oct 2019 14:24:48 +0000 Received: from [10.8.0.75] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.145]) with ESMTPSA (Nemesis) id 1N9doJ-1hz3dp3lYL-015Zpl for ; Mon, 21 Oct 2019 16:24:44 +0200 Subject: Re: Calling jsonb_array_elements 4 times in the same query To: pgsql-general References: From: Thomas Kellerer Message-ID: Date: Mon, 21 Oct 2019 16:24:43 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:QPB3tb0cLYVEs3dThwILiV5ndGe1cdgDuGijqwpUQ1qHwudp6Ux +sEOza5xpgKzLKB+9U76nrYZzCa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95Dcv3GvaF8lHwP44Z1H2N4Xs TW2Crf9YXq9r6z9aFUF/gNtd5wQ1LcQHgupnjSC2Q= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexander Farber schrieb am 21.10.2019 um 15:39: > I am trying to construct a query, which would draw a game board when given a move id (aka mid): > >     SELECT >         hand, >         JSONB_ARRAY_ELEMENTS(tiles)->'col' AS col, >         JSONB_ARRAY_ELEMENTS(tiles)->'row' AS row, >         JSONB_ARRAY_ELEMENTS(tiles)->'letter' AS letter, >         JSONB_ARRAY_ELEMENTS(tiles)->'value' AS value >     FROM words_moves >     WHERE action = 'play' AND >     gid = (SELECT gid FROM words_moves WHERE mid = 391416) >     AND played <= (SELECT played FROM words_moves WHERE WHERE mid = 391416) >     ORDER BY played DESC > > The above query works for me and fetches all moves performed in a game id (aka gid) up to the move id 391416. > > 1. Is it okay to call JSONB_ARRAY_ELEMENTS four times in the query, will PostgreSQL optimize that to a single call? Typically set returning functions should be used in the FROM clause, not the SELECT list: SELECT hand, t.tile -> 'col' AS col, t.tile -> 'row' AS row, t.tile -> 'letter' AS letter, t.tile -> 'value' AS value FROM words_moves cross join jsonb_array_elements(tiles) as t(tile) WHERE action = 'play' AND gid = (SELECT gid FROM words_moves WHERE mid = 391416) AND played <= (SELECT played FROM words_moves WHERE WHERE mid = 391416) ORDER BY played DESC