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 1kTord-0008Ao-L4 for pgsql-sql@arkaria.postgresql.org; Sat, 17 Oct 2020 16:15:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kTorb-0007nW-Ta for pgsql-sql@arkaria.postgresql.org; Sat, 17 Oct 2020 16:15:23 +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 1kTorb-0007nP-LE for pgsql-sql@lists.postgresql.org; Sat, 17 Oct 2020 16:15:23 +0000 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kTorU-0006Cx-Iv for pgsql-sql@lists.postgresql.org; Sat, 17 Oct 2020 16:15:22 +0000 Received: by mail-io1-xd42.google.com with SMTP id q25so7811203ioh.4 for ; Sat, 17 Oct 2020 09:15:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=wmsW0T3gLmV744qZFkTG4ZMyU7L2NTCBYwUKjUpEXHU=; b=kGCOXOeeAveRfQoc1C3HRGyxc0k50IuZOITQUt8RATt5JuGZxBB2lr5OXOTnHO5uzg 58HLgwdGw5GHCwBbCwv+tKBKY1lrgATDYOfpIxs7X2mGY5oix4xyCLzzj0DAL/db5SaJ RdLvqryYbHy/Xzho0CkRY76h+G5WmOvutvgZ++Yv+6QHdFLT52KYLcUm0KNzV1PUt9A1 5CC7+jVdVA87JzBtLK9Hn4y3HWAbpxRznSFQEKiUbCN643t1lZcL0ey0yjyFaIp/G9Un UlcW9TONFr1PNf5yrjFaSjCA2t3jxpM8FUW2l2goy20dAKU/Up7hCJRsNolY+IId96YJ jn1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=wmsW0T3gLmV744qZFkTG4ZMyU7L2NTCBYwUKjUpEXHU=; b=ifePnEitrUHdnDyOU1/1tm9ggKDrRmiOMxQ9DKPr1SCHQ+G/v1EXDDwSL3OKj9+Nvh 2gqL27HsZnQOkk2xBdZLpDod0ZrjovCD5DfBpt3taZIJdGvNgxgm9//GRvh0kShxV86F fll+7tfmN66yaZ7Qeloqemrjqk8NA5i2HRxC7mhM/g6qoAMPCpDP3RTvPUfBf6iHsmNs tYPozmeohOcRvfIjxdJsOoNUAaft3DOk0bBMWGpq1ULIJYBSlWOeZVjDYhCwP37/yshl KKw6QnMlHtJWN8OK6N6HABNWhWQJQomkTMh98INXsE4PTek9eYdN/1a0W8KVjAtDsdtP PHBg== X-Gm-Message-State: AOAM533FmP3324vJm6GxAVmRq/eu0qnBf2jeyubpLOm0CMnQ6/QOdQ+x zEOGKClWcl8otCBRzacEMsrv1Eyd6YNRTw== X-Google-Smtp-Source: ABdhPJx2P2dd35BjOm1AZmYCJ8C8O+gR0mUQ5Hw26tBwxVMJVeNgHCYODnHFBrNYmU8zpWsiOyBVQw== X-Received: by 2002:a02:a317:: with SMTP id q23mr6161685jai.35.1602951314552; Sat, 17 Oct 2020 09:15:14 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:9168:af3c:9973:6f54? ([2601:681:5500:dde0:9168:af3c:9973:6f54]) by smtp.gmail.com with ESMTPSA id f85sm5692136ill.39.2020.10.17.09.15.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2020 09:15:13 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: get counts of multiple field values in a jsonb column From: Rob Sargent In-Reply-To: Date: Sat, 17 Oct 2020 10:15:12 -0600 Cc: pgsql-sql@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: =?utf-8?Q?Martin_Norb=C3=A4ck_Olivers?= X-Mailer: Apple Mail (2.3608.120.23.2.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Oct 17, 2020, at 9:00 AM, Martin Norb=C3=A4ck Olivers = wrote: >=20 > HI! > I'm using postgres to store unstructured fields in a jsonb column. I = also have a quite complicated query on the table, joining it with other = tables etc, and given that query I want to get a count of all the values = for a number of keys in the data. >=20 > Currently I'm doing one query for each key, like this > select data->>'field1', count(*) from COMPLICATED QUERY group by 1 > select data->>'field2', count(*) from COMPLICATED QUERY group by 1 > ... > select data->>'fieldN', count(*) from COMPLICATED QUERY group by 1 >=20 > field1, field2, ..., fieldN are known at query time. >=20 > But as the number of keys I want to count for increases, so does the = time it takes to run all these queries. I think the main problem is that = COMPLICATED QUERY is complicated and takes time to run each time. I = would very much like to run only one query that counts all the values of = all the fields, but I'm not quite sure how to do that. I'm looking at = all the aggregation functions but can't quite find one that suits this = purpose. >=20 > I would love to get some input on ways to make this faster. >=20 > Regards, >=20 > Martin If the =E2=80=9CCOMPLICATED QUERY=E2=80=9D is repeated verbatim each = time, run it once into a temp table and do the counts against the temp = table. create temp table complicate as COMPLICATED_QUERY; /*maybe index*/ select data->>'field1', count(*) from COMPLICATED QUERY group by 1 You can put the whole thing in a function and drop the temp table each = run. A CTE based on COMPLICATED_QUERY might work too.=