From 442fcfec5a0d5e6a1d546d3e1d5871dd436125ff Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@gmail.com>
Date: Wed, 17 Feb 2021 15:00:08 +0200
Subject: [PATCH] lib/storage: tune the logic for sorting tag filters according
 the their exeuction times

---
 lib/storage/index_db.go | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go
index 51fde2806e..8357046493 100644
--- a/lib/storage/index_db.go
+++ b/lib/storage/index_db.go
@@ -2786,23 +2786,21 @@ func (is *indexSearch) getMetricIDsForDateAndFilters(date uint64, tfs *TagFilter
 	}
 	tfws := make([]tagFilterWithWeight, len(tfs.tfs))
 	ct := fasttime.UnixTimestamp()
+	hasDurationReset := false
 	for i := range tfs.tfs {
 		tf := &tfs.tfs[i]
 		durationSeconds, lastQueryTimestamp := is.getDurationAndTimestampForDateFilter(date, tf)
-		if ct > lastQueryTimestamp+60 {
-			// It is time to update filter duration stats.
-			if tf.isNegative || tf.isRegexp && len(tf.orSuffixes) == 0 {
-				// Negative and regexp filters usually take the most time, so move them to the end of filters
-				// in the hope they won't be executed at all.
-				if durationSeconds == 0 {
-					durationSeconds = 10
-				}
-			} else {
-				// Reset duration stats for relatively fast {key="value"} and {key=~"foo|bar|baz"} filters, so it is re-populated below.
-				if durationSeconds < 0.5 {
-					durationSeconds = 0
-				}
-			}
+		if durationSeconds == 0 && (tf.isNegative || tf.isRegexp && len(tf.orSuffixes) == 0) {
+			// Negative and regexp filters usually take the most time, so move them to the end of filters
+			// in the hope they won't be executed at all.
+			durationSeconds = 10
+			is.storeDurationAndTimestampForDateFilter(date, tf, durationSeconds, 0)
+		}
+		if !hasDurationReset && ct > lastQueryTimestamp+10*60 && durationSeconds < 0.2 {
+			// Reset duration stats for relatively fast filters, so it is re-populated below.
+			// Reset duration for a single filter at a time in order to reduce negative impact on query time.
+			durationSeconds = 0
+			hasDurationReset = true
 		}
 		tfws[i] = tagFilterWithWeight{
 			tf:                 tf,