Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sql-statements/sql-statement-drop-stats.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 中对应的记录。
Comment on lines +85 to +86

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


```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

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

```

## MySQL 兼容性
Expand Down
Loading