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 1mTAdl-0008Rg-E2 for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 22:22:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mTAdj-0008CL-FW for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 22:22:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTAdi-0008CB-C3 for pgsql-general@lists.postgresql.org; Wed, 22 Sep 2021 22:22:55 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mTAdd-0006p5-R0 for pgsql-general@postgresql.org; Wed, 22 Sep 2021 22:22:52 +0000 Received: by mail-pl1-x62d.google.com with SMTP id bb10so2751241plb.2 for ; Wed, 22 Sep 2021 15:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alaska.edu; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=qE5Y9uPWPYp2UknBWJYKGae3BJ3LAIC6pBst1kBtz9E=; b=CtlEjQ1Eyu1rGvH/1qj5znc/27KydX9Huc8sr5/abfHqOhrALeRXfADp0nfIO4EPDN jfksF2qFCJ7GhSh8zn8bHQYgz4pSqHGp7n7YarJD5UpSmYut6FpUm8axYtlODLIysdEn KmcE6O/pFWdsgvsH9rHaHNcMeGVCGqt8RP58iKjHDgPvRF7fFLuigzmSIJ28+P6RnMga a+liGusJ7ABXWd8Fl2pXt8yqaV8FN2yxtcsoQdxvUBEUM2rDdCs5N7x6cX0IoLRu8o/f 4S2YywY3L01wS+mdt4gBaoyU/Czs7n5PH5l92endf46Ypw9vko8jrnGRLF+QrHnEEuwX j8tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=qE5Y9uPWPYp2UknBWJYKGae3BJ3LAIC6pBst1kBtz9E=; b=iox5dmHzrU+cUHnt3jpsaBPmFAlGRXaIPAm0ni9AZ5PaR8raQG8UOtSZOEFMtaLNSF 6TtSnXtKdv06/2vNpa5DLgYS/ileUSBRy2N7dhZ0l4cHMTHKVXhBAT0bfQWVBPeV5muq CkSTI5VWwWwqI5ydPK+7CI3FLmPWb62VtNK/YL7qjo8FxSoVd36VJeDHseuqBbsCJXyi jprY33mcZXggPzzQCEuU7Hum8A/zRJAp1bLhEEj/idVH7hHEGaFcvWlic9b9Z0d0umdc 52vuCnZ6Uqys8H4+IwsdyE108H5rhZL359zveAap64vMFd9+l2BlG9kHLtq85mYt90SC nPvw== X-Gm-Message-State: AOAM5309SqHmD3JcWgAl7HwpS+FnXhf82yjzU54RLvFhYIxFnVFZR9Me T02c3Yry3XpEQCzGiDvKJk1hq9PB2ZYl8g== X-Google-Smtp-Source: ABdhPJyfmm+7jijHjDefIrXVb0oYbYQXtQghCW3raowFxDvtAE7vWCuOxzveQpet9IyP9zp9EYdGCQ== X-Received: by 2002:a17:90b:791:: with SMTP id l17mr1459264pjz.155.1632349368664; Wed, 22 Sep 2021 15:22:48 -0700 (PDT) Received: from [10.27.81.2] (191-92-174-206.gci.net. [206.174.92.191]) by smtp.gmail.com with ESMTPSA id w13sm7008803pjc.29.2021.09.22.15.22.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Sep 2021 15:22:48 -0700 (PDT) From: Israel Brewster Message-Id: <5E650CD6-AF86-48E7-BC2B-EB3A6C01D8F6@alaska.edu> Content-Type: multipart/alternative; boundary="Apple-Mail=_EF7C5469-4D07-4E5E-A21E-97693AFEB6F8" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: Faster distinct query? Date: Wed, 22 Sep 2021 14:22:47 -0800 In-Reply-To: Cc: PostgreSQL Mailing Lists To: Ryan Booz References: <0BB059BF-54BD-46FA-8B26-28FEF9A04198@alaska.edu> X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_EF7C5469-4D07-4E5E-A21E-97693AFEB6F8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Sep 22, 2021, at 2:05 PM, Ryan Booz wrote: >=20 > Ah. I didn't realize that. If SkipScan was chosen, you'd actually see = it as one of the execution nodes. I also realize I was making a few = assumptions about your data, are channels shared among stations, or are = all channels unique (like an ID) per station? That would impact the = index and approach. Ok, that may be a good point: =E2=80=9Cchannel=E2=80=9D is currently a = varchar column, containing something like =E2=80=98BHZ=E2=80=99, = =E2=80=98EHZ=E2=80=99, =E2=80=98BHE=E2=80=99 etc. There are only a = handful of possible channels that I am currently aware of, which are = shared among stations - most stations have a =E2=80=98BHZ=E2=80=99 = channel, for example. That would be fairly simple to normalize out if = that would help. >=20 > Something like: >=20 > station | channel > ----------|----------- > 1 1 > 1 2 > 2 3 > 2 4 >=20 > or: > station | channel > ----------|----------- > 1 1 > 1 2 > 2 1 > 2 2 >=20 >=20 >=20 >=20 > On Wed, Sep 22, 2021 at 5:53 PM Israel Brewster > wrote: >> On Sep 22, 2021, at 1:50 PM, Ryan Booz > wrote: >>=20 >> Cool. I'd be interested to see the explain on it if you ever try it = again. On that cardinality, I'd expect it to be really fast, so I'm = interested to see if the (SkipScan) nodes were actually used. >=20 > With timescaledb extension installed, the explain is what I posted in = the original message (https://explain.depesz.com/s/mtxB#html = ). Without timescaledb = installed, the explain looks the same, except it takes twice as long to = run. >=20 > Unless I missed something in your message, i.e. some sort of tweak to = the query to get it to use the timescaledb features? >=20 > --- > Israel Brewster > Software Engineer > Alaska Volcano Observatory=20 > Geophysical Institute - UAF=20 > 2156 Koyukuk Drive=20 > Fairbanks AK 99775-7320 > Work: 907-474-5172 > cell: 907-328-9145 >=20 >>=20 >> On Wed, Sep 22, 2021 at 5:35 PM Israel Brewster = > wrote: >>=20 >>> On Sep 22, 2021, at 12:49 PM, Ryan Booz > wrote: >>>=20 >>> [Timescale Dev Advocate here] >>> I realize this might not be the most accepted answer (could be = interpreted as trying to "sell" something), but feels like an = opportunity to talk about DISTINCT queries and opportunities. Because = you have that index, Timescale 2.3 added a "Skip Scan" query planner = node that works on regular BTree indexes (it doesn't have to be = time-series/TimescaleDB Hypertable data at all). In this case, your = distinct query would likely run in a few milliseconds based on the = counts you mention (170 stations, 3 channels per station), and then the = outer aggregation would do the GROUP BY. So, you **could** add the = TimescaleDB extension to your database (or a copy of) and give it a try. = You don't actually need to use any TimescaleDB features otherwise. >>=20 >> I had actually already done that, as I was considering, in spite of = past negative experiences with timescaledb, experimenting with it on = this DB to see if it worked any better with this data. Out of curiosity, = I tried removing the timescaledb extension, whereupon the query in = question took roughly twice as long. So you are right that installing = timescaledb speeds things up, even when not using any timescaledb = specific functions. So that was a good call. Thanks! >>=20 >> --- >> Israel Brewster >> Software Engineer >> Alaska Volcano Observatory=20 >> Geophysical Institute - UAF=20 >> 2156 Koyukuk Drive=20 >> Fairbanks AK 99775-7320 >> Work: 907-474-5172 >> cell: 907-328-9145 >>=20 >>>=20 >>> A writeup of why this kind of DISTINCT query is slow in PostgreSQL = (for now) and what we did to overcome it: = https://blog.timescale.com/blog/how-we-made-distinct-queries-up-to-8000x-f= aster-on-postgresql/ = >>> Plans for a similar feature in PostgreSQL proper that we'd totally = support but hasn't made forward progress yet: = https://commitfest.postgresql.org/19/1741/ = >>> Anyway, it might be worth a shot. HTH >>>=20 >>> Ryan B >>>=20 >>> On Wed, Sep 22, 2021 at 4:27 PM David G. Johnston = > wrote: >>> On Wed, Sep 22, 2021 at 1:21 PM Michael Lewis > wrote: >>> In the future, please share the plan returned by explain analyze, = and some data about how many rows in the involved tables, >>>=20 >>> I believe we consider it acceptable to link to an explain viewer, = which is what the OP did. Reading explain output in email has its own = challenges, and I'd rather have the website than a text attachment. >>>=20 >>>=20 >>> How does the below work? It should do a very simple index scan only, = then aggregate the relative few rows after the fact. >>>=20 >>> select station, array_agg(distinct(channel)) as channels >>> FROM( >>> SELECT station,channel FROM data GROUP BY station,channel >>> ) AS sub >>> group by station; >>>=20 >>> Yeah, am pondering this too, though seems like the queries should be = identical so the plan/execution should be the same either way. >>>=20 >>>=20 >>> If there is correlation between station & channel, then you might = look at creating a multivariate statistics object and analyzing the = table so the planner can make better choices >>>=20 >>> There is no where clause so I'm doubtful there is much to be gained = going down this path. The Index-Only scan seems like an optimal way to = obtain this data and the existing query already does that. The = aggregation path might vary though it seems like that shouldn't be the = case here. >>>=20 >>> David J. >>=20 >=20 --Apple-Mail=_EF7C5469-4D07-4E5E-A21E-97693AFEB6F8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On = Sep 22, 2021, at 2:05 PM, Ryan Booz <ryan@timescale.com> = wrote:

Ah. I didn't realize that. If SkipScan was chosen, you'd = actually see it as one of the execution nodes. I also realize I was = making a few assumptions about your data, are channels shared among = stations, or are all channels unique (like an ID) per station? That = would impact the index and approach.

Ok, that may be a good point: =E2=80=9Cchannel=E2=80= =9D is currently a varchar column, containing something like =E2=80=98BHZ=E2= =80=99, =E2=80=98EHZ=E2=80=99, =E2=80=98BHE=E2=80=99 etc. There are only = a handful of possible channels that I am currently aware of, which are = shared among stations - most stations have a =E2=80=98BHZ=E2=80=99 = channel, for example. That would be fairly simple to normalize out if = that would help.


Something like:

station | = channel
----------|-----------
1            1
1            = 2
2          =   3
2        =     4

or:
station | channel
----------|-----------
1        =     1
1            2
2    =         1
2        =     2




On Wed, Sep = 22, 2021 at 5:53 PM Israel Brewster <ijbrewster@alaska.edu> wrote:
On Sep 22, 2021, at 1:50 PM, Ryan Booz <ryan@timescale.com> wrote:

Cool. I'd be interested to see = the explain on it if you ever try it again. On that cardinality, I'd = expect it to be really fast, so I'm interested to see if the (SkipScan) = nodes were actually used.

With timescaledb extension installed, = the explain is what I posted in the original message (https://explain.depesz.com/s/mtxB#html). Without = timescaledb installed, the explain looks the same, except it takes twice = as long to run.

Unless I missed something in your message, i.e. some sort of = tweak to the query to get it to use the timescaledb features?

---
Israel Brewster
Software= Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk = Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell: =  907-328-9145


On Wed, Sep 22, 2021 at 5:35 PM Israel = Brewster <ijbrewster@alaska.edu> wrote:

On Sep 22, 2021, at 12:49 PM, Ryan Booz <ryan@timescale.com> wrote:

[Timescale Dev = Advocate here]
I realize this might not be the most = accepted answer (could be interpreted as trying to "sell" something), = but feels like an opportunity to talk about DISTINCT queries and = opportunities. Because you have that index, Timescale 2.3 added a = "Skip Scan" query planner node that works on regular BTree indexes (it = doesn't have to be time-series/TimescaleDB Hypertable data at all). In = this case, your distinct query would likely run in a few milliseconds = based on the counts you mention (170 stations, 3 channels per station), = and then the outer aggregation would do the GROUP BY. So, you **could** = add the TimescaleDB extension to your database (or a copy of) and give = it a try. You don't actually need to use any TimescaleDB features = otherwise.

I had actually already = done that, as I was considering, in spite of past negative experiences = with timescaledb, experimenting with it on this DB to see if it worked = any better with this data. Out of curiosity, I tried removing the = timescaledb extension, whereupon the query in question took roughly = twice as long. So you are right that installing timescaledb speeds = things up, even when not using any timescaledb specific functions. So = that was a good call. Thanks!

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical = Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: = 907-474-5172
cell:  907-328-9145


Anyway, it might be worth a shot. HTH

Ryan B

On Wed, Sep 22, 2021 at 4:27 PM David G. Johnston = <david.g.johnston@gmail.com> wrote:
On Wed, Sep = 22, 2021 at 1:21 PM Michael Lewis <mlewis@entrata.com> wrote:
In the future, please share the plan returned by = explain analyze, and some data about how many rows in the involved = tables,

I believe we consider it acceptable to link to an explain = viewer, which is what the OP did.  Reading explain output in email = has its own challenges, and I'd rather have the website than a text = attachment.


How does the below work? It should = do a very simple index scan only, then aggregate the relative few rows = after the fact.

select = station, array_agg(distinct(channel)) as channels
FROM(
SELECT station,channel FROM data = GROUP BY station,channel
) AS sub
group by station;

Yeah, am = pondering this too, though seems like the queries should be identical so = the plan/execution should be the same either way.


If= there is correlation between station & channel, then you might look = at creating a multivariate statistics object and analyzing the table so = the planner can make better choices

There is no where clause so I'm doubtful there is much to be = gained going down this path.  The Index-Only scan seems like an = optimal way to obtain this data and the existing query already does = that.  The aggregation path might vary though it seems like that = shouldn't be the case here.

David J.



= --Apple-Mail=_EF7C5469-4D07-4E5E-A21E-97693AFEB6F8--