On Tue, Feb 26, 2019 at 01:54:00PM +0100, kimaidou wrote: > I manage to avoid the disk sort after performing a VACUUM ANALYSE; > And with a session work_mem = '250MB' > > * SQL http://paste.debian.net/1070207/ > * EXPLAIN https://explain.depesz.com/s/nJ2y > > It stills spent 16s > It seems this kind of query will need better hardware to scale... Once you've exhausted other ideas, you could consider making that a TEMPORARY TABLE, and creating an index on it (and analyzing it) and then aggregating. It'd be several separate queries. Justin