title: SHOW STATS_HEALTHY summary: TiDB 数据库中 SHOW STATS_HEALTHY 的使用概况。

SHOW STATS_HEALTHY

SHOW STATS_HEALTHY 语句可以预估统计信息的准确度,也就是健康度。健康度低的表可能会生成次优查询执行计划。

可以通过执行 ANALYZE 表命令来改善表的健康度。当表的健康度下降到低于 tidb_auto_analyze_ratio 时,则会自动执行 ANALYZE 命令。

语法图

ShowStmt

ShowStmt

ShowTargetFilterable

ShowTargetFilterable

ShowLikeOrWhereOpt

ShowLikeOrWhereOpt

示例

加载示例数据并运行 ANALYZE 命令:

  1. CREATE TABLE t1 (
  2. id INT NOT NULL PRIMARY KEY auto_increment,
  3. b INT NOT NULL,
  4. pad VARBINARY(255),
  5. INDEX(b)
  6. );
  7. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM dual;
  8. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  9. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  10. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  11. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  12. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  13. INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
  14. SELECT SLEEP(1);
  15. ANALYZE TABLE t1;
  16. SHOW STATS_HEALTHY; # should be 100% healthy
  1. SHOW STATS_HEALTHY;
  1. +---------+------------+----------------+---------+
  2. | Db_name | Table_name | Partition_name | Healthy |
  3. +---------+------------+----------------+---------+
  4. | test | t1 | | 100 |
  5. +---------+------------+----------------+---------+
  6. 1 row in set (0.00 sec)

执行批量更新来删除大约 30% 的记录,然后检查统计信息的健康度:

  1. DELETE FROM t1 WHERE id BETWEEN 101010 AND 201010; # delete about 30% of records
  2. SHOW STATS_HEALTHY;
  1. SHOW STATS_HEALTHY;
  2. +---------+------------+----------------+---------+
  3. | Db_name | Table_name | Partition_name | Healthy |
  4. +---------+------------+----------------+---------+
  5. | test | t1 | | 50 |
  6. +---------+------------+----------------+---------+
  7. 1 row in set (0.00 sec)

MySQL 兼容性

SHOW STATS_HEALTHY 语句是 TiDB 对 MySQL 语法的扩展。

另请参阅