From 7b8d2033d0eedf3e8af038184c86df7559f4d04c Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Sun, 4 Oct 2020 13:54:15 +0300 Subject: [PATCH] Remove unnecessary ServiceName index seek if tags query is available Signed-off-by: Michael Burman --- plugin/storage/badger/spanstore/reader.go | 27 ++++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/plugin/storage/badger/spanstore/reader.go b/plugin/storage/badger/spanstore/reader.go index 134da6e44c2..4571497bf9a 100644 --- a/plugin/storage/badger/spanstore/reader.go +++ b/plugin/storage/badger/spanstore/reader.go @@ -264,23 +264,28 @@ func setQueryDefaults(query *spanstore.TraceQueryParameters) { func serviceQueries(query *spanstore.TraceQueryParameters, indexSeeks [][]byte) [][]byte { if query.ServiceName != "" { indexSearchKey := make([]byte, 0, 64) // 64 is a magic guess + tagQueryUsed := false + for k, v := range query.Tags { + tagSearch := []byte(query.ServiceName + k + v) + tagSearchKey := make([]byte, 0, len(tagSearch)+1) + tagSearchKey = append(tagSearchKey, tagIndexKey) + tagSearchKey = append(tagSearchKey, tagSearch...) + indexSeeks = append(indexSeeks, tagSearchKey) + tagQueryUsed = true + } + if query.OperationName != "" { indexSearchKey = append(indexSearchKey, operationNameIndexKey) indexSearchKey = append(indexSearchKey, []byte(query.ServiceName+query.OperationName)...) } else { - indexSearchKey = append(indexSearchKey, serviceNameIndexKey) - indexSearchKey = append(indexSearchKey, []byte(query.ServiceName)...) + if !tagQueryUsed { // Tag query already reduces the search set with a serviceName + indexSearchKey = append(indexSearchKey, serviceNameIndexKey) + indexSearchKey = append(indexSearchKey, []byte(query.ServiceName)...) + } } - indexSeeks = append(indexSeeks, indexSearchKey) - if len(query.Tags) > 0 { - for k, v := range query.Tags { - tagSearch := []byte(query.ServiceName + k + v) - tagSearchKey := make([]byte, 0, len(tagSearch)+1) - tagSearchKey = append(tagSearchKey, tagIndexKey) - tagSearchKey = append(tagSearchKey, tagSearch...) - indexSeeks = append(indexSeeks, tagSearchKey) - } + if len(indexSearchKey) > 0 { + indexSeeks = append(indexSeeks, indexSearchKey) } } return indexSeeks