Slow postgresql query with distinct on subquery

I’ve been working on improving the query times for few common query patterns in Postgres. The table structure can be simplified to the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create table file_group (
id serial primary key,
tag varchar(64),
creation_time timestamp
);
</code>
<code>create table file_group ( id serial primary key, tag varchar(64), creation_time timestamp ); </code>
create table file_group (
  id serial primary key,
  tag varchar(64),
  creation_time timestamp
);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create table file (
id serial primary key,
file_group_id integer not null references file_group,
name text,
ordering int
);
</code>
<code>create table file ( id serial primary key, file_group_id integer not null references file_group, name text, ordering int ); </code>
create table file (
  id serial primary key,
  file_group_id integer not null references file_group,
  name text,
  ordering int
);

For a given file_group, we can have 0-N files referencing it. For all queries, I need to determine the most recent file_group for each unique tag value, and then retrieve the files associated with them. All of the common queries follow a structure similar to this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
select * from
(
select distinct on (file_group.tag) *
from file_group
where file_group.tag < 'C' -- dynamic user supplied filter
order by file_group.tag asc, file_group.creation_time desc
) fg
left join file on fg.id = file.file_group_id -- left join in case of 0 files in group
order by -- returned ordering of files is important
fg.tag asc,
fg.creation_time desc,
file.ordering asc;
</code>
<code> select * from ( select distinct on (file_group.tag) * from file_group where file_group.tag < 'C' -- dynamic user supplied filter order by file_group.tag asc, file_group.creation_time desc ) fg left join file on fg.id = file.file_group_id -- left join in case of 0 files in group order by -- returned ordering of files is important fg.tag asc, fg.creation_time desc, file.ordering asc; </code>

select * from
(
    select distinct on (file_group.tag) * 
    from file_group
    where file_group.tag < 'C'  -- dynamic user supplied filter
    order by file_group.tag asc, file_group.creation_time desc
) fg
left join file on fg.id = file.file_group_id  -- left join in case of 0 files in group
order by -- returned ordering of files is important
    fg.tag asc, 
    fg.creation_time desc,
    file.ordering asc;

I’ve tried a few different indexing approaches to improve the speed of this query pattern. Currently there is an index on file_group (tag, creation_time desc) which works well for the dynamic user supplied filters that are applied to the where clause.

However, I’m currently stuck on the distinct on subquery. When there are a large number of matching rows, the planner uses relatively slow nested loops to join the subquery and file table. I’ve been struggling to come up with a solution without subqueries using some window function or similar.

The desired output can be seen in this fiddle here.

Some other details

  • In reality the file_group table has multiple tag columns, previously this was stored in a single HSTORE column
  • The file_group table has ~50M records and the file table has ~60M. Most file groups have a single file in them, but some have 0 and some have many.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật