LOCK STATS

LOCK STATS is used to lock the statistics of tables or partitions. When the statistics is locked, TiDB does not automatically update the statistics of the table or partition. For details on the behavior, see Behaviors of locking statistics.

Synopsis

LockStatsStmt

LOCK STATS - 图1

TableNameList

LOCK STATS - 图2

TableName

LOCK STATS - 图3

PartitionNameList

LOCK STATS - 图4

  1. LockStatsStmt ::=
  2. 'LOCK' 'STATS' (TableNameList) | (TableName 'PARTITION' PartitionNameList)
  3. TableNameList ::=
  4. TableName (',' TableName)*
  5. TableName ::=
  6. Identifier ( '.' Identifier )?
  7. PartitionNameList ::=
  8. Identifier ( ',' Identifier )*

Examples

Create table t, and insert data into it. When the statistics of table t are not locked, the ANALYZE statement can be successfully executed.

  1. mysql> CREATE TABLE t(a INT, b INT);
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> INSERT INTO t VALUES (1,2), (3,4), (5,6), (7,8);
  4. Query OK, 4 rows affected (0.00 sec)
  5. Records: 4 Duplicates: 0 Warnings: 0
  6. mysql> ANALYZE TABLE t;
  7. Query OK, 0 rows affected, 1 warning (0.02 sec)
  8. mysql> SHOW WARNINGS;
  9. +-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  10. | Level | Code | Message |
  11. +-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/4) as the sample-rate=1" |
  13. +-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. 1 row in set (0.00 sec)

Lock the statistics of table t and execute ANALYZE. From the output of SHOW STATS_LOCKED, you can see that the statistics of table t have been locked. The warning message shows that the ANALYZE statement has skipped table t.

  1. mysql> LOCK STATS t;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> SHOW STATS_LOCKED;
  4. +---------+------------+----------------+--------+
  5. | Db_name | Table_name | Partition_name | Status |
  6. +---------+------------+----------------+--------+
  7. | test | t | | locked |
  8. +---------+------------+----------------+--------+
  9. 1 row in set (0.01 sec)
  10. mysql> ANALYZE TABLE t;
  11. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  12. mysql> SHOW WARNINGS;
  13. +---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
  14. | Level | Code | Message |
  15. +---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
  16. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t, reason to use this rate is "use min(1, 110000/8) as the sample-rate=1" |
  17. | Warning | 1105 | skip analyze locked table: test.t |
  18. +---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
  19. 2 rows in set (0.00 sec)

In addition, you can also lock the statistics of a partition using LOCK STATS. For example:

Create a partition table t, and insert data into it. When the statistics of partition p1 are not locked, the ANALYZE statement can be successfully executed.

  1. mysql> CREATE TABLE t(a INT, b INT) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (10), PARTITION p1 VALUES LESS THAN (20), PARTITION p2 VALUES LESS THAN (30));
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> INSERT INTO t VALUES (1,2), (3,4), (5,6), (7,8);
  4. Query OK, 4 rows affected (0.00 sec)
  5. Records: 4 Duplicates: 0 Warnings: 0
  6. mysql> ANALYZE TABLE t;
  7. Query OK, 0 rows affected, 6 warning (0.02 sec)
  8. mysql> SHOW WARNINGS;
  9. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  10. | Level | Code | Message |
  11. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Warning | 1105 | disable dynamic pruning due to t has no global stats |
  13. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/4) as the sample-rate=1" |
  14. | Warning | 1105 | disable dynamic pruning due to t has no global stats |
  15. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1" |
  16. | Warning | 1105 | disable dynamic pruning due to t has no global stats |
  17. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p2, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1" |
  18. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. 6 rows in set (0.01 sec)

Lock the statistics of partition p1 and execute ANALYZE. The warning message shows that the ANALYZE statement has skipped partition p1.

  1. mysql> LOCK STATS t PARTITION p1;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> SHOW STATS_LOCKED;
  4. +---------+------------+----------------+--------+
  5. | Db_name | Table_name | Partition_name | Status |
  6. +---------+------------+----------------+--------+
  7. | test | t | p1 | locked |
  8. +---------+------------+----------------+--------+
  9. 1 row in set (0.00 sec)
  10. mysql> ANALYZE TABLE t PARTITION p1;
  11. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  12. mysql> SHOW WARNINGS;
  13. +---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | Level | Code | Message |
  15. +---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1" |
  17. | Warning | 1105 | skip analyze locked table: test.t partition (p1) |
  18. +---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. 2 rows in set (0.00 sec)

For information on unlock statistics, see UNLOCK STATS.

MySQL compatibility

This statement is a TiDB extension to MySQL syntax.

See also