PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 数据库 > MySQL教程 > 正文

    存储引擎和Mysql服务层出现索引信息不一致错误提示

    作者:admin来源:oschina浏览:时间:2020-09-30 00:07:50我要评论
    导读:存储引擎和Mysql服务层出现索引信息不一致错误提示
    错误日志:

    1. [ERROR] Table vip_cube/imp_sup_dm_sup_brand_name_goods_online_half_hm contains 2 indexes inside InnoDB, which is different from the number of indexes 1 defined in the MySQL 

    存储引擎和Mysql服务层出现索引统计信息不一致,是否进行了DDL操作(创建了索引?出现这个错误,新创建的索引是否能使用?)

    问题重现:

    1. root@localhost*5.5.48-log[test] >create table employees like employees.employees; 
    2. Query OK, 0 rows affected (0.21 sec) 
    3. root@localhost*5.5.48-log[test] > \! cp employees.frm employees.frm.old 
    4. root@localhost*5.5.48-log[test] >alter table employees add index idx_first_name(first_name); 
    5. Query OK, 0 rows affected (0.64 sec) 
    6. Records: 0 Duplicates: 0 Warnings: 0 
    7. root@localhost*5.5.48-log[test] >\! mv employees.frm.old employees.frm 
    8. root@localhost*5.5.48-log[test] >\! chown mysql.mysql employees.frm 
    9. root@localhost*5.5.48-log[test] >flush tables; 
    10. root@localhost*5.5.48-log[test] >select first_name from employees where first_name like 'a%' limit 1; 
    11. Empty set (0.00 sec) 

    查看错误日志:

    1. [ERROR] Table test/employees contains 2 indexes inside InnoDB, which is different from the number of indexes 1 defined in the MySQL 
    2. root@localhost*5.5.48-log[test] >explain select first_name from employees where first_name like 'a%' limit 1; 
    3. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    4. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
    5. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    6. | 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 1 | Using where | 
    7. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    8. 1 row in set (0.00 sec) 

    可以看到语句是走不到索引的,语句分析和优化是mysql server完成?

    然后恢复创建索引后的frm文件:

    1. root@localhost*5.5.48-log[test] > mv employees.frm.2 employees.frm – `employees.frm.2之前备份了` 
    2. root@localhost*5.5.48-log[test] >flush tables; 
    3. Query OK, 0 rows affected (0.02 sec) 
    4. root@localhost*5.5.48-log[test] >explain select first_name from employees where first_name like 'a%' limit 1; 
    5. +----+-------------+-----------+-------+----------------+----------------+---------+------+------+--------------------------+
    6. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
    7. +----+-------------+-----------+-------+----------------+----------------+---------+------+------+--------------------------+
    8. | 1 | SIMPLE | employees | index | idx_first_name | idx_first_name | 58 | NULL | 1 | Using where; Using index | 
    9. +----+-------------+-----------+-------+----------------+----------------+---------+------+------+--------------------------+
    10. 1 row in set (0.00 sec) 
    11. root@localhost*5.5.48-log[test] >alter table employees engine=innodb; 
    12. Query OK, 0 rows affected (0.07 sec) 
    13. Records: 0 Duplicates: 0 Warnings: 0 
    14. root@localhost*5.5.48-log[test] >flush tables; 
    15. Query OK, 0 rows affected (0.04 sec) 
    16. root@localhost*5.5.48-log[test] >explain select first_name from employees where first_name like 'a%' limit 1; 
    17. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    18. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
    19. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    20. | 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 1 | Using where | 
    21. +----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
    22. 1 row in set (0.00 sec) 


    然后恢复有索引的frm文件

    1. root@localhost*5.5.48-log[test] >flush tables; 
    2. Query OK, 0 rows affected (0.04 sec) 
    3. root@localhost*5.5.48-log[test] >show create table employees\G; 
    4. *************************** 1. row *************************** 
    5. Table: employees 
    6. Create TableCREATE TABLE `employees` ( 
    7. `emp_no` int(11) NOT NULL
    8. `birth_date` date NOT NULL
    9. `first_name` varchar(14) NOT NULL
    10. `last_name` varchar(16) NOT NULL
    11. `gender` enum('M','F'NOT NULL
    12. `hire_date` date NOT NULL
    13. PRIMARY KEY (`emp_no`), 
    14. KEY `idx_first_name` (`first_name`) – `索引是可以看到,查frm文件` 
    15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 
    16. 1 row in set (0.00 sec) 
    17. ERROR: 
    18. No query specified 

    但是日志会报如下错,说明存储引擎的索引已经在执行alter table employees engine=innodb;删除(重建表基于frm定义) :

    1. 160508 17:44:01 [ERROR] Table test/employees contains 1 indexes inside InnoDB, which is different from the number of indexes 2 defined in the MySQL 
    2. 160508 17:44:01 [ERROR] Innodb could not find key n:o 1 with name idx_first_name from dict cache for table test/employees 
    3. 160508 17:44:01 [ERROR] Table test/employees contains fewer indexes inside InnoDB than are defined in the MySQL .frm file. Have you mixed up .frm files from different installations? See http://dev.mysql.com/doc/refman/5.5/en/innodb-troubleshooting.html 

    参考文档:
    https://www.percona.com/blog/2011/11/29/innodb-vs-mysql-index-counts/
    https://www.percona.com/doc/percona-server/5.1/management/innodb_fast_index_creation.html
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-35-3561-1.html
    相关热词搜索: 存储引擎 mysql索引