Skip to content

Commit

Permalink
sql/stats: generate statistics forecasts in the stats cache
Browse files Browse the repository at this point in the history
As of this commit, we now try to generate statistics forecasts for every
column of every table. This happens whenever statistics are loaded into
or refreshed in the stats cache. We use only the forecasts that fit the
historical collected statistics very well, meaning we have high
confidence in their accuracy.

Fixes: #79872

Release note (performance improvement): Enable table statistics
forecasts, which predict future statistics based on historical collected
statistics. Forecasts help the optimizer produce better plans for
queries that read data modified after the latest statistics collection.
We use only the forecasts that fit the historical collected statistics
very well, meaning we have high confidence in their accuracy. Forecasts
can be viewed using `SHOW STATISTICS FOR TABLE ... WITH FORECAST`.
  • Loading branch information
michae2 committed Aug 16, 2022
1 parent 32f38c5 commit b4420e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/sql/stats/automatic_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,14 @@ func checkStatsCount(
if err != nil {
return err
}
if len(stats) != expected {
return fmt.Errorf("expected %d stat(s) but found %d", expected, len(stats))
var count int
for i := range stats {
if stats[i].Name != jobspb.ForecastStatsName {
count++
}
}
if count != expected {
return fmt.Errorf("expected %d stat(s) but found %d", expected, count)
}
return nil
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/stats/stats_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,5 +720,8 @@ ORDER BY "createdAt" DESC, "columnIDs" DESC, "statisticID" DESC
return nil, err
}

forecasts := ForecastTableStatistics(ctx, statsList)
statsList = append(forecasts, statsList...)

return statsList, nil
}

0 comments on commit b4420e5

Please sign in to comment.