Skip to content

sql-statement-drop-stats: fix STATS_META output after DROP STATS - #21887

Open
doc-claw-bot wants to merge 1 commit into
pingcap:masterfrom
doc-claw-bot:docs-cn/fix-drop-stats-meta-example
Open

sql-statement-drop-stats: fix STATS_META output after DROP STATS#21887
doc-claw-bot wants to merge 1 commit into
pingcap:masterfrom
doc-claw-bot:docs-cn/fix-drop-stats-meta-example

Conversation

@doc-claw-bot

@doc-claw-bot doc-claw-bot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

What is changed, added or deleted? (Required)

  • 同步 pingcap/docs#23557 的修订,说明 DROP STATS 会清除直方图相关统计信息,但不会删除 STATS_META 对应行。
  • 更新示例输出,展示执行 DROP STATS 后,SHOW STATS_META 仍会返回 row count 和 modify count。
  • 使中文文档与当前 TiDB 实际行为保持一致。

Which TiDB version(s) do your changes apply to? (Required)

  • master (the latest development version)
  • v9.0 (TiDB 9.0 versions)
  • v8.5 (TiDB 8.5 versions)
  • v8.1 (TiDB 8.1 versions)
  • v7.5 (TiDB 7.5 versions)
  • v7.1 (TiDB 7.1 versions)
  • v6.5 (TiDB 6.5 versions)

What is the related PR or file link(s)?

AI agent involvement

  • The changes in this PR were primarily made by an AI agent on behalf of the PR author.

Do your changes match any of the following descriptions?

  • Delete files
  • Change aliases
  • Need modification after applied to another branch
  • Might cause conflicts after applied to another branch

@doc-claw-bot doc-claw-bot added the type/bugfix This PR fixes a bug. label Aug 18, 2026
@ti-chi-bot ti-chi-bot Bot added contribution This PR is from a community contributor. missing-translation-status This PR does not have translation status info. labels Aug 18, 2026
@doc-claw-bot doc-claw-bot added translation/from-docs This PR is translated from a PR in pingcap/docs. and removed missing-translation-status This PR does not have translation status info. labels Aug 18, 2026
@ti-chi-bot ti-chi-bot Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 18, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

@doc-claw-bot: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-verify 3891dc3 link true /test pull-verify

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

更新 DROP STATS 文档示例。示例显示 SHOW STATS_META 仍返回行数、修改计数和统计元数据记录。TopN、bucket 等直方图统计信息会被清除。

Changes

DROP STATS 文档更新

Layer / File(s) Summary
统计元数据结果说明
sql-statements/sql-statement-drop-stats.md
更新执行结果,展示表的统计元数据。补充 DROP STATS 保留行数、修改计数及 STATS_META 记录,并清除 TopN、bucket 等直方图统计信息。

Estimated code review effort: 1(简单)| ~5 分钟

Merge Risk: 🔵 Low · up to 3891d

The PR is mergeable with explicit owner follow-up because the updated documentation still conflates TopN with histogram buckets and omits Last_analyze_time from the post-DROP STATS output.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed 标题准确概括了修复 DROP STATSSTATS_META 输出的主要变更,内容清晰且具体。
Description check ✅ Passed 描述完整说明了变更内容、适用版本、相关链接、AI 参与情况和变更类型,满足模板要求。
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot

ti-chi-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign oreoxmt for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 31e0fbac-209e-4c15-abf9-ee785d3ef576

📥 Commits

Reviewing files that changed from the base of the PR and between 41add7f and 3891dc3.

📒 Files selected for processing (1)
  • sql-statements/sql-statement-drop-stats.md

Included review availability: Your plan includes up to 4 reviews per rolling hour; 3 remain after this review.

Comment on lines +85 to +86
`DROP STATS` 会清除 TopN、bucket 等直方图相关统计信息,但不会删除
`STATS_META` 中对应的记录。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

不要把 TopN 归入直方图。

TopN 与直方图是两类独立的统计信息。bucket 属于直方图。当前措辞会错误地将 TopN 归类为直方图。官方统计信息文档也将两者分开说明。(docs.pingcap.com)

As per path instructions: 对可以安全替换的连续 Markdown 行提供 committable suggestion。

Suggested change
`DROP STATS` 会清除 TopN、bucket 等直方图相关统计信息,但不会删除
`STATS_META` 中对应的记录。
`DROP STATS` 会清除 TopN、直方图 bucket 等统计信息,但不会删除
`STATS_META` 中对应的记录。

Source: Path instructions

Comment on lines +93 to +98
+---------+------------+----------------+---------------------+--------------+-----------+
| Db_name | Table_name | Partition_name | Update_time | Modify_count | Row_count |
+---------+------------+----------------+---------------------+--------------+-----------+
| test | t | | 2020-05-25 20:34:33 | 0 | 0 |
+---------+------------+----------------+---------------------+--------------+-----------+
1 row in set (0.00 sec)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- changed file context ---'
sed -n '70,110p' sql-statements/sql-statement-drop-stats.md
printf '%s\n' '--- SHOW STATS_META documentation references ---'
rg -n -C 4 'Last_analyze_time|SHOW STATS_META|Update_time|Modify_count|Row_count' sql-statements/sql-statement-show-stats-meta.md sql-statements
printf '%s\n' '--- repository version metadata ---'
rg -n 'TiDB v?[0-9]+\.[0-9]+|version|版本' README.md docs sql-statements 2>/dev/null | head -80 || true

Repository: pingcap/docs-cn

Length of output: 45540


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- branch and diff metadata ---'
git branch --show-current
git diff --stat
git diff -- sql-statements/sql-statement-drop-stats.md
printf '%s\n' '--- current branch version table ---'
sed -n '1,35p' README.md
printf '%s\n' '--- DROP STATS implementation references in tracked files ---'
rg -n -C 5 'LastAnalyzeTime|last_analyze_time|DropStats|DROP STATS|drop stats' --glob '*.{go,sql,md}' . | head -240

Repository: pingcap/docs-cn

Length of output: 12274


🌐 Web query:

site:github.com/pingcap/tidb "LastAnalyzeTime" "DROP STATS"

💡 Result:

The terms "LastAnalyzeTime" and "DROP STATS" are primarily associated with the TiDB distributed database system [1][2][3]. In TiDB: 1. DROP STATS: This is a SQL command used to remove collected table statistics from the system [4][5]. It is primarily used to clean up metadata when statistics become outdated, invalid, or are no longer needed [1][6]. The syntax typically takes the form DROP STATS <table_name> [5]. 2. LastAnalyzeTime: This is a field found in the mysql.stats_meta system table that records the timestamp of the last successful ANALYZE operation performed on a table [2][3][7]. It allows administrators to track when statistics were last refreshed [7]. Technical context regarding their interaction: - When DROP STATS is executed, the associated statistics are removed [6]. - Issues have historically been reported where a table's LastAnalyzeTime might persist or appear non-null even after stats have been dropped or if the table has never actually been analyzed [8]. - Developers often manage these behaviors to ensure that metadata consistency is maintained (e.g., ensuring LastAnalyzeTime reflects the actual state of statistical collection) [1][9][3]. For managing extended statistics specifically, newer versions of TiDB have shifted toward using ALTER TABLE... DROP TIDB_STATS to replace older, deprecated syntax, ensuring better consistency across the system [10].

Citations:


🌐 Web query:

site:docs.pingcap.com/tidb/stable "DROP STATS" "Last_analyze_time"

💡 Result:

There is no SQL command "DROP STATS Last_analyze_time". Based on standard database documentation (specifically TiDB, which utilizes both "DROP STATS" and "Last_analyze_time" terminology), these are two distinct concepts [1][2]. 1. DROP STATS: This is a valid SQL command used to delete the statistics of a specified table or its partitions [1]. The correct syntax for this command is: DROP STATS TableName; [1] 2. Last_analyze_time: This is not a command but a column name returned by the "SHOW STATS_META" statement [2]. It displays the timestamp of the last time a table was analyzed [2]. It appears you may have conflated a command with a metadata field. To manage table statistics, you can either drop them using DROP STATS [1] or view their current metadata, including the last analyze time, using: SHOW STATS_META; [2]

Citations:


🏁 Script executed:

#!/bin/bash
set -e
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
curl -fsSL 'https://api.github.com/repos/pingcap/tidb/git/trees/master?recursive=1' \
  | jq -r '.tree[] | select(.type=="blob" and (.path|test("\\.(go|sql)$"))) | .path' \
  | rg 'statistics|executor|infoschema|parser' > "$tmpdir/paths"
printf '%s\n' '--- candidate source paths ---'
cat "$tmpdir/paths" | rg 'stats|statistics|infoschema' | head -120
printf '%s\n' '--- relevant current source lines ---'
while IFS= read -r path; do
  body="$(curl -fsSL "https://raw.githubusercontent.com/pingcap/tidb/master/$path" || true)"
  if printf '%s' "$body" | rg -q 'LastAnalyzeTime|last_analyze_time|DropStats'; then
    printf '%s\n' "### $path"
    printf '%s' "$body" | rg -n -C 5 'LastAnalyzeTime|last_analyze_time|DropStats' | head -160
  fi
done < <(rg 'stats|statistics' "$tmpdir/paths" | head -80)

Repository: pingcap/docs-cn

Length of output: 5471


🏁 Script executed:

#!/bin/bash
set -e
for path in \
  pkg/executor/show_stats.go \
  pkg/executor/infoschema_reader.go \
  pkg/parser/ast/stats.go \
  pkg/statistics/handle/handle.go \
  pkg/statistics/handle/storage.go
do
  body="$(curl -fsSL "https://raw.githubusercontent.com/pingcap/tidb/master/$path" || true)"
  if printf '%s' "$body" | rg -q 'LastAnalyzeTime|last_analyze_time|DropStats|DROP STATS'; then
    printf '%s\n' "### $path"
    printf '%s' "$body" | rg -n -C 8 'LastAnalyzeTime|last_analyze_time|DropStats|DROP STATS' | head -240
  fi
done

Repository: pingcap/docs-cn

Length of output: 2140


🏁 Script executed:

#!/bin/bash
set -e
curl -fsSL 'https://api.github.com/repos/pingcap/tidb/git/trees/master?recursive=1' |
  jq -r '.tree[] | select(.type=="blob") | .path' |
  rg '(^|/)(stats|statistics|analyze|infoschema|executor)([^/]*)(\.go|\.sql)$|stats_meta|statsmeta' |
  head -240

Repository: pingcap/docs-cn

Length of output: 4444


🏁 Script executed:

#!/bin/bash
set -e
for path in \
  pkg/ddl/executor.go \
  pkg/statistics/handle/storage/stats_read_writer.go \
  pkg/statistics/analyze.go \
  pkg/executor/analyze.go \
  pkg/infoschema/infoschema.go
do
  body="$(curl -fsSL "https://raw.githubusercontent.com/pingcap/tidb/master/$path")"
  printf '%s\n' "### $path"
  printf '%s' "$body" |
    rg -n -i -C 8 'drop stats|last[_a-z]*analy|stats_meta|statsmeta|analyze_time' |
    head -260 || true
done

Repository: pingcap/docs-cn

Length of output: 13338


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- DDL and SHOW-related source paths ---'
curl -fsSL 'https://api.github.com/repos/pingcap/tidb/git/trees/master?recursive=1' |
  jq -r '.tree[] | select(.type=="blob") | .path' |
  rg '(^|/)(ddl|executor|statistics|infoschema)/.*(stats|ddl|show|meta).*\.go$|(^|/)ddl/.*\.go$' |
  head -320

Repository: pingcap/docs-cn

Length of output: 10672


🏁 Script executed:

#!/bin/bash
set -e
for path in \
  pkg/executor/show_stats.go \
  pkg/executor/ddl.go \
  pkg/statistics/handle/ddl/ddl.go \
  pkg/ddl/stat.go
do
  body="$(curl -fsSL "https://raw.githubusercontent.com/pingcap/tidb/master/$path")"
  printf '%s\n' "### $path"
  printf '%s' "$body" |
    rg -n -i -C 12 'last[_a-z]*analy|stats_meta|statsmeta|dropstats|drop stats|delete.*stats|truncate.*stats' |
    head -360 || true
done

Repository: pingcap/docs-cn

Length of output: 5981


🏁 Script executed:

#!/bin/bash
set -e
for path in \
  pkg/statistics/handle/ddl/ddl.go \
  pkg/statistics/handle/storage/stats_read_writer.go \
  pkg/executor/ddl.go \
  pkg/ddl/stat.go
do
  body="$(curl -fsSL "https://raw.githubusercontent.com/pingcap/tidb/master/$path")"
  printf '%s\n' "### $path"
  printf '%s' "$body" |
    rg -n -i -C 10 'delete|remove|clear|reset|pseudo|analy[sz]|lastanaly|stats.*storage|storage.*stats' |
    head -420 || true
done

Repository: pingcap/docs-cn

Length of output: 16785


🏁 Script executed:

#!/bin/bash
set -e
curl -sS -D /tmp/tidb-search-headers \
  'https://api.github.com/search/code?q=DropStatsStmt+repo%3Apingcap%2Ftidb' \
  -o /tmp/tidb-search-body
printf '%s\n' '--- response status ---'
head -1 /tmp/tidb-search-headers
printf '%s\n' '--- response body ---'
cat /tmp/tidb-search-body
rm -f /tmp/tidb-search-headers /tmp/tidb-search-body

Repository: pingcap/docs-cn

Length of output: 336


🌐 Web query:

site:github.com/pingcap/tidb/issues "DROP STATS" "Last_analyze_time"

💡 Result:

There is no standard SQL command known as DROP STATS. Based on the provided search results, the term Last_analyze_time is a column found in database statistics metadata tables (such as mysql.stats_meta in TiDB), which tracks when a table's statistics were last updated via an ANALYZE operation [1][2]. The search results discuss the behavior, maintenance, and potential bugs regarding statistics collection and metadata, but do not contain any evidence of a command to drop statistics or interact with the Last_analyze_time column using a DROP STATS syntax [3][1][2]. If you are attempting to remove statistics, you should check your specific database management system's documentation for commands like ANALYZE (to update/refresh) or specific administrative procedures for managing or deleting statistics metadata.

Citations:


🌐 Web query:

site:github.com/pingcap/docs-cn "DROP STATS" "Last_analyze_time"

💡 Result:

The command DROP STATS is a specific SQL statement used in TiDB to delete table statistics [1][2]. There is no standard command or system variable named Last_analyze_time that is used with DROP STATS. It appears the user may be confusing the DROP STATS command with metadata or status monitoring commands [3]. In TiDB, you can view information about statistics and analyze jobs using: 1. SHOW ANALYZE STATUS: To see the status of statistics collection tasks, including start and end times [3]. 2. Information Schema tables: Such as the ANALYZE_STATUS table, which contains metadata about analyze jobs [3]. If the intention is to delete statistics for a table, the correct syntax is: DROP STATS TableName; [2] To delete statistics for specific partitions or global statistics in a partitioned table, you can use: DROP STATS TableName PARTITION PartitionNameList; [2] DROP STATS TableName GLOBAL; [2][4] If you are looking for the last time a table was analyzed, this information is typically found in the system tables (like mysql.stats_meta) or viewed via statistics-related inspection tools provided by the database, rather than through a DROP command.

Citations:


🌐 Web query:

site:docs.pingcap.com "DROP STATS" "Last_analyze_time" "NULL"

💡 Result:

There is no standard SQL command DROP STATS Last_analyze_time NULL. The DROP STATS statement in TiDB is used to delete all statistics for a specified table, not to reset individual metadata fields like Last_analyze_time [1][2][3]. If you are using TiDB and want to remove statistics for a table, the correct syntax is: DROP STATS TableName; [1][3] Regarding the Last_analyze_time field seen in SHOW STATS_META: - This field is system-generated and reflects the last time an ANALYZE operation was performed on the table [4][5][6]. - When no ANALYZE has been performed, or if statistics have been cleared, this value may appear as NULL [7]. - There is no direct command to manually set or "drop" this specific metadata field to NULL while retaining other statistics [1][8]. If your goal is to have Last_analyze_time appear as NULL, executing DROP STATS on the table will typically remove its statistics record entirely [1][3], which effectively removes the Last_analyze_time value along with all other stats data [7].

Citations:


🏁 Script executed:

#!/bin/bash
set -e
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
curl -fsSL 'https://codeload.github.com/pingcap/tidb/tar.gz/refs/heads/master' |
  tar -xz -C "$tmpdir"
root="$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d | head -1)"
printf '%s\n' '--- DropStatsStmt references ---'
rg -n -C 10 'DropStatsStmt|dropStats|DROP STATS|DropStats' "$root/pkg" | head -400
printf '%s\n' '--- stats metadata deletion/reset references ---'
rg -n -i -C 10 'last_analyze_time|lastanaly|delete from mysql\.stats_meta|stats_meta.*delete|delete.*stats_meta|truncate.*stats' "$root/pkg" | head -400

Repository: pingcap/docs-cn

Length of output: 50371


🏁 Script executed:

#!/bin/bash
set -e
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
curl -fsSL 'https://codeload.github.com/pingcap/tidb/tar.gz/refs/heads/master' |
  tar -xz -C "$tmpdir"
root="$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d | head -1)"
printf '%s\n' '--- executeDropStats ---'
sed -n '3530,3605p' "$root/pkg/executor/simple.go"
printf '%s\n' '--- soft-delete storage logic ---'
sed -n '125,180p' "$root/pkg/statistics/handle/storage/gc.go"
printf '%s\n' '--- stats load reset logic ---'
sed -n '500,550p' "$root/pkg/statistics/handle/storage/read.go"

Repository: pingcap/docs-cn

Length of output: 7250


补充 Last_analyze_time 列。

DROP STATS 后,SHOW STATS_META 仍返回七列;未分析统计信息时,Last_analyze_timeNULL

Suggested change
+---------+------------+----------------+---------------------+--------------+-----------+
| Db_name | Table_name | Partition_name | Update_time | Modify_count | Row_count |
+---------+------------+----------------+---------------------+--------------+-----------+
| test | t | | 2020-05-25 20:34:33 | 0 | 0 |
+---------+------------+----------------+---------------------+--------------+-----------+
1 row in set (0.00 sec)
+---------+------------+----------------+---------------------+--------------+-----------+---------------------+
| Db_name | Table_name | Partition_name | Update_time | Modify_count | Row_count | Last_analyze_time |
+---------+------------+----------------+---------------------+--------------+-----------+---------------------+
| test | t | | 2020-05-25 20:34:33 | 0 | 0 | NULL |
+---------+------------+----------------+---------------------+--------------+-----------+---------------------+
1 row in set (0.00 sec)

Source: Path instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution This PR is from a community contributor. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. translation/from-docs This PR is translated from a PR in pingcap/docs. type/bugfix This PR fixes a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant