MySQL “Show table status”, auto increment is not correct

后端 未结 1 433
清酒与你
清酒与你 2020-12-17 05:31

I create a new table in Mysql, add some rows, yet the Auto_increment field of show tables still returns NULL.

The mysql manual says: this field should return: \"Th

相关标签:
1条回答
  • 2020-12-17 06:18

    It's a feature.. not a bug.

    The table statistics are cached. To disable the cache and always have the latest version you should change the server variable that indicates the duration of the cache-clear to 0:

    SET PERSIST information_schema_stats_expiry = 0
    

    The default value of this property has changed to 86400 (24 hours) in Mysql 8.x

    Example:

    SET PERSIST information_schema_stats_expiry = 86400
    -- 86400 is the default value of mysql 8.x  if you have never changed this you don't need to set this
    
    
    show variables like 'information_schema_stats_expiry';
    
    +---------------------------------+-------+
    | Variable_name                   | Value |
    +---------------------------------+-------+
    | information_schema_stats_expiry | 86400 |
    +---------------------------------+-------+
    
    create schema mytest;
    
    create table `test` (
        `id` int(5) not null auto_increment,
        `name` varchar(256),
        PRIMARY KEY(`id`)
    );
    
    insert into test values(null,'name1')
    insert into test values(null,'name2')
    insert into test values(null,'name3')
    
    show table status where name like 'test';
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | test | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |               0 |            0 |         0 |              4 | 2018-10-09 15:32:15 | 2018-10-09 15:32:16 | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    -- The Auto_increment field is correctly set to 4.. but is now cached.
    
    insert into test values(null,'name3');
    
    show table status where name like 'test';
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | test | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |               0 |            0 |         0 |              4 | 2018-10-09 15:32:15 | 2018-10-09 15:32:16 | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    -- The Auto_increment is still 4 (it was cached).
    
    drop schema mytest
    

    Now we change the configuration:

    SET PERSIST information_schema_stats_expiry = 0
    

    and we run the same test:

    show variables like 'information_schema_stats_expiry'
    
    
    +---------------------------------+-------+
    | Variable_name                   | Value |
    +---------------------------------+-------+
    | information_schema_stats_expiry | 0     |
    +---------------------------------+-------+
    
    create schema mytest;
    create table `test` (
        `id` int(5) not null auto_increment,
        `name` varchar(256),
        PRIMARY KEY(`id`)
    );
    
    insert into test values(null,'name1');
    insert into test values(null,'name2');
    insert into test values(null,'name3');
    
    show table status where name like 'test';
    
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | test | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |               0 |            0 |         0 |              4 | 2018-10-09 15:32:49 | 2018-10-09 15:32:49 | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    --  Auto_increment is 4, but the result is not cached!
    
    insert into test values(null,'name3');
    
    
    
    show table status where name like 'test';
    
    
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    | test | InnoDB |      10 | Dynamic    |    4 |           4096 |       16384 |               0 |            0 |         0 |              5 | 2018-10-09 15:32:49 | 2018-10-09 15:32:49 | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
    +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
    -- The  Auto_increment field is now 5 (a correct, not cached value)
    
    drop schema mytest;
    
    0 讨论(0)
提交回复
热议问题