From a6e2818085a8c9097a2a1251d88f0d80533106b6 Mon Sep 17 00:00:00 2001 From: Rebecca Taft Date: Fri, 28 Apr 2023 09:11:31 -0500 Subject: [PATCH] sql/stats: collect automatic table statistics on the system.jobs table Fixes #102213 Release note (performance improvement): We now automatically collect table statistics on the system.jobs table, which will enable the optimizer to produce better query plans for internal queries that access the system.jobs table. This may result in better performance of the system. --- pkg/jobs/utils.go | 2 +- pkg/sql/stats/automatic_stats.go | 4 ++-- pkg/sql/stats/automatic_stats_test.go | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/jobs/utils.go b/pkg/jobs/utils.go index 7af25f6420a5..80bc909cc221 100644 --- a/pkg/jobs/utils.go +++ b/pkg/jobs/utils.go @@ -59,7 +59,7 @@ func RunningJobExists( SELECT id FROM - system.jobs@jobs_status_created_idx + system.jobs WHERE job_type IN ` + typeStrs + ` AND status IN ` + NonTerminalStatusTupleString + ` diff --git a/pkg/sql/stats/automatic_stats.go b/pkg/sql/stats/automatic_stats.go index 005f91217b48..5ae5dcf1b021 100644 --- a/pkg/sql/stats/automatic_stats.go +++ b/pkg/sql/stats/automatic_stats.go @@ -568,7 +568,7 @@ FROM AS OF SYSTEM TIME '-%s' WHERE tbl.database_name IS NOT NULL - AND tbl.table_id NOT IN (%d, %d, %d, %d) -- excluded system tables + AND tbl.table_id NOT IN (%d, %d, %d) -- excluded system tables AND tbl.drop_time IS NULL AND ( crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', d.descriptor, false)->'table'->>'viewQuery' @@ -652,7 +652,7 @@ func (r *Refresher) ensureAllTables( getAllTablesQuery := fmt.Sprintf( getAllTablesTemplateSQL, initialTableCollectionDelay, - keys.TableStatisticsTableID, keys.LeaseTableID, keys.JobsTableID, keys.ScheduledJobsTableID, + keys.TableStatisticsTableID, keys.LeaseTableID, keys.ScheduledJobsTableID, autoStatsPredicate, systemTablesPredicate, ) r.getApplicableTables(ctx, getAllTablesQuery, diff --git a/pkg/sql/stats/automatic_stats_test.go b/pkg/sql/stats/automatic_stats_test.go index 568eb3273557..424b2f101133 100644 --- a/pkg/sql/stats/automatic_stats_test.go +++ b/pkg/sql/stats/automatic_stats_test.go @@ -201,8 +201,8 @@ func TestEnsureAllTablesQueries(t *testing.T) { require.NoError(t, cache.Start(ctx, keys.SystemSQLCodec, s.RangeFeedFactory().(*rangefeed.Factory))) r := MakeRefresher(s.AmbientCtx(), st, executor, cache, time.Microsecond /* asOfTime */) - // Exclude the 4 system tables which don't use autostats. - systemTablesWithStats := bootstrap.NumSystemTablesForSystemTenant - 4 + // Exclude the 3 system tables which don't use autostats. + systemTablesWithStats := bootstrap.NumSystemTablesForSystemTenant - 3 numUserTablesWithStats := 2 // This now includes 36 system tables as well as the 2 created above. @@ -280,7 +280,7 @@ func checkAllTablesCount(ctx context.Context, systemTables bool, expected int, r getAllTablesQuery := fmt.Sprintf( getAllTablesTemplateSQL, collectionDelay, - keys.TableStatisticsTableID, keys.LeaseTableID, keys.JobsTableID, keys.ScheduledJobsTableID, + keys.TableStatisticsTableID, keys.LeaseTableID, keys.ScheduledJobsTableID, autoStatsEnabledOrNotSpecifiedPredicate, systemTablesPredicate, ) r.getApplicableTables(ctx, getAllTablesQuery, @@ -297,7 +297,7 @@ func checkExplicitlyEnabledTablesCount(ctx context.Context, expected int, r *Ref getTablesWithAutoStatsExplicitlyEnabledQuery := fmt.Sprintf( getAllTablesTemplateSQL, collectionDelay, - keys.TableStatisticsTableID, keys.LeaseTableID, keys.JobsTableID, keys.ScheduledJobsTableID, + keys.TableStatisticsTableID, keys.LeaseTableID, keys.ScheduledJobsTableID, explicitlyEnabledTablesPredicate, autoStatsOnSystemTablesEnabledPredicate, ) r.getApplicableTables(ctx, getTablesWithAutoStatsExplicitlyEnabledQuery,