gpt4 book ai didi

MySQL "Show table status",自动增量不正确

转载 作者:行者123 更新时间:2023-11-29 15:22:48 24 4
gpt4 key购买 nike

我在Mysql中创建了一个新表,添加了一些行,但显示表的Auto_increment字段仍然返回NULL。

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

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

我做错了什么?

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

<小时/>

重现步骤:

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'

编辑:

过了一会儿,它会自动开始工作。没有明确的原因如何让它开始工作。

最佳答案

这是一个功能..而不是一个错误。

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

SET PERSIST information_schema_stats_expiry = 0

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

示例:

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

我们运行相同的测试:

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 "Show table status",自动增量不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com