This commit is contained in:
Aliaksandr Valialkin 2024-05-15 03:23:33 +02:00
parent fcce0fc6e1
commit ff2b6fbe35
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 28 additions and 0 deletions

View file

@ -36,6 +36,7 @@ func ProcessQueryRequest(ctx context.Context, w http.ResponseWriter, r *http.Req
if limit > 0 {
q.AddPipeLimit(uint64(limit))
}
q.Optimize()
tenantIDs := []logstorage.TenantID{tenantID}

View file

@ -215,6 +215,33 @@ func (q *Query) AddPipeLimit(n uint64) {
})
}
// Optimize tries optimizing the query.
func (q *Query) Optimize() {
q.pipes = optimizeSortLimitPipes(q.pipes)
}
func optimizeSortLimitPipes(pipes []pipe) []pipe {
// Merge 'sort ... | limit ...' into 'sort ... limit ...'
i := 1
for i < len(pipes) {
pl, ok := pipes[i].(*pipeLimit)
if !ok {
i++
continue
}
ps, ok := pipes[i-1].(*pipeSort)
if !ok {
i++
continue
}
if ps.limit == 0 || pl.n < ps.limit {
ps.limit = pl.n
}
pipes = append(pipes[:i], pipes[i+1:]...)
}
return pipes
}
func (q *Query) getNeededColumns() ([]string, []string) {
neededFields := newFieldsSet()
neededFields.add("*")