revert GROUP BY optimization

This commit is contained in:
Gani Georgiev
2026-01-18 18:38:40 +02:00
parent adb991eb02
commit 65750bca8d
2 changed files with 71 additions and 246 deletions

View File

@@ -186,30 +186,37 @@ func (r *RecordFieldResolver) updateQueryWithCollectionListRule(c *Collection, t
} }
func (r *RecordFieldResolver) updateQueryWithDeduplicateConstraint(query *dbx.SelectQuery) { func (r *RecordFieldResolver) updateQueryWithDeduplicateConstraint(query *dbx.SelectQuery) {
info := query.Info() query.Distinct(true)
if info.Distinct { // @todo Reasearch better options for generic rows deduplication.
return //
} // Disable the GROUP BY conditional checks for now since it prevents
// proper utilization of ORDER BY indexes (and maybe others)
// (https://github.com/pocketbase/pocketbase/discussions/7461)
// already has the group by registered // info := query.Info()
var groupByCol = r.baseCollection.Name // if info.Distinct {
if r.baseCollectionAlias != "" { // return
groupByCol = r.baseCollectionAlias // }
}
groupByCol += ".id"
if len(info.GroupBy) > 0 && info.GroupBy[0] == groupByCol {
return
}
// when deemed safe (GROUP BY could have different execution order compared to DISTINCT), // // already has the group by registered
// prefer GROUP BY to deduplicate only on the id field instead of all columns // var groupByCol = r.baseCollection.Name
// so that the size of a single row wouldn't matter that much // if r.baseCollectionAlias != "" {
if preferGroupBy(info, groupByCol) { // groupByCol = r.baseCollectionAlias
query.GroupBy(groupByCol) // }
} else { // groupByCol += ".id"
query.Distinct(true) // if len(info.GroupBy) > 0 && info.GroupBy[0] == groupByCol {
} // return
// }
// // when deemed safe (GROUP BY could have different execution order compared to DISTINCT),
// // prefer GROUP BY to deduplicate only on the id field instead of all columns
// // so that the size of a single row wouldn't matter that much
// if preferGroupBy(info, groupByCol) {
// query.GroupBy(groupByCol)
// } else {
// query.Distinct(true)
// }
} }
func preferGroupBy(info *dbx.QueryInfo, fullUnquotedGroupByCol string) bool { func preferGroupBy(info *dbx.QueryInfo, fullUnquotedGroupByCol string) bool {

File diff suppressed because one or more lines are too long