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 1mTImf-0004lx-K4 for pgsql-general@arkaria.postgresql.org; Thu, 23 Sep 2021 07:04:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mTIme-0000bz-Jq for pgsql-general@arkaria.postgresql.org; Thu, 23 Sep 2021 07:04:40 +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 1mTIme-0000bX-84 for pgsql-general@lists.postgresql.org; Thu, 23 Sep 2021 07:04:40 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTImc-0001xB-NB for pgsql-general@postgresql.org; Thu, 23 Sep 2021 07:04:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=2Z2v35srK/JyZMa11GH5LqDjy+WBwgLUTu4FgnPimrw=; b=hZ/UqvNSivmMavaHPyztgFWop1 6PvDtiXyilqVlL7Fd5YdLL3wBkg8As4AbzE9NeLH6K3iWGNogRIVkLOQTXayJn1Vm/6811mUicD8X CacvAfeY9BcglQ12t/6NM9n7B2XYeaMWmVsCo8e6wPZMpJ/x6UYfHP0QZDC3PhGCKgo0=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1mTIma-0005lV-0h; Thu, 23 Sep 2021 09:04:36 +0200 Date: Thu, 23 Sep 2021 09:04:35 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Israel Brewster Cc: PostgreSQL Mailing Lists Subject: Re: Faster distinct query? Message-ID: <20210923070435.GB8425@depesz.com> Reply-To: depesz@depesz.com References: <0BB059BF-54BD-46FA-8B26-28FEF9A04198@alaska.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0BB059BF-54BD-46FA-8B26-28FEF9A04198@alaska.edu> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Sep 22, 2021 at 12:05:22PM -0800, Israel Brewster wrote: > I was wondering if there was any way to improve the performance of this query: > > SELECT station,array_agg(distinct(channel)) as channels FROM data GROUP BY station; > > The explain execution plan can be found here: > https://explain.depesz.com/s/mtxB#html > > and it looks pretty straight forward. It does an index_only scan, followed by an aggregate, to produce a result that is a list of stations along with a list of channels associated with each (there can be anywhere from 1 to 3 channels associated with each station). This query takes around 5 minutes to run. > > To work around the issue, I created a materialized view that I can update periodically, and of course I can query said view in no time flat. However, I’m concerned that as the dataset grows, the time it takes to refresh the view will also grow (correct me if I am wrong there). > > This is running PostgreSQL 13, and the index referenced is a two-column index on data(station, channel) It looks that there is ~ 170 stations, and ~ 800 million rows int he table. can you tell us how many rows has this: select distinct station, channel from data; If this is not huge, then you can make the query run much faster using skip scan - recursive cte. Best regards, depesz