How to understand history-based query optimization
SUM
, AVG
, and COUNT
.
It then suggests indexes to reduce computational load during query processing.10
minutes in the form of a history-based statistics (HBS) snapshot.
Each engine has a dedicated HBS component that is always active, continuously generating snapshots for the engine to reference during query planning.
These snapshots are saved and shared among all engines running on the same database within the same account.
Consequently, an engine can benefit from the query history from another engine, improving performance across all engines without needing to restart the analysis process.
After engine restart, the engine retrieves the latest HBS snapshot.
EXPLAIN
with the statistics
option, as follows:
EXPLAIN
option statistics
annotates all nodes of the query plan with the sources of statistics used in its cost estimation.
An example of the EXPLAIN (statistics)
output below shows source: history
, and we know that the estimates were made based on history.
EXPLAIN (statistics)
, including various possible values that the source field can take, see Explain: Example with statistics.
The EXPLAIN(statistics)
output will not show source:history
during the first run of an alternate query plan if the optimizer finds that the initial query plan was not optimal. This absence occurs because the alternative plan has not yet gathered telemetry data during its initial run.
In the next execution of the same query, the optimizer understands that it should choose an alternative plan to avoid the slow execution.
This means, that history-based statistics are in effect, but we may not see history
shown in sources of the EXPLAIN (statistics)
output for the new run.
It could be that for the alternative plan we have not collected its execution telemetry yet.