Skip to content

Conversation

@pkolaczk
Copy link

This commit adds new metrics related to the operation of SAI query
planner. The metrics should help checking if the query planner makes
proper decisions by correlating them with the other metrics,
e.g. the metrics of the actual query execution.

Per-query metrics (histograms):

  • RowsEstimated: the estimated number of rows to be returned by
    the query
  • CostEstimated: the abstract cost of query execution
  • InverseSelectivityEstimated: the inverse of query selectivity,
    before applying the query LIMIT
    (1 means the query selects all rows, 10 means it
    selects every 10th row, etc.)
  • IndexReferencesInQuery: the number of index references in the
    unoptimized query execution plan (the same index may
    be referenced multiple times and counts separately)
  • IndexReferencesInPlan: the number of index references in the
    optimized query execution plan (the same index may
    be referenced multiple times and counts separately)

Per-table:

  • TotalRowsEstimated: counts the sum of all row estimates from
    all completed queries
  • TotalCostEstimated: counts the sum of all cost estimates from
    all completed queries
  • TotalQueriesCompletedInSelectivityGroup{N}, where N in [0, 12):
    counts the number of completed queries with selectivity S:
    10^(-N-1) < S <= 10^(-N) for N < 11,
    S <= 10^(-N) for N = 11
    In other words, the higher the group N, the smaller fraction of rows
    the query is estimated to return. The selectivity calculation
    does not include the final LIMIT of the query.

@github-actions
Copy link

github-actions bot commented Nov 17, 2025

Checklist before you submit for review

  • This PR adheres to the Definition of Done
  • Make sure there is a PR in the CNDB project updating the Converged Cassandra version
  • Use NoSpamLogger for log lines that may appear frequently in the logs
  • Verify test results on Butler
  • Test coverage for new/modified code is > 80%
  • Proper code formatting
  • Proper title for each commit staring with the project-issue number, like CNDB-1234
  • Each commit has a meaningful description
  • Each commit is not very long and contains related changes
  • Renames, moves and reformatting are in distinct commits
  • All new files should contain the DataStax copyright header instead of the Apache License one

@pkolaczk pkolaczk requested a review from adelapena November 17, 2025 19:33
@sonarqubecloud
Copy link

@cassci-bot
Copy link

❌ Build ds-cassandra-pr-gate/PR-2130 rejected by Butler


1 regressions found
See build details here


Found 1 new test failures

Test Explanation Runs Upstream
o.a.c.metrics.ClientRequestMetricsLatenciesTest.testViewWriteMetrics (compression) REGRESSION 🔴 0 / 17

Found 1 known test failures

Copy link

@ekaterinadimitrova2 ekaterinadimitrova2 left a comment

Choose a reason for hiding this comment

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

Not a full review, just started looking into this, just dropped a few quick comments

public final Timer annGraphSearchLatency;

/** Query execution cost as estimated by the planner */
public final Histogram costEstimated;

Choose a reason for hiding this comment

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

we probably need some parameter to be able to enable/disable these in case we overload a metrics server

This commit adds new metrics related to the operation of SAI query
planner. The metrics should help checking if the query planner makes
proper decisions by correlating them with the other metrics,
e.g. the metrics of the actual query execution.

Per-query metrics (histograms):
- `RowsEstimated`: the estimated number of rows to be returned by
   the query
- `CostEstimated`: the abstract cost of query execution
- `LogSelectivityEstimated`: minus decimal logarithm of query
   selectivity,  before applying the query LIMIT (0 means the query
   selects all rows, 5 means it selects 10^(-5) = 0.00001 subset
   of rows)
- `IndexReferencesInQuery`: the number of index references in the
  unoptimized query execution plan (the same index may
  be referenced multiple times and counts separately)
- `IndexReferencesInPlan`: the number of index references in the
  optimized query execution plan (the same index may
  be referenced multiple times and counts separately)

Per-table:
- `TotalRowsEstimated`: counts the sum of all row estimates from
  all completed queries
- `TotalCostEstimated`: counts the sum of all cost estimates from
  all completed queries
@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from 7986ec2 to 52e877c Compare November 18, 2025 11:11
Comment on lines +328 to +330
if (originalPlan != null && optimizedPlan != null) {
queryPlanInfo = new QueryPlanInfo(originalPlan, optimizedPlan);
} else {

Choose a reason for hiding this comment

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

Nit: braces on new line

public final int indexReferencesInQuery;
public final int indexReferencesInPlan;

public QueryPlanInfo(@Nonnull Plan originalPlan, @Nonnull Plan optimizedPlan)

Choose a reason for hiding this comment

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

CNDB-15260 recently added SAI metrics to the log messages produced by the slow query logger. These messages contain most of the metrics that are tracked by QueryMonitorableExecutionInfo. The log messages also contain the query and the tree view of the query plan. I understand we don't need to add any of the metrics here to those log messages, because they don't add anything that is not already in the printed query and plan, is this correct?

This is how the log messages look like:

DEBUG [node1_isolatedExecutor:1] node1 2025-10-09 11:14:57,354 MonitoringTask.java:175 - 2 operations were slow in the last 131 msecs:
<SELECT * FROM distributed_test_keyspace.t WHERE (s = s_2 OR s = s_3) AND  ALLOW FILTERING>, time 111 msec - slow timeout 100 msec
  SAI slow query metrics:
    sstablesHit: 2
    segmentsHit: 2
    partitionsRead: 2
    rowsFiltered: 2
    rowsPreFiltered: 0
    trieSegmentsHit: 2
    bkdPostingListsHit: 0
    bkdSegmentsHit: 0
    bkdPostingsSkips: 0
    bkdPostingsDecodes: 0
    triePostingsSkips: 0
    triePostingsDecodes: 2
    annGraphSearchLatencyNanos: 0
    shadowedPrimaryKeyCount: 0
  SAI slow query plan:
    Limit 2147483647 (rows: 1.8, cost/row: 101.5, cost: 3000.0..3177.6)
     └─ Filter (s = s_2 OR s = s_3) (sel: 1.000000000) (rows: 1.8, cost/row: 101.5, cost: 3000.0..3177.6)
         └─ Fetch (rows: 1.8, cost/row: 101.5, cost: 3000.0..3177.6)
             └─ Union (keys: 1.8, cost/key: 0.1, cost: 3000.0..3000.2)
                 ├─ LiteralIndexScan of t_s_idx (sel: 0.250000000, step: 1.0) (keys: 1.0, cost/key: 0.1, cost: 1500.0..1500.1)
                 │  predicate: Expression{name: s, op: EQ, lower: (s_2, true), upper: (s_2, true), exclusions: []}
                 └─ LiteralIndexScan of t_s_idx (sel: 0.250000000, step: 1.0) (keys: 1.0, cost/key: 0.1, cost: 1500.0..1500.1)
                    predicate: Expression{name: s, op: EQ, lower: (s_3, true), upper: (s_3, true), exclusions: []}

// Null means the query execution order hasn't been decided yet.
private FilterSortOrder filterSortOrder = null;
private Plan originalPlan = null;

Choose a reason for hiding this comment

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

Nit: double blank line

Copy link

@ekaterinadimitrova2 ekaterinadimitrova2 left a comment

Choose a reason for hiding this comment

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

Looks great, left just some questions.
There are some GH problems today and CI fails. You will need to restart it to check the results.

// Determines the order of using indexes for filtering and sorting.
// Null means the query execution order hasn't been decided yet.
private FilterSortOrder filterSortOrder = null;
private Plan originalPlan = null;

Choose a reason for hiding this comment

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

Have you considered if storing only the metrics and not the full originalPlan and optimizedPlan may make more sense?

import org.apache.cassandra.db.filter.IndexHints;
import org.apache.cassandra.db.filter.RowFilter;
import org.apache.cassandra.index.sai.IndexContext;
import org.apache.cassandra.index.sai.QueryContext;

Choose a reason for hiding this comment

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

This is not used

{
Cost cost = cost();
if (!(cost instanceof RowsIterationCost))
throw new UnsupportedOperationException("Expected rows is only supported for plans returning rows (called on " + this.getClass() + ')');

Choose a reason for hiding this comment

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

While this is true, shouldn't we return some 0 value maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants