-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats: table-level setting to turn auto stats collection on/off
Fixes #40989 Previously, there was no way to enable or disable automatic statistics collection at the table level. It could only be turned on or off via the `sql.stats.automatic_collection.enabled` cluster setting. This was inadequate because statistics collection can be expensive for large tables, and it would be desirable to defer collection until after data is finished loading, or in off hours. Also, small tables which are frequently updated may trigger statistics collection leading to unnecessary overhead and/or unpredictable query plan changes. To address this, this patch adds support for setting of the following cluster settings at the table level: ``` sql.stats.automatic_collection.enabled sql.stats.automatic_collection.fraction_stale_rows sql.stats.automatic_collection.min_stale_rows ``` for example: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = true); ALTER TABLE t1 SET ("sql.stats.automatic_collection.fraction_stale_rows" = 0.1, "sql.stats.automatic_collection.min_stale_rows" = 2000); ``` The table-level setting takes precedence over the cluster setting. Release justification: Low risk fix for missing fine-grained control over automatic statistics collection. Release note (sql change): Automatic statistics collection can now be enabled or disabled for individual tables, taking precedence over the cluster setting, for example: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = true); ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = false); ALTER TABLE t1 RESET ("sql.stats.automatic_collection.enabled"); ``` RESET removes the setting value entirely, in which case the cluster setting of the same name is in effect for the table. Cluster settings `sql.stats.automatic_collection.fraction_stale_rows` and `sql.stats.automatic_collection.min_stale_rows` can now also be set at the table level, either at table creation time, or later, independent of whether auto stats is enabled: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.fraction_stale_rows" = 0.1, "sql.stats.automatic_collection.min_stale_rows" = 2000); CREATE TABLE t1 (a INT, b INT) WITH ("sql.stats.automatic_collection.enabled" = true, "sql.stats.automatic_collection.min_stale_rows" = 1000000, "sql.stats.automatic_collection.fraction_stale_rows" = 0.05 ); ``` Tables that have auto stats collection explicitly enabled or disabled may be discovered by querying system tables, for example, find all tables with auto stats enabled at the table level: ``` SELECT tbl.database_name || '.' || tbl.schema_name || '.' || tbl.name FROM crdb_internal.tables AS tbl INNER JOIN system.descriptor AS d ON d.id = tbl.table_id WHERE tbl.database_name IS NOT NULL AND tbl.database_name <> '%s' AND tbl.drop_time IS NULL AND crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', d.descriptor, false)->'table'->'clusterSettingsForTable' ->> 'sqlStatsAutomaticCollectionEnabled' = 'true'; ?column? -------------------- defaultdb.mws.t1 ```
- Loading branch information
Mark Sirek
committed
Mar 22, 2022
1 parent
9cdc505
commit 4db4555
Showing
16 changed files
with
1,003 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.