-
Notifications
You must be signed in to change notification settings - Fork 1.2k
sql-statement-drop-stats: fix STATS_META output after DROP STATS #21887
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,12 +81,21 @@ DROP STATS t; | |||||||||||||||||||||||||
| Query OK, 0 rows affected (0.00 sec) | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 执行 `DROP STATS` 后,`SHOW STATS_META` 仍会返回该表的行数和修改计数。 | ||||||||||||||||||||||||||
| `DROP STATS` 会清除 TopN、bucket 等直方图相关统计信息,但不会删除 | ||||||||||||||||||||||||||
| `STATS_META` 中对应的记录。 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```sql | ||||||||||||||||||||||||||
| SHOW STATS_META WHERE db_name='test' and table_name='t'; | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
| Empty set (0.00 sec) | ||||||||||||||||||||||||||
| +---------+------------+----------------+---------------------+--------------+-----------+ | ||||||||||||||||||||||||||
| | 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) | ||||||||||||||||||||||||||
|
Comment on lines
+93
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 || trueRepository: 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 -240Repository: pingcap/docs-cn Length of output: 12274 🌐 Web query:
💡 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:
💡 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
doneRepository: 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 -240Repository: 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
doneRepository: 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 -320Repository: 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
doneRepository: 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
doneRepository: 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-bodyRepository: pingcap/docs-cn Length of output: 336 🌐 Web query:
💡 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:
💡 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:
💡 Result: There is no standard SQL command 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 -400Repository: 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 补充
Suggested change
Source: Path instructions |
||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ## MySQL 兼容性 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
不要把
TopN归入直方图。TopN与直方图是两类独立的统计信息。bucket属于直方图。当前措辞会错误地将TopN归类为直方图。官方统计信息文档也将两者分开说明。(docs.pingcap.com)As per path instructions: 对可以安全替换的连续 Markdown 行提供 committable suggestion。
Source: Path instructions