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
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

@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from 7986ec2 to 52e877c Compare November 18, 2025 11:11
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.

@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from 1729cba to 37f1fa3 Compare November 21, 2025 15:16
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.

Overall looks great, I just left some tiny comments.
There is some issue with CI so it did not run.
Regarding testing - shouldn't we add some test for the new flag added - cassandra.sai.metrics.query_plan.enabled?

@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch 2 times, most recently from 2f424cb to 765b18b Compare November 26, 2025 20:01
@pkolaczk
Copy link
Author

pkolaczk commented Dec 8, 2025

@ekaterinadimitrova2

Regarding testing - shouldn't we add some test for the new flag added - cassandra.sai.metrics.query_plan.enabled?

Yes, added.

@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from 6e8682d to dab6384 Compare December 9, 2025 07:32
Copy link

@adelapena adelapena 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, +1

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.

About SAI_QUERY_PLAN_METRICS_ENABLED:

  • On the other PR there was a mention the new property may work also dynamically, during runtime. I think we need to add a test for that too, if that will be the expectation.

  • I left a comment that we need to reset to default value the property at the end of each test where we toggle it.

  • Thinking about enable/disable at runtime - we should think whether that would persist the metrics we already collected (in case we disable) or not. WDYT?

  • My main concern is that we report 0s instead of not registering the metrics at all once we disable them. That is something we discussed when we added a setting to be able to disable the IndexMetrics. More in the PR comments.

@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from a39c56f to dd2eb69 Compare December 10, 2025 18:03
@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from 47135d3 to ffa84a5 Compare December 15, 2025 12:27
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):
- `RowsToReturnEstimated`: the estimated number of rows to be returned
  by the query
- `RowsToFetchEstimated`: the estimated number of rows the query
  is going to fetch from storage
- `KeysToIterateEstimated`: the estimated number of primary keys to
  read from the indexes when executing the query to completion
- `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:
- `TotalRowsToReturnEstimated`: the sum of all estimates of returned
  rows from all completed queries
- `TotalRowsToFetchEstimated`: the sum of all estimates of fetched
  rows from all completed queries
- `TotalKeysToIterateEstimated`: the sum of all estimates of iterated
  primary keys from all completed queries
- `TotalCostEstimated`: counts the sum of all cost estimates from
  all completed queries

# Conflicts:
#	src/java/org/apache/cassandra/index/sai/QueryContext.java
@pkolaczk pkolaczk force-pushed the c15508-query-planner-metrics branch from ffa84a5 to af73a4f Compare December 15, 2025 12:33
@sonarqubecloud
Copy link

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