Continuously Executing ANALYZE TABLE for Several Days

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 持续执行ANALYZE TABLE好几天

| username: love-cat

[TiDB Usage Environment] Production Environment
[TiDB Version] 5.2.2
[Reproduction Path] Operations performed that led to the issue
[Encountered Issue: Issue Phenomenon and Impact]
Observed a slow log continuously executing a SQL statement for several days without completion:
ANALYZE TABLE thc_5001_democqzs.form_engine_template;

[Resource Configuration] Mixed resource deployment
[Attachment: Screenshot/Log/Monitoring]

| username: 我是咖啡哥 | Original post link

Is this an automatically executed job?
I’ve encountered this before, where the automatic execution for large tables keeps failing, but manual execution is quick. You might want to try running it manually to see.

| username: love-cat | Original post link

Okay, I’ll give it a try.

| username: 特雷西-迈克-格雷迪 | Original post link

Analyze table is generally very fast (even for tables with billions of rows) because analyze uses a sampling ratio, not a full table scan.

| username: tidb菜鸟一只 | Original post link

If the table is large, it is recommended to collect manually. You can use WITH FLOAT_NUM SAMPLERATE to analyze a small sample of data. If there are many large tables in the entire database and you do not want to incur too much overhead from collecting statistics, you can set tidb_enable_fast_analyze to 1 to enable the fast analyze feature. However, this is not recommended. Instead, you can increase the automatic threshold by adjusting tidb_auto_analyze_ratio and then deploy manual tasks to perform small sample analysis on large tables.

| username: h5n1 | Original post link

The automatic analyze is slow because it runs in a single thread. Is the data in this table changing frequently?

| username: love-cat | Original post link

Not frequently, we provide private deployment for our customers. After the initial MySQL synchronization to TiDB, it continuously executes analyze automatically.

| username: love-cat | Original post link

Okay, thanks, I’ll try this parameter.

| username: jansu-dev | Original post link

Hi, any updates?

  1. (Possible) auto_analyze now has a hardcoded concurrency of 1, which might be causing it to be very slow due to the large data volume.
  2. (Root Cause) The root cause is still unclear; the slow analyze is just a symptom. We need to analyze the table data volume and log information to understand why it’s slow.
  3. (Measure) tidb菜鸟一只 suggested a feasible method, but lowering the SAMPLERATE might lead to some instability in the execution plan.
  4. (Measure) h5n1 also suggested a feasible method. Manual analyze allows adjusting the concurrency parameters, which can be effective if the large data volume is causing it to run indefinitely. tidb_build_stats_concurrency and tidb_distsql_scan_concurrency and tidb_index_serial_scan_concurrency
| username: Raymond | Original post link

The automatic running of the large table keeps failing. Could you please tell me why it is failing?

| username: Raymond | Original post link

Excuse me, teacher, will the automatically executed analyze statement be controlled by the max_execution_time system variable? If the automatically executed analyze statement can be controlled by the max_execution_time system variable, then can we set the max_execution_time to an acceptable value?

| username: tidb菜鸟一只 | Original post link

Automatic running is done in a single thread, so it’s definitely slow, which is why collection fails.

| username: Raymond | Original post link

Could it be that the collection failed because it was slow? Shouldn’t it keep running? Is there some kind of timeout mechanism, or did something go wrong while it was running?

| username: tidb菜鸟一只 | Original post link

There is a window period, otherwise running continuously would affect your normal business.

| username: Raymond | Original post link

Did you achieve this window period through max_execution_time?

| username: tidb菜鸟一只 | Original post link

tidb_auto_analyze_start_time and tidb_auto_analyze_end_time specify the start and end times for automatic analysis. For large tables, it is not recommended to use automatic collection. Instead, you can deploy scheduled tasks and use multi-threaded small sample collection; otherwise, it will be impossible to complete the collection for very large tables.

| username: Raymond | Original post link

Got it, thank you, teacher.

| username: jansu-dev | Original post link

Actually, it is also controlled by this variable, with multiple conditions interfering with each other. What tidb newbie said is also correct.

| username: Raymond | Original post link

Okay, thank you.

| username: gcworkerishungry | Original post link

Check if the log shows the error “GC life time is shorter than transaction duration”? If so, it is likely a large table, and the analyze operation exceeded the GC life time. You can perform the analyze operation manually or create a crontab task to handle it.