-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[doc][2025.1] auto analyze service #25188
Open
yifanguan
wants to merge
11
commits into
yugabyte:master
Choose a base branch
from
yifanguan:auto_analyze_doc_page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−31
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f57aac1
add dedicated page for auto analyze service
yifanguan 7ddaf9d
Edits
ddhodge 1eae383
Move to query tuning
ddhodge c254de6
format
ddhodge f136413
fix conflict
ddhodge 8388fd5
Merge branch 'master' into pr/yifanguan/25188
ddhodge 4c1ab51
Merge branch 'master' into auto_analyze_doc_page
ddhodge 17501a0
Merge branch 'master' into pr/yifanguan/25188
ddhodge a7551eb
add note on running analyze
ddhodge e67708a
edits
ddhodge 40923a5
misc edits
ddhodge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 89 additions & 0 deletions
89
docs/content/preview/explore/query-1-performance/auto-analyze.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: Auto Analyze service | ||
headerTitle: Auto Analyze service | ||
linkTitle: Auto Analyze | ||
description: Use the Auto Analyze service to keep table statistics up to date | ||
headcontent: Keep table statistics up to date automatically | ||
tags: | ||
feature: tech-preview | ||
menu: | ||
preview: | ||
identifier: auto_analyze | ||
parent: query-tuning | ||
weight: 700 | ||
type: docs | ||
--- | ||
|
||
To create optimal plans for queries, the query planner needs accurate and up-to-date statistics related to tables and their columns. These statistics are also used by the YugabyteDB [cost-based optimizer](../../../reference/configuration/yb-tserver/#yb-enable-base-scans-cost-model) (CBO) to create optimal execution plans for queries. To generate the statistics, you run the [ANALYZE](../../../api/ysql/the-sql-language/statements/cmd_analyze/) command. ANALYZE collects statistics about the contents of tables in the database, and stores the results in the `pg_statistic` system catalog. | ||
|
||
Similar to [PostgreSQL autovacuum](https://www.postgresql.org/docs/current/routine-vacuuming.html#AUTOVACUUM), the YugabyteDB Auto Analyze service automates the execution of ANALYZE commands for any table where rows have changed more than a configurable threshold for the table. This ensures table statistics are always up-to-date. | ||
|
||
## Enable Auto Analyze | ||
|
||
The Auto Analyze service is {{<tags/feature/tp>}}. Before you can use the feature, you must enable it by setting `ysql_enable_auto_analyze_service` to true on all YB-Masters, and both `ysql_enable_auto_analyze_service` and `ysql_enable_table_mutation_counter` to true on all YB-Tservers. | ||
|
||
For example, to create a single-node [yugabyted](../../../reference/configuration/yugabyted/) cluster with Auto Analyze enabled, use the following command: | ||
|
||
```sh | ||
./bin/yugabyted start --master_flags "ysql_enable_auto_analyze_service=true" --tserver_flags "ysql_enable_auto_analyze_service=true,ysql_enable_table_mutation_counter=true" | ||
``` | ||
|
||
To enable Auto Analyze on an existing cluster, a rolling restart is required to set `ysql_enable_auto_analyze_service` and `ysql_enable_table_mutation_counter` to true. | ||
|
||
## Configure Auto Analyze | ||
|
||
You can control how frequently the service updates table statistics using the following YB-TServer flags: | ||
|
||
- `ysql_auto_analyze_threshold` - the minimum number of mutations (INSERT, UPDATE, and DELETE) needed to run ANALYZE on a table. Default is 50. | ||
- `ysql_auto_analyze_scale_factor` - a fraction that determines when enough mutations have been accumulated to run ANALYZE for a table. Default is 0.1. | ||
|
||
Increasing either of these flags reduces the frequency of statistics updates. | ||
|
||
If the total number of mutations for a table is greater than its analyze threshold, then the service runs ANALYZE on the table. The analyze threshold of a table is calculated as follows: | ||
|
||
```sh | ||
analyze_threshold = ysql_auto_analyze_threshold + (ysql_auto_analyze_scale_factor * <table_size>) | ||
``` | ||
|
||
where `<table_size>` is the current `reltuples` column value stored in the `pg_class` catalog. | ||
|
||
`ysql_auto_analyze_threshold` is important for small tables. With default settings, if a table has 100 rows and 20 are mutated, ANALYZE won't run as the threshold is not met, even though 20% of the rows are mutated. | ||
|
||
On the other hand, `ysql_auto_analyze_scale_factor` is especially important for big tables. If a table has 1,000,000,000 rows, 10% (100,000,000 rows) would have to be mutated before ANALYZE runs. Set the scale factor to a lower value to allow for more frequent statistics collection for such large tables. | ||
|
||
In addition, `ysql_auto_analyze_batch_size` controls the maximum number of tables the Auto Analyze service tries to analyze in a single ANALYZE statement. The default is 10. Setting this flag to a larger value can potentially reduce the number of YSQL catalog cache refreshes if Auto Analyze decides to ANALYZE many tables in the same database at the same time. | ||
|
||
For more information on flags used to configure the Auto Analyze service, refer to [Auto Analyze service flags](../../../reference/configuration/yb-tserver#auto-analyze-service-flags). | ||
ddhodge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Example | ||
|
||
With Auto Analyze enabled, try the following SQL statements. | ||
|
||
```sql | ||
CREATE TABLE test (k INT PRIMARY KEY, v INT); | ||
SELECT reltuples FROM pg_class WHERE relname = 'test'; | ||
``` | ||
|
||
```output | ||
reltuples | ||
----------- | ||
-1 | ||
(1 row) | ||
``` | ||
|
||
```sql | ||
INSERT INTO test SELECT i, i FROM generate_series(1, 100) i; | ||
-- Wait for few seconds | ||
SELECT reltuples FROM pg_class WHERE relname = 'test'; | ||
``` | ||
|
||
```output | ||
reltuples | ||
----------- | ||
100 | ||
(1 row) | ||
``` | ||
|
||
## Limitations | ||
|
||
Because ANALYZE is a DDL statement, it can cause DDL conflicts when run concurrently with other DDL statements. As Auto Analyze runs ANALYZE in the background, you should turn off Auto Analyze if you want to execute DDL statements. You can do this by setting `ysql_enable_auto_analyze_service` to false on all YB-TServers at runtime. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added parenthesis is unnecessary IMO.