MySQL“显示表格状态",自动递增不正确 [英] MySQL "Show table status", auto increment is not correct

查看:74
本文介绍了MySQL“显示表格状态",自动递增不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mysql中创建了一个新表,添加了一些行,但是show table的Auto_increment字段仍然返回NULL.

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

mysql手册说:该字段应返回:下一个Auto_increment值"

The mysql manual says: this field should return: "The next Auto_increment value"

https://dev.mysql.com /doc/refman/8.0/en/show-table-status.html

我在做什么错了?

如何正确找到下一个auto_increment值?

How can I find the next auto_increment value correctly?

复制步骤:

create table `test` (
  `id` int(5) not null auto_increment,
  `name` varchar(256),
  PRIMARY KEY(`id`)
);

然后我跑步:

show table status where name like 'test';

结果:

Name,   Engine,   Version, ...,  Auto_increment, ... 
'test', 'InnoDB', '10',    ...,  NULL, ...

然后我跑:

insert into test values(null,'name1');
insert into test values(null,'name2');
insert into test values(null,'name3');

-其他插入语法-

insert into test (name) values('name4');
insert into test (name) values('name5');
insert into test (name) values('name6');

获取表的状态

show table status where name like 'test';

结果

Name,   Engine,   Version, ...,  Auto_increment, ... 
'test', 'InnoDB', '10',    ...,  NULL, ...

表格中的数据

select * from test;

结果:

1   name1
2   name2
3   name3

供您参考:

SHOW VARIABLES LIKE "%version%";

结果:

'innodb_version', '8.0.12'
'protocol_version', '10'
'slave_type_conversions', ''
'tls_version', 'TLSv1,TLSv1.1,TLSv1.2'
'version', '8.0.12'
'version_comment', 'MySQL Community Server - GPL'
'version_compile_machine', 'x86_64'
'version_compile_os', 'Win64'
'version_compile_zlib', '1.2.11'

自动提交:

SHOW VARIABLES LIKE "autocommit";

结果:

'autocommit', 'ON'

一段时间后,它会自动开始工作.没有明确的理由使它开始工作.

After a while it automagically starts working. No clear reason how to make it start working.

推荐答案

这是一项功能.不是bug.

It's a feature.. not a bug.

表统计信息已缓存.要禁用缓存并始终使用最新版本,您应该将指示缓存清除持续时间的服务器变量更改为0:

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

在Mysql 8.x中,此属性的默认值已更改为86400(24小时)

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

示例:

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

现在我们更改配置:

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;

这篇关于MySQL“显示表格状态",自动递增不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆