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 1iMYrg-0001lS-J5 for pgsql-general@arkaria.postgresql.org; Mon, 21 Oct 2019 14:40:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iMYrf-0006Og-62 for pgsql-general@arkaria.postgresql.org; Mon, 21 Oct 2019 14:40:55 +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 1iMYrd-0006OF-GU for pgsql-general@lists.postgresql.org; Mon, 21 Oct 2019 14:40:54 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iMYrY-0008VL-LY for pgsql-general@postgresql.org; Mon, 21 Oct 2019 14:40:53 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 1CAAA542; Mon, 21 Oct 2019 10:40:27 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 21 Oct 2019 10:40:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=v SmIWezjWQkytSAzD4hLJdUXpDCtQ71pRK7rLw80UwQ=; b=tarC9zmWY5qQvd7ZN gCcb8T4Xflsg/lmK+Yr0ByhsE6uZUfBpj+fQ8p8kijH0a0/RAFb4b+yosEsOEObd 7/sNo9vbtxT/TftW6mT45JErztt76Sw8IsnpgZwY4GCjc0MMWQbqfAR+O5lyayST /tCnaiFxQ2YgD7qyCaXGaCRyX69eu3VSnEjeJqf4Pr35bnp6+K0hIKRmzEg+zSFS GQPAlA5bKSZfkiCU9THl/X5Foe7p0v1jx9yLe2gwtb3tQ2QghXsqkHwhj2i3mJrC LPfaA/q9z48PCr9qXHWNlnQ74px4s1icFf5j7/3voGEfULh6BMzipChxBsQoHWsn FYJTw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=vSmIWezjWQkytSAzD4hLJdUXpDCtQ71pRK7rLw80U wQ=; b=LpEp/E8Evi7+SB2ghXSBAll86WkxvoDM3z9cZc4P79T5T/UIH73TU0cz8 7dpi/Y08t27sZNliyNUQEkEojaPP7zsDKXQUSy5fhrGOG2/Qhavp23V+NV+PKAVA JlQQOdWcJ5Q+2R7LBLUhWctRsZIqh1dASywJl5OjGpTeeL6OixWFEEZH1kGRMwmy x5swb4iA0nRXQB5/hhaBJWILHGBBhDz+d1iYRjz4/potoet2UokdGFuxpdr0Qdvz t9g9FRnRDMSMFbXWY0yczVqw/ybweU5YHL4KlZnhXw41eiFwfI3Wd4XV9eaxr8/f 7pfAK1R8KSR3uiBw5IInC2ttfsN4A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrkeehgdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehslhhovhgrrdguvgenucfkphepudejgedrvddurdelhedrledt necurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrg hvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (174-21-95-90.tukw.qwest.net [174.21.95.90]) by mail.messagingengine.com (Postfix) with ESMTPA id 24294D6005A; Mon, 21 Oct 2019 10:40:26 -0400 (EDT) Subject: Re: Calling jsonb_array_elements 4 times in the same query To: Alexander Farber , pgsql-general References: From: Adrian Klaver Message-ID: Date: Mon, 21 Oct 2019 07:40:25 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/21/19 6:39 AM, Alexander Farber wrote: > Hello, good afternoon! > > With PostgreSQL 10 I host a word game, which stores player moves as a > JSON array of objects with properties: col, row, value, letter - > > CREATE TABLE words_moves ( >         mid     BIGSERIAL PRIMARY KEY, >         action  text NOT NULL, >         gid     integer NOT NULL REFERENCES words_games ON DELETE CASCADE, >         uid     integer NOT NULL REFERENCES words_users ON DELETE CASCADE, >         played  timestamptz NOT NULL, >         tiles   jsonb, >         letters text, >         hand    text, >         score   integer CHECK(score >= 0), >         puzzle  boolean NOT NULL DEFAULT false > ); > > 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. > > In my Java program I then just draw the tiles at the board, one by one > (here a picture: https://slova.de/game-62662/ ) > > I have however 3 questions please: > > 1. Is it okay to call JSONB_ARRAY_ELEMENTS four times in the query, will > PostgreSQL optimize that to a single call? What is the structure of the JSON in tiles? In other words could you expand the data in one go using jsonb_to_record()? > 2. Do you think if it is okay to sort by played timestamp or should I > better sort by mid? > 3. Performancewise is it okay to use the 2 subqueries for finding gid > and played when given a mid? I could see collapsing them into a single query: Something like: FROM words_moves JOIN (select gid, played from word_moves where mid = 39146) AS m_id ON word_moves.gid = m_id.gid WHERE ... > > Thank you > Alex > -- Adrian Klaver adrian.klaver@aklaver.com