# `VERSION::6.5.0` Locking Statistics

+ **`LOCK STATS`** and **`UNLOCK STATS`** commands
+ When statistics of a table is locked,
  + Modifications on stats are prohibited
  `ANALYZE` statements can not be executed

```
tidb> LOCK STATS users;
Query OK, 0 rows affected (0.01 sec)

tidb> SHOW stats_locked;
+---------+------------+----------------+--------+
| Db_name | Table_name | Partition_name | Status |
+---------+------------+----------------+--------+
|  test   |   users    |                | locked |
+---------+------------+----------------+--------+
1 row in set (0.00 sec)

tidb> ANALYZE TABLE users;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

tidb> SHOW warnings;
+---------+------+---------------------------------------------------------------------+
| Level   | Code | Message                                                             |
+---------+------+---------------------------------------------------------------------+
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.users |
| Warning | 1105 | skip analyze locked table: users                                    |
+---------+------+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

tidb> UNLOCK STATS users;
Query OK, 0 rows affected (0.00 sec)

tidb> ANALYZE TABLE users;
Query OK, 0 rows affected, 1 warning (0.10 sec)

tidb> SHOW WARNINGS;
+-------+------+---------------------------------------------------------------------+
| Level | Code | Message                                                             |
+-------+------+---------------------------------------------------------------------+
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.users |
+-------+------+---------------------------------------------------------------------+
1 row in set (0.00 sec)

```
------------------------------------------------------------------------------
Starting with version 6.5.0, TiDB includes support for locking statistics. When the statistics of a table are locked, any modifications to the table's statistics are prohibited, and the `ANALYZE` statement cannot be executed on that table. For example:

Create table named `users`, and insert data into it. When the statistics of table `users` are not locked, the `ANALYZE` statement can be successfully executed.
```
tidb> CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(32));
Query OK, 0 rows affected (0.53 sec)

tidb> INSERT INTO users VALUES (1, 'Tom'), (2, 'Jack'), (3, 'Frank'), (4, 'Tony');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

tidb> ANALYZE TABLE users;
Query OK, 0 rows affected, 1 warning (0.11 sec)

tidb> SHOW WARNINGS;
+-------+------+---------------------------------------------------------------------+
| Level | Code | Message                                                             |
+-------+------+---------------------------------------------------------------------+
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.users |
+-------+------+---------------------------------------------------------------------+
1 row in set (0.01 sec)
```

Lock the statistics of table users and execute `ANALYZE`. Upon running `SHOW STATS_LOCKED`, you will observe that the statistics for the `users` table are locked. Additionally, a warning message will indicate that the `ANALYZE` statement has bypassed the `users` table.

```
tidb> LOCK STATS users;
Query OK, 0 rows affected (0.01 sec)

tidb> SHOW stats_locked;
+---------+------------+----------------+--------+
| Db_name | Table_name | Partition_name | Status |
+---------+------------+----------------+--------+
| test    | users      |                | locked |
+---------+------------+----------------+--------+
1 row in set (0.00 sec)

tidb> ANALYZE TABLE users;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

tidb> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------+
| Level   | Code | Message                                                             |
+---------+------+---------------------------------------------------------------------+
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.users |
| Warning | 1105 | skip analyze locked table: users                                    |
+---------+------+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
```

After unlocking the statistics of table `user` and `ANALYZE` can be successfully executed again.

```
tidb> UNLOCK STATS users;
Query OK, 0 rows affected (0.00 sec)

tidb> ANALYZE TABLE users;
Query OK, 0 rows affected, 1 warning (0.10 sec)

tidb> SHOW WARNINGS;
+-------+------+---------------------------------------------------------------------+
| Level | Code | Message                                                             |
+-------+------+---------------------------------------------------------------------+
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.users |
+-------+------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
```

Note: Until TiDB version 7.1, the feature of locking statistics is an experimental feature. Exercise caution when using it in a production environment.