Skip to content

Conversation

@dimitarvdimitrov
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov commented Nov 19, 2025

In draft until an initial review from me

What this PR does

The exhaustive planner generates all 2^N possible plans for N matchers, which becomes prohibitively expensive for queries with many matchers.

This change implements a branch-and-bound algorithm that:

  • Explores the plan space using a priority queue ordered by lower bound cost
  • Prunes branches that cannot lead to better plans than the current best
  • Uses an int to track decided predicates instead of copying boolean slices

For undecided predicates, the lower bound calculation:

  • Treats the first undecided predicate as an index predicate
  • Treats all other undecided predicates as minimal-cost scan predicates

Benchmarks

The tradeoff is that for smaller plans the new exploration is more expensive

Details

goos: darwin
goarch: arm64
pkg: github.com/grafana/mimir/pkg/ingester/lookupplan
cpu: Apple M1 Pro
                                                                                                                        │    before.txt    │               after.txt               │
                                                                                                                        │      sec/op      │    sec/op      vs base                │
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name-10                                                                1.171µ ± 10%    2.227µ ±  3%   +90.18% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name-10                                                                 997.6n ± 24%   2426.5n ± 19%  +143.22% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name#01-10                                                              107.4µ ±  4%    221.6µ ±  9%  +106.35% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_low_cardinality_label_name-10                                                          19.66µ ±  3%    40.23µ ±  1%  +104.59% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/specific_low_cardinality_label_high_cardinality_metric_name-10                                20.59µ ±  4%    42.16µ ±  2%  +104.78% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_prefix_matcher-10                                                      38.55µ ±  3%    78.02µ ±  1%  +102.40% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher-10                                                             1.120µ ±  4%    2.242µ ±  2%  +100.27% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher#01-10                                                          1.073µ ±  0%    2.171µ ±  1%  +102.42% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_label-10                                                                         936.4n ±  0%   1982.0n ± 14%  +111.66% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matchers-10                                                            1.836µ ±  0%    4.206µ ± 13%  +129.08% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/higher_cardinality_regex_prefix_and_exact_matcher-10                                          1.958µ ±  1%    4.306µ ± 13%  +119.89% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/negative_exact_matcher-10                                                                     1.107µ ±  1%    2.378µ ± 22%  +114.87% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/empty_matchers-10                                                                             1.936µ ±  5%    4.373µ ± 12%  +125.85% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/exact_non_existent_metric_high_cardinality_method-10                                          1.819µ ±  0%    3.406µ ±  2%   +87.27% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_set_matcher-10                                                         1.157µ ±  0%    2.320µ ±  2%  +100.56% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_negative_metchers-10                                                         1.834µ ±  1%    4.014µ ±  1%  +118.93% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_negative_matchers-10                                                3.735µ ±  1%    8.044µ ±  3%  +115.40% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_wildcard_regex_matchers-10                                          8.161µ ±  7%   19.136µ ±  1%  +134.49% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers-10                                                   7.168µ ±  1%   16.632µ ±  0%  +132.04% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_mixed_matcher_types-10                                                       7.326µ ±  1%   17.122µ ±  0%  +133.72% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method-10                           110.1µ ±  1%    217.0µ ±  2%   +97.05% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric-10                                      107.8µ ±  0%    214.5µ ±  1%   +98.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_with_exact_matcher-10                                                  39.02µ ±  0%    79.53µ ±  0%  +103.84% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_matcher-10                                                 39.07µ ±  1%    79.67µ ±  0%  +103.90% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_high_cardinality_exact_matchers-10                                                   1.965µ ±  0%    4.222µ ±  4%  +114.91% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_exact_matchers-10                                                           3.649µ ±  4%    7.910µ ±  0%  +116.79% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_exact_matcher-10                                                   214.4µ ±  2%    423.5µ ±  1%   +97.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_negative_empty_metric_name-10                                   108.0µ ±  6%    215.5µ ±  0%   +99.49% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_specific_metric-10                                              108.2µ ±  3%    215.1µ ±  0%   +98.83% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_pattern_with_exact_matcher-10                                          40.36µ ±  0%    81.84µ ±  0%  +102.76% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_mixed_cardinality_labels-10                                      3.774µ ±  0%    8.223µ ±  1%  +117.89% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_with_regex_and_exact_matchers-10                                            219.5µ ±  1%    437.6µ ±  0%   +99.39% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_and_exact_matchers-10                                      44.33µ ±  0%    91.82µ ±  0%  +107.15% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_high_cardinality_matchers-10                                       219.5µ ±  0%    437.7µ ±  2%   +99.39% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers-10                                     264.8µ ±  2%    556.8µ ±  8%  +110.24% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/conflicting_instance_matchers_exact_wins-10                                                   338.2µ ±  1%    683.1µ ± 12%  +101.96% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_regex_in_a_regular_query-10                                                      291.3µ ±  1%    516.2µ ±  3%   +77.20% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_regexp_in_a_regular_query-10                                            284.7µ ±  0%    574.0µ ±  2%  +101.58% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_in_a_regular_query-10                                                            282.9µ ±  2%    504.9µ ±  3%   +78.49% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_in_a_regular_query-10                                                   282.9µ ±  1%    564.5µ ±  1%   +99.51% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_regex_in_a_regular_query-10                                                282.4µ ±  0%    555.8µ ±  2%   +96.82% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_set_value_regex_in_a_regular_query-10                                            283.3µ ±  7%    507.3µ ±  2%   +79.08% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_regexp_in_a_regular_query-10                                      283.0µ ±  0%    565.6µ ±  2%   +99.88% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_in_a_regular_query-10                                                      283.4µ ±  1%    509.8µ ±  1%   +79.85% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_in_a_regular_query-10                                             285.4µ ±  1%    563.3µ ±  1%   +97.35% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_with_empty_matcher-10             333.3µ ±  1%    653.7µ ±  5%   +96.14% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_matching_a_lot_of_values-10       329.6µ ±  0%    667.7µ ±  3%  +102.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars-10                                                 221.2µ ±  0%    443.7µ ±  2%  +100.58% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars_and_a_negative_matcher-10                          221.1µ ±  0%    437.3µ ±  2%   +97.80% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact_high_cardinality-10                                             1356988.7µ ±  3%    315.0µ ±  1%   -99.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_mixed_types_with_low_cardinality-10                                       1411861.6µ ±  0%    393.9µ ±  4%   -99.97% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_regex_patterns-10                                                     1348604.9µ ±  0%    608.7µ ±  0%   -99.95% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_exact-10                                                      1387.204m ±  5%    4.384m ±  3%   -99.68% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact-10                                                              1345294.4µ ±  0%    315.0µ ±  2%   -99.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_regex-10                                                      1410.001m ±  0%    4.985m ±  2%   -99.65% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_non_existent_labels-10                                               1419173.4µ ±  0%    334.0µ ±  0%   -99.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_half_existent_half_nonexistent_exact-10                                   1391492.1µ ±  0%    314.1µ ±  1%   -99.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_expensive_regex_patterns-10                                               1329730.5µ ±  0%    638.0µ ±  1%   -99.95% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_empty_values-10                                                       1352.524m ±  1%    4.662m ±  4%   -99.66% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_mostly_empty_values-10                                                1346.290m ±  1%    4.668m ±  3%   -99.65% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_duplicate_label_low_cardinality_wins-10                                   2873901.0µ ±  3%    742.3µ ±  4%   -99.97% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name_with_sharding-10                                                  1.133µ ±  3%    2.241µ ±  2%   +97.79% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher_with_sharding-10                                               1.154µ ±  5%    2.248µ ±  1%   +94.76% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers_with_sharding-10                                     7.196µ ±  0%   16.693µ ±  4%  +131.99% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method_with_sharding-10             109.5µ ±  0%    216.6µ ±  0%   +97.79% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers_with_sharding-10                       264.8µ ±  0%    531.7µ ±  1%  +100.83% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/edge_case_low_cardinality_with_high_shard_count-10                                            1.141µ ±  1%    2.325µ ±  9%  +103.72% (p=0.002 n=6)
geomean                                                                                                                       174.9µ          84.67µ         -51.60%

                                                                                                                        │    before.txt     │               after.txt                │
                                                                                                                        │       B/op        │     B/op       vs base                 │
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name-10                                                                  615.0 ±  0%     2091.0 ± 0%   +240.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name-10                                                                   612.0 ±  1%     2087.0 ± 0%   +241.01% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name#01-10                                                                658.0 ±  9%     2098.0 ± 1%   +218.84% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_low_cardinality_label_name-10                                                            656.0 ±  5%     2083.0 ± 0%   +217.53% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/specific_low_cardinality_label_high_cardinality_metric_name-10                                  788.5 ±  4%     2892.0 ± 0%   +266.77% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_prefix_matcher-10                                                        642.0 ±  8%     2050.0 ± 0%   +219.31% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher-10                                                               618.0 ±  0%     2091.0 ± 0%   +238.35% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher#01-10                                                            616.0 ±  1%     2091.0 ± 0%   +239.45% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_label-10                                                                           608.5 ±  1%     2095.0 ± 0%   +244.29% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matchers-10                                                              739.0 ±  1%     3494.5 ± 0%   +372.87% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/higher_cardinality_regex_prefix_and_exact_matcher-10                                            728.0 ±  1%     3462.0 ± 0%   +375.55% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/negative_exact_matcher-10                                                                       617.0 ±  1%     2090.5 ± 0%   +238.82% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/empty_matchers-10                                                                               712.5 ±  1%     3411.0 ± 0%   +378.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/exact_non_existent_metric_high_cardinality_method-10                                            740.0 ±  1%     2903.0 ± 0%   +292.30% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_set_matcher-10                                                           670.0 ±  0%     2187.0 ± 0%   +226.42% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_negative_metchers-10                                                           739.5 ±  1%     3499.0 ± 0%   +373.16% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_negative_matchers-10                                                  858.5 ±  1%     6069.5 ± 0%   +606.99% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_wildcard_regex_matchers-10                                          2.050Ki ±  1%   12.688Ki ± 0%   +518.81% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers-10                                                     975.0 ±  1%    10927.0 ± 0%  +1020.72% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_mixed_matcher_types-10                                                        1012.5 ±  1%    10983.0 ± 0%   +984.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method-10                             898.0 ±  3%     4292.0 ± 1%   +377.95% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric-10                                        751.0 ±  7%     2907.0 ± 1%   +287.08% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_with_exact_matcher-10                                                    795.0 ±  4%     3452.0 ± 0%   +334.21% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_matcher-10                                                   775.0 ±  3%     3452.0 ± 0%   +345.42% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_high_cardinality_exact_matchers-10                                                     743.0 ±  1%     3495.5 ± 0%   +370.46% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_exact_matchers-10                                                             856.5 ±  1%     6073.5 ± 0%   +609.11% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_exact_matcher-10                                                     729.0 ±  7%     3492.0 ± 1%   +379.01% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_negative_empty_metric_name-10                                     762.0 ±  4%     3452.0 ± 1%   +353.02% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_specific_metric-10                                                778.0 ±  7%     3484.0 ± 0%   +347.81% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_pattern_with_exact_matcher-10                                          1.900Ki ±  2%    5.357Ki ± 0%   +181.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_mixed_cardinality_labels-10                                        861.0 ±  0%     6069.0 ± 0%   +604.88% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_with_regex_and_exact_matchers-10                                             1020.0 ±  5%    10907.0 ± 0%   +969.31% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_and_exact_matchers-10                                      1.036Ki ±  3%   10.050Ki ± 0%   +869.93% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_high_cardinality_matchers-10                                       1.006Ki ±  7%    9.445Ki ± 1%   +839.03% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers-10                                     1.097Ki ±  8%   19.879Ki ± 0%  +1712.69% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/conflicting_instance_matchers_exact_wins-10                                                   7.125Ki ±  2%   19.410Ki ± 0%   +172.42% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_regex_in_a_regular_query-10                                                      3.085Ki ±  4%    9.594Ki ± 1%   +210.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_regexp_in_a_regular_query-10                                            3.050Ki ±  5%   37.817Ki ± 0%  +1139.78% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_in_a_regular_query-10                                                            1.306Ki ±  9%    6.101Ki ± 1%   +367.28% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_in_a_regular_query-10                                                   1.369Ki ± 14%   31.983Ki ± 0%  +2236.85% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_regex_in_a_regular_query-10                                                1.291Ki ±  5%   27.511Ki ± 0%  +2030.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_set_value_regex_in_a_regular_query-10                                            1.322Ki ±  7%    6.195Ki ± 1%   +368.54% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_regexp_in_a_regular_query-10                                      1.291Ki ± 10%   32.015Ki ± 0%  +2380.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_in_a_regular_query-10                                                      1.306Ki ±  5%    6.101Ki ± 1%   +367.07% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_in_a_regular_query-10                                             1.306Ki ±  9%   32.015Ki ± 0%  +2351.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_with_empty_matcher-10             1.524Ki ±  5%    5.944Ki ± 1%   +289.94% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_matching_a_lot_of_values-10       7.034Ki ±  3%   16.337Ki ± 0%   +132.25% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars-10                                                 6.855Ki ±  1%   17.820Ki ± 0%   +159.94% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars_and_a_negative_matcher-10                          6.879Ki ±  3%   17.820Ki ± 0%   +159.05% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact_high_cardinality-10                                             426551.03Ki ±  0%    17.39Ki ± 0%   -100.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_mixed_types_with_low_cardinality-10                                       426555.52Ki ±  0%    33.19Ki ± 0%    -99.99% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_regex_patterns-10                                                     426554.82Ki ±  0%    33.56Ki ± 0%    -99.99% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_exact-10                                                       416.554Mi ±  0%    1.103Mi ± 0%    -99.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact-10                                                              426551.03Ki ±  0%    17.40Ki ± 0%   -100.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_regex-10                                                       416.561Mi ±  0%    1.117Mi ± 0%    -99.73% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_non_existent_labels-10                                               426553.10Ki ±  0%    24.99Ki ± 0%    -99.99% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_half_existent_half_nonexistent_exact-10                                   426551.03Ki ±  0%    17.40Ki ± 0%   -100.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_expensive_regex_patterns-10                                               426583.03Ki ±  0%    87.15Ki ± 0%    -99.98% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_empty_values-10                                                        416.553Mi ±  0%    1.102Mi ± 0%    -99.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_mostly_empty_values-10                                                 416.554Mi ±  0%    1.102Mi ± 0%    -99.74% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_duplicate_label_low_cardinality_wins-10                                   855689.93Ki ±  0%    23.31Ki ± 0%   -100.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name_with_sharding-10                                                    638.0 ±  1%     2109.0 ± 0%   +230.56% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher_with_sharding-10                                                 639.0 ±  1%     2107.5 ± 0%   +229.81% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers_with_sharding-10                                       994.5 ±  1%    10946.5 ± 0%  +1000.70% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method_with_sharding-10               954.5 ±  4%     4308.0 ± 1%   +351.34% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers_with_sharding-10                       1.142Ki ± 10%   19.895Ki ± 0%  +1641.94% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/edge_case_low_cardinality_with_high_shard_count-10                                              639.0 ±  0%     2108.5 ± 0%   +229.97% (p=0.002 n=6)
geomean                                                                                                                       10.75Ki          9.381Ki         -12.74%

                                                                                                                        │  before.txt   │              after.txt               │
                                                                                                                        │   allocs/op   │  allocs/op   vs base                 │
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name-10                                                               14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name-10                                                                14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_label_name#01-10                                                             14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_low_cardinality_label_name-10                                                         14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/specific_low_cardinality_label_high_cardinality_metric_name-10                               15.00 ± 0%    38.00 ± 0%   +153.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_prefix_matcher-10                                                     13.00 ± 0%    30.00 ± 0%   +130.77% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher-10                                                            14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher#01-10                                                         14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_label-10                                                                        14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matchers-10                                                           15.00 ± 0%    43.00 ± 0%   +186.67% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/higher_cardinality_regex_prefix_and_exact_matcher-10                                         14.00 ± 0%    41.00 ± 0%   +192.86% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/negative_exact_matcher-10                                                                    14.00 ± 0%    32.00 ± 0%   +128.57% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/empty_matchers-10                                                                            13.00 ± 0%    38.00 ± 0%   +192.31% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/exact_non_existent_metric_high_cardinality_method-10                                         15.00 ± 0%    38.00 ± 0%   +153.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_set_matcher-10                                                        15.00 ± 0%    34.00 ± 0%   +126.67% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_negative_metchers-10                                                        15.00 ± 0%    43.00 ± 0%   +186.67% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_negative_matchers-10                                               16.00 ± 0%    62.00 ± 0%   +287.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_wildcard_regex_matchers-10                                         26.00 ± 0%   114.00 ± 0%   +338.46% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers-10                                                  17.00 ± 0%    96.00 ± 0%   +464.71% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_mixed_matcher_types-10                                                      17.00 ± 0%    96.00 ± 0%   +464.71% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method-10                          16.00 ± 0%    48.00 ± 0%   +200.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric-10                                     15.00 ± 0%    38.00 ± 0%   +153.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_with_exact_matcher-10                                                 14.00 ± 0%    41.00 ± 0%   +192.86% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_matcher-10                                                14.00 ± 0%    41.00 ± 0%   +192.86% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_high_cardinality_exact_matchers-10                                                  15.00 ± 0%    43.00 ± 0%   +186.67% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_exact_matchers-10                                                          16.00 ± 0%    62.00 ± 0%   +287.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_exact_matcher-10                                                  14.00 ± 0%    41.00 ± 0%   +192.86% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_negative_empty_metric_name-10                                  14.00 ± 0%    40.00 ± 0%   +185.71% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_specific_metric-10                                             15.00 ± 0%    42.00 ± 0%   +180.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_regex_pattern_with_exact_matcher-10                                         29.00 ± 0%    71.00 ± 0%   +144.83% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_mixed_cardinality_labels-10                                     16.00 ± 0%    62.00 ± 0%   +287.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/mixed_cardinality_with_regex_and_exact_matchers-10                                           16.00 ± 0%    94.00 ± 0%   +487.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_metric_with_regex_and_exact_matchers-10                                     16.00 ± 0%    90.00 ± 0%   +462.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_regex_with_high_cardinality_matchers-10                                      15.00 ± 0%    84.00 ± 0%   +460.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers-10                                    16.00 ± 0%   158.00 ± 0%   +887.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/conflicting_instance_matchers_exact_wins-10                                                  96.00 ± 0%   229.00 ± 0%   +138.54% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_regex_in_a_regular_query-10                                                     41.00 ± 0%   106.00 ± 0%   +158.54% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_regexp_in_a_regular_query-10                                           41.00 ± 0%   308.00 ± 0%   +651.22% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_in_a_regular_query-10                                                           17.00 ± 0%    58.00 ± 0%   +241.18% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_negative_in_a_regular_query-10                                                  17.00 ± 0%   244.00 ± 0%  +1335.29% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_regex_in_a_regular_query-10                                               16.00 ± 0%   210.00 ± 0%  +1212.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_set_value_regex_in_a_regular_query-10                                           18.00 ± 0%    60.00 ± 0%   +233.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_regexp_in_a_regular_query-10                                     16.00 ± 0%   242.00 ± 0%  +1412.50% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_in_a_regular_query-10                                                     17.00 ± 0%    58.00 ± 0%   +241.18% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/non_existent_value_negative_in_a_regular_query-10                                            17.00 ± 0%   244.00 ± 0%  +1335.29% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_with_empty_matcher-10            21.00 ± 0%    62.00 ± 0%   +195.24% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/multiple_instance_matchers_one_with_low_cardinality_another_matching_a_lot_of_values-10      95.00 ± 0%   206.00 ± 0%   +116.84% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars-10                                                94.00 ± 0%   218.00 ± 0%   +131.91% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/expensive_alternating_regex_with_dot_stars_and_a_negative_matcher-10                         94.00 ± 0%   218.00 ± 0%   +131.91% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact_high_cardinality-10                                             110466.0 ± 0%    147.0 ± 0%    -99.87% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_mixed_types_with_low_cardinality-10                                       110526.0 ± 0%    317.0 ± 0%    -99.71% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_regex_patterns-10                                                     110513.0 ± 0%    304.0 ± 0%    -99.72% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_exact-10                                                     110.469k ± 0%   5.202k ± 0%    -95.29% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_exact-10                                                              110466.0 ± 0%    148.0 ± 0%    -99.87% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_all_negative_regex-10                                                     110.562k ± 0%   5.391k ± 0%    -95.12% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_non_existent_labels-10                                               110491.0 ± 0%    223.0 ± 0%    -99.80% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_half_existent_half_nonexistent_exact-10                                   110466.0 ± 0%    147.0 ± 0%    -99.87% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_expensive_regex_patterns-10                                               110.892k ± 0%   1.041k ± 0%    -99.06% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_empty_values-10                                                      110.447k ± 0%   5.160k ± 0%    -95.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_with_mostly_empty_values-10                                               110.448k ± 0%   5.162k ± 0%    -95.33% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/twenty_matchers_duplicate_label_low_cardinality_wins-10                                   233146.0 ± 0%    221.0 ± 0%    -99.91% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/simple_existing_metric_name_with_sharding-10                                                 15.00 ± 0%    33.00 ± 0%   +120.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/high_cardinality_exact_matcher_with_sharding-10                                              15.00 ± 0%    33.00 ± 0%   +120.00% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/metric_name_with_multiple_exact_matchers_with_sharding-10                                    18.00 ± 0%    97.00 ± 0%   +438.89% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/low_cardinality_instance_with_high_cardinality_metric_and_method_with_sharding-10            17.00 ± 0%    49.00 ± 0%   +188.24% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/complex_high_cardinality_metric_with_multiple_matchers_with_sharding-10                      17.00 ± 0%   159.00 ± 0%   +835.29% (p=0.002 n=6)
CostBasedPlannerPlanIndexLookup/edge_case_low_cardinality_with_high_shard_count-10                                           15.00 ± 0%    33.00 ± 0%   +120.00% (p=0.002 n=6)
geomean                                                                                                                      89.00         99.58         +11.88%

Which issue(s) this PR fixes or relates to

closes #11921

based on the changes from #13572

Checklist

  • Tests updated.
  • Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]. If changelog entry is not needed, please add the changelog-not-needed label to the PR.
  • about-versioning.md updated with experimental features.

Note

Replaces exhaustive 2^N plan generation with a branch-and-bound priority-queue explorer, refactors planner to stream plans, and updates tracing, pooling, and tests (incl. large-matcher cases).

  • Planner:
    • Branch-and-Bound: Adds generatePlansBranchAndBound with a min-heap over plan lower-bound costs; prunes branches and merges remaining candidates (branch_and_bound.go).
    • Plan Streaming: Switches from materializing/sorting all plans to iterating (iter.Seq[plan]); selects first plan containing index matchers.
    • Bounds/Helpers: Introduces newIndexOnlyPlan(), hasAnyIndexPredicate(), and virtual predicate tweaks for non-zero minimal costs; adds GetPlans() slab-backed PQ; removes "too many matchers" early abort.
    • Tracing/Metrics: Refactors outcome recording to use iterators; emits top-K alternative plans; preserves query stats updates.
    • Disabled Planning: Adds planningDisabled(ctx) and context helper; returns input plan unchanged when disabled.
  • Tests & Data:
    • Replaces "too many matchers" test with TestCostBasedPlannerWithManyMatchers (1000 matchers); adds preservation and preference tests.
    • Updates error message assertions and expands planner_test_cases.csv with high-cardinality 20-matcher scenarios and sharded variants.

Written by Cursor Bugbot for commit 42ec853. This will update automatically on new commits. Configure here.

@dimitarvdimitrov dimitarvdimitrov requested a review from a team as a code owner November 19, 2025 11:12
@dimitarvdimitrov dimitarvdimitrov marked this pull request as draft November 19, 2025 11:12
@dimitarvdimitrov dimitarvdimitrov force-pushed the dimitar/index-planning/branch-and-bound-proper branch 3 times, most recently from 958ba9d to 9c471e6 Compare November 19, 2025 14:19
@dimitarvdimitrov dimitarvdimitrov force-pushed the dimitar/index-planning/branch-and-bound-proper branch 2 times, most recently from 9a9da0c to 142f2ad Compare November 19, 2025 14:50
@dimitarvdimitrov dimitarvdimitrov changed the base branch from main to dimitar/index-planning/add-virtual-predicates November 19, 2025 14:50
@dimitarvdimitrov dimitarvdimitrov added the changelog-not-needed PRs that don't need a CHANGELOG.md entry label Nov 19, 2025
@dimitarvdimitrov dimitarvdimitrov force-pushed the dimitar/index-planning/branch-and-bound-proper branch 2 times, most recently from 0c02b81 to fccd6f3 Compare November 19, 2025 15:18
This will help us test the branch and bound planner with more complex queries.

Signed-off-by: Dimitar Dimitrov <[email protected]>
Signed-off-by: Dimitar Dimitrov <[email protected]>
…d-bound

Remove the partialPlan struct and its duplicated cost calculation methods.
Instead, use the plan struct directly with its virtualPredicate() method
and numDecidedPredicates field to compute lower bound costs.

Changes:
- Removed partialPlan struct
- Removed all cost methods from partialPlan (now inherited from plan)
- Changed partialPlans heap type to plans
- Updated pools to use plan instead of partialPlan
- Branch-and-bound now uses plan directly with numDecidedPredicates tracking
- TotalCost() automatically computes lower bound when numDecidedPredicates < len(predicates)

This simplifies the code by eliminating duplication while maintaining the same behavior.

Signed-off-by: Dimitar Dimitrov <[email protected]>
@dimitarvdimitrov dimitarvdimitrov force-pushed the dimitar/index-planning/branch-and-bound-proper branch from fccd6f3 to 17d3132 Compare November 19, 2025 15:34
Signed-off-by: Dimitar Dimitrov <[email protected]>
@dimitarvdimitrov dimitarvdimitrov marked this pull request as ready for review November 19, 2025 16:12
heap.Push(prospectPlans, newPlanWithCost(current.plan, current.numDecidedPredicates+1))
}

// Fall back to index-only plan to ensure that our code doesn't choose a more expensive plan than the naive plan.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use the cost of the indexOnlyPlan as the starting bestCompleteCost to further narrow the possible number of generated plans/branches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it would 👍 let me do that now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 42ec853 ; this also changed the tests. but I checked and the costs of the old and the new plans is the same - 11.5, 0, 0. So adding the index-only plan at the start just influences which equivalent plan with the same cost we choose, but they're all in theory equally cheap

Signed-off-by: Dimitar Dimitrov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-not-needed PRs that don't need a CHANGELOG.md entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

index planning: Optimize plan generation

2 participants