From f3fae7e2336b4edb997b93e12a9546c24e2dd977 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 29 Oct 2021 09:47:12 -0400 Subject: [PATCH] Optimize basic media_file query, avoiding adding "group by" or joining with genres if not required --- persistence/mediafile_repository.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 542d0e652..2886dd7e7 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -61,7 +61,18 @@ func (r *mediaFileRepository) Put(m *model.MediaFile) error { func (r *mediaFileRepository) selectMediaFile(options ...model.QueryOptions) SelectBuilder { sql := r.newSelectWithAnnotation("media_file.id", options...).Columns("media_file.*") sql = r.withBookmark(sql, "media_file.id") - return r.withGenres(sql).GroupBy("media_file.id") + if len(options) > 0 && options[0].Filters != nil { + s, _, _ := options[0].Filters.ToSql() + // If there's any reference of genre in the filter, joins with genre + if strings.Contains(s, "genre") { + sql = r.withGenres(sql) + // If there's no filter on genre_id, group the results by media_file.id + if !strings.Contains(s, "genre_id") { + sql = sql.GroupBy("media_file.id") + } + } + } + return sql } func (r *mediaFileRepository) Get(id string) (*model.MediaFile, error) {