-
Notifications
You must be signed in to change notification settings - Fork 21
CNDB-15508: Query planner metrics #2130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Checklist before you submit for review
|
|
❌ Build ds-cassandra-pr-gate/PR-2130 rejected by Butler1 regressions found Found 1 new test failures
Found 1 known test failures |
ekaterinadimitrova2
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
7986ec2 to
52e877c
Compare
| if (originalPlan != null && optimizedPlan != null) { | ||
| queryPlanInfo = new QueryPlanInfo(originalPlan, optimizedPlan); | ||
| } else { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: double blank line
ekaterinadimitrova2
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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() + ')'); |
There was a problem hiding this comment.
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?



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 bythe query
CostEstimated: the abstract cost of query executionInverseSelectivityEstimated: 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 theunoptimized query execution plan (the same index may
be referenced multiple times and counts separately)
IndexReferencesInPlan: the number of index references in theoptimized query execution plan (the same index may
be referenced multiple times and counts separately)
Per-table:
TotalRowsEstimated: counts the sum of all row estimates fromall completed queries
TotalCostEstimated: counts the sum of all cost estimates fromall 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.