From b4420e56b006bb98d932df1a1260fe2f996c2d60 Mon Sep 17 00:00:00 2001 From: Michael Erickson Date: Fri, 12 Aug 2022 21:02:41 -0700 Subject: [PATCH] sql/stats: generate statistics forecasts in the stats cache 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`. --- pkg/sql/stats/automatic_stats_test.go | 10 ++++++++-- pkg/sql/stats/stats_cache.go | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/sql/stats/automatic_stats_test.go b/pkg/sql/stats/automatic_stats_test.go index efb2ca0e9053..8b3d861abc58 100644 --- a/pkg/sql/stats/automatic_stats_test.go +++ b/pkg/sql/stats/automatic_stats_test.go @@ -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 }) diff --git a/pkg/sql/stats/stats_cache.go b/pkg/sql/stats/stats_cache.go index 1e9e1cc252d4..abe779ef7a1f 100644 --- a/pkg/sql/stats/stats_cache.go +++ b/pkg/sql/stats/stats_cache.go @@ -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 }