-
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
|
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
7986ec2 to
52e877c
Compare
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.
1729cba to
37f1fa3
Compare
| this.indexReferencesInQuery = originalPlan.referencedIndexCount(); | ||
| this.indexReferencesInPlan = optimizedPlan.referencedIndexCount(); | ||
| this.searchExecutedBeforeOrder = optimizedPlan.isSearchThenOrderHybrid(); | ||
| this.filterExecutedAfterOrderedScan = optimizedPlan.isOrderedScanThenFilterHybrid(); |
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.
indexReferencesInQuery and indexReferencesInPlan have associated histograms in TableQueryMetrics, but not counters. searchExecutedBeforeOrder and filterExecutedAfterOrderedScan have counters but not histograms. Is that intentional?
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 it breaks the consistency, I am wondering whether it is this way because:
- histograms for boolean values are not very useful
- for the others the index reference counts are small integers
Maybe we add some note to make clear the rationale? Wondering whether that would be helpful or it may become outdated doc in time
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.
As for booleans - I think nothing technically stops us from having a boolean histogram (but we'd have to convert it to 0 and 1 as currently histograms are based on longs); but I feel it wouldn't be very useful in this case. We get 100% of the same information from the per-table counters.
As for the other histograms - well, it is hard to represent them accurately as per table counters. Like, I feel it doesn't make much sense to sum all index references from all queries... So we have counters in per table for things which sum events naturally (like rows returned - it is quite easy to understand what a total number of rows returned is), but per-query histograms for other properties of queries which are not "summable" in that way.
Note there are many other histograms in perQuery which don't have (a) corresponding PerTable counter(s), so I don't think it breaks consistency. Anyway, we really need to expose those histograms in CNDB somehow, but let's live it for a followup ticket.
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.
Makes perfect sense to me, thanks.
src/java/org/apache/cassandra/index/sai/metrics/TableQueryMetrics.java
Outdated
Show resolved
Hide resolved
test/unit/org/apache/cassandra/index/sai/metrics/QueryMetricsTest.java
Outdated
Show resolved
Hide resolved
test/unit/org/apache/cassandra/index/sai/metrics/QueryMetricsTest.java
Outdated
Show resolved
Hide resolved
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.
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?
test/unit/org/apache/cassandra/index/sai/metrics/QueryMetricsTest.java
Outdated
Show resolved
Hide resolved
src/java/org/apache/cassandra/index/sai/metrics/TableQueryMetrics.java
Outdated
Show resolved
Hide resolved
| this.indexReferencesInQuery = originalPlan.referencedIndexCount(); | ||
| this.indexReferencesInPlan = optimizedPlan.referencedIndexCount(); | ||
| this.searchExecutedBeforeOrder = optimizedPlan.isSearchThenOrderHybrid(); | ||
| this.filterExecutedAfterOrderedScan = optimizedPlan.isOrderedScanThenFilterHybrid(); |
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 it breaks the consistency, I am wondering whether it is this way because:
- histograms for boolean values are not very useful
- for the others the index reference counts are small integers
Maybe we add some note to make clear the rationale? Wondering whether that would be helpful or it may become outdated doc in time
5307d54 to
18ee113
Compare
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
…ics.java Co-authored-by: Andrés de la Peña <[email protected]>
…ics.java Fix javadoc Co-authored-by: ekaterinadimitrova2 <[email protected]>
2f424cb to
765b18b
Compare
|
❌ Build ds-cassandra-pr-gate/PR-2130 rejected by Butler3 regressions found Found 3 new test failures
Found 3 known test failures |
| double logSelectivity = -Math.log10(queryPlanInfo.selectivityEstimated); | ||
| logSelectivityEstimated.update((int) (Math.min(20, Math.floor(logSelectivity)))); |
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 big deal, but Math.log10 is a relatively costly operation, and we'll call it seven times per query if per-kind metrics are enabled. Since this is the only read of QueryPlanInfo.selectivityEstimated, maybe we can put the logarithm of the selectivity in the plan info, as QueryPlanInfo.logSelectivityEstimated, so it's computed only once per query?
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.
Perhaps we could do the same for the quicker calls to Math.round for costEstimated, rowsToReturnEstimated, rowsToFetchEstimated and keysToIterateEstimated? Those will be repeated 14 times per query with per-query-kind metrics, so we can save up to 5*13=65 calls per query:
public final long costEstimated;
public final long rowsToReturnEstimated;
public final long rowsToFetchEstimated;
public final long keysToIterateEstimated;
public final int logSelectivityEstimated;
...
public PlanInfo(@Nonnull Plan.RowsIteration originalPlan, @Nonnull Plan.RowsIteration optimizedPlan)
{
this.costEstimated = Math.round(optimizedPlan.fullCost());
this.rowsToReturnEstimated = Math.round(optimizedPlan.expectedRows());
this.rowsToFetchEstimated = Math.round(optimizedPlan.estimatedRowsToFetch());
this.keysToIterateEstimated = Math.round(optimizedPlan.estimatedKeysToIterate());
this.logSelectivityEstimated = (Math.min(20, (int) Math.floor(-Math.log10(optimizedPlan.selectivity()))));That was the intention of having a QueryContext.Snapshot class, reducing the amplification caused by having multiple consumers of these metrics (per-query-kind metrics and slow query logger).



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.