Skip to content
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

sql-statements: add SHOW STATS_HEALTHY (#3388) #3400

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
+ [`SHOW PROFILES`](/sql-statements/sql-statement-show-profiles.md)
+ [`SHOW PUMP STATUS`](/sql-statements/sql-statement-show-pump-status.md)
+ [`SHOW SCHEMAS`](/sql-statements/sql-statement-show-schemas.md)
+ [`SHOW STATS_HEALTHY`](/sql-statements/sql-statement-show-stats-healthy.md)
+ [`SHOW STATS_HISTOGRAMS`](/sql-statements/sql-statement-show-histograms.md)
+ [`SHOW STATES_META`](/sql-statements/sql-statement-show-stats-meta.md)
+ [`SHOW STATUS`](/sql-statements/sql-statement-show-status.md)
Expand Down
89 changes: 89 additions & 0 deletions sql-statements/sql-statement-show-stats-healthy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: SHOW STATS_HEALTHY
summary: An overview of the usage of SHOW STATS_HEALTHY for TiDB database.
---

# SHOW STATS_HEALTHY

The `SHOW STATS_HEALTHY` statement shows an estimation of how accurate statistics are believed to be. Tables with a low percentage health may generate sub-optimal query execution plans.

The health of a table can be improved by running the `ANALYZE` table command. `ANALYZE` runs automatically when the health drops below the [`tidb_auto_analyze_ratio`](/system-variables.md#tidb_auto_analyze_ratio) threshold.

## Synopsis

**ShowStmt**

![ShowStmt](/media/sqlgram/ShowStmt.png)

**ShowTargetFiltertable**

![ShowTargetFilterable](/media/sqlgram/ShowTargetFilterable.png)

**ShowLikeOrWhereOpt**

![ShowLikeOrWhereOpt](/media/sqlgram/ShowLikeOrWhereOpt.png)

## Examples

Load example data and run `ANALYZE`:

{{< copyable "sql" >}}

```sql
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY auto_increment,
b INT NOT NULL,
pad VARBINARY(255),
INDEX(b)
);

INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM dual;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
INSERT INTO t1 SELECT NULL, FLOOR(RAND()*1000), RANDOM_BYTES(255) FROM t1 a JOIN t1 b JOIN t1 c LIMIT 100000;
SELECT SLEEP(1);
ANALYZE TABLE t1;
SHOW STATS_HEALTHY; # should be 100% healthy
```

```sql
...
mysql> SHOW STATS_HEALTHY;
+---------+------------+----------------+---------+
| Db_name | Table_name | Partition_name | Healthy |
+---------+------------+----------------+---------+
| test | t1 | | 100 |
+---------+------------+----------------+---------+
1 row in set (0.00 sec)
```

Perform a bulk update deleting approximately 30% of the records. Check the health of the statistics:

{{< copyable "sql" >}}

```sql
DELETE FROM t1 WHERE id BETWEEN 101010 AND 201010; # delete about 30% of records
SHOW STATS_HEALTHY;
```

```sql
mysql> SHOW STATS_HEALTHY;
+---------+------------+----------------+---------+
| Db_name | Table_name | Partition_name | Healthy |
+---------+------------+----------------+---------+
| test | t1 | | 50 |
+---------+------------+----------------+---------+
1 row in set (0.00 sec)
```

## MySQL compatibility

This statement is a TiDB extension to MySQL syntax.

## See also

* [ANALYZE](/sql-statements/sql-statement-analyze-table.md)
* [Introduction to Statistics](/statistics.md)
2 changes: 1 addition & 1 deletion sql-statements/sql-statement-show-table-regions.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: SHOW TABLE REGIONS
summary: Learn how to use SHOW TABLE REGIONS in TiDB.
aliases: ['/docs/stable/sql-statements/sql-statement-show-table-regions/','/docs/v4.0/sql-statements/sql-statement-show-table-regions/','/docs/stable/reference/sql/statements/show-table-regions/']
aliases: ['/docs/stable/sql-statements/sql-statement-show-table-regions/','/docs/v4.0/sql-statements/sql-statement-show-table-regions/','/docs/stable/reference/sql/statements/show-table-regions/','/tidb/v2.1/sql-statements/sql-statement-show-table-regions','/docs/v2.1/sql-statements/sql-statement-show-table-regions/','/docs/v2.1/reference/sql/statements/show-table-regions/']
---

# SHOW TABLE REGIONS
Expand Down
2 changes: 1 addition & 1 deletion sql-statements/sql-statement-split-region.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Split Region
summary: An overview of the usage of Split Region for the TiDB database.
aliases: ['/docs/stable/sql-statements/sql-statement-split-region/','/docs/v4.0/sql-statements/sql-statement-split-region/','/docs/stable/reference/sql/statements/split-region/']
aliases: ['/docs/stable/sql-statements/sql-statement-split-region/','/docs/v4.0/sql-statements/sql-statement-split-region/','/docs/stable/reference/sql/statements/split-region/','/tidb/v2.1/sql-statements/sql-statement-split-region','/docs/v2.1/sql-statements/sql-statement-split-region/','/docs/v2.1/reference/sql/statements/split-region/']
---

# Split Region
Expand Down