MySQL5.5索引数在InnoDB引擎内与索引数在mysql中定义的数量是不一致问题

在查看MySQL错误日志的时候发现这样的错误,如下:

160322 21:42:59 [ERROR] Table baby/baby_order contains 12 indexes inside InnoDB, which is different from the number of indexes 11 defined in the My

SQL

大概意思是说表baby_order的索引数在InnoDB引擎内与索引数在mysql中定义的数量是不一致的

为什么会出现这样的错误呢?

参考了这篇文章的解释

https://www.percona.com/blog/2011/11/29/innodb-vs-mysql-index-counts/

I had a customer recently who a few strange errors in their mysqld.err log:

[ERROR] Table database_name/table_name contains 8 indexes inside InnoDB, which is different from the number of indexes 7 defined in the MySQL

1

[ERROR] Table database_name/table_name contains 8 indexes inside InnoDB, which is different from the number of indexes 7 defined in the MySQL

This customer was running Percona Server 5.1 and they got this error on two tables during a maintenance window when they were adding indexes to the same tables.

We had a suspicion that it had something to do with Fast index creation in Innodb, and that it had been corrected when the ALTER TABLE completed because the errors had not recurred.

Reproducing the error on a test system is simple:

复现这个错误的方法:

create an Innodb table

make a copy of the .frm file

do an ALTER TABLE to add an index

then copy the old .frm file back into place

re-open the table  (Might need a FLUSH TABLES or mysqld restart here)

From my testing, I saw that the error only happened when the table was opened and not on every table access.  So, it was a possibility that the indexes were out of sync and we weren’t seeing new errors in the log simply because the table hadn’t been re-opened.

But, before getting too crazy, how can we verify the problem still exists?  We need a way to compare the output of SHOW CREATE TABLE to what Innodb thinks.  What Innodb thinks is in the Innodb Data dictionary.

The first recommendation I got was to simply use the INFORMATION_SCHEMA.INNODB_SYS_INDEXES table, which exists in Percona Server 5.1, but doesn’t appear in MySQL until 5.6 (if the manual is to be trusted).  I’d probably consider this on a newer version of Percona Server or MysqL 5.6.

Another person (I’m looking at you, Baron) was adverse to trusting INNODB_SYS_INDEXES from some bad experiences with it, and suggested the Innodb Table monitor instead, see my next post for how that turned out, but this basically will regurgitate the entire Innodb Data dictionary to the mysqld error log file.

可以通过flush table1 table2 强制表关闭,然后重新打开表

If I had to do it over again, I think I’d simply try doing:  FLUSH TABLES table1, table2; to force the tables to close and be reopened and simply see if the error message comes back.  That might something of a performance impact, but it seems to be the most stable.

In this case, it turned out that the indexes were not out of sync, so I didn’t have to do anything to fix it.

However if I did have to fix it, I found on my test table that the extra index in Innodb could be removed by doing:

修复这个问题可以执行如下的语句更改表引擎为innodb

ALTER TABLE table_name ENGINE=Innodb;

This, of course, rebuilds the whole table based on the .frm table definition and removes the existing index in Innodb, which might not be desirable, but at least you can re-add it later.  However, it’s not the greatest thing to do on a live production database master if you can help it.

Another solution might be to figure out what index was missing via the Innodb data dictionary (more on that in a minute), create a separate table identical to the existing .frm, add that index to it, and copy the new .frm back over the original.  Kind of scary.

My advice is to ensure the error still exists before trying to fix it.

时间: 2025-01-02 03:53:59

MySQL5.5索引数在InnoDB引擎内与索引数在mysql中定义的数量是不一致问题的相关文章

mysql中InnoDB存储引擎的行锁和表锁

Mysql的InnoDB存储引擎支持事务,默认是行锁.因为这个特性,所以数据库支持高并发,但是如果InnoDB更新数据的时候不是行锁,而是表锁的话,那么其并发性会大打折扣,而且也可能导致你的程序出错. 而导致行锁变为表锁的情况之一就是: SQL的更新(update)或者删除(delete)语句中未使用到索引,导致在InnoDB在对数据进行相应操作的时候必须把整个表锁起来进行检索(表锁).而如果使用了索引的话,InnoDB只会通过索引条件检索数据,而只锁住索引对应的行(行锁). 下面记录一下我遇到

关于MySQL InnoDB表的二级索引是否加入主键列的问题解释

关于MySQL InnoDB表的二级索引是否加入主键,总结如下: 1对于MySQL InnoDB表的二级索引是否加入主键,官方也有明确的说明,建议线上MySQL的二级索引创建时强制加入主键所有的列,可以做到所有的MySQL 版本统一. 2.MySQL 5.6.9之前,InnoDB引擎层是会对二级索引做自动扩展,但是优化器不能识别出扩展的主键. 3.MySQL 5.6.9开始InnoDB引擎层是会对二级索引做自动扩展,优化器能识别出扩展的主键. 4.索引的大小一样,二级索引有没有加入主键列,在In

mysql之innodb引擎使用方法

前言 闲来无事做不如MySQL. 一.简介:1.Linux下使用MySQL数据库时,为了支持事务操作需要用到InnoDB引擎,对于表中处理的插入,更新等操作失败时,回滚前面不应该完成的操作是必须的. 2.一般MySQL默认的数据库引擎是MyISAM,不支持事务和外键,则可使用支持事务和外键的InnoDB引擎. 3.本笔记着重讲解MySQL的autocommit变量,如何在数据库中设置自动提交,禁止自动提交,如何在对表操作失败后回滚,对表操作成功后提交事务! 二.操作方法MySQL的autocom

MYSQL 5.6中禁用INNODB引擎

并不是所有人都需要INNODB引擎,虽然它弥补了MYSQL缺乏事务支持的毛病,但是它的磁盘性能一直是让人比较担忧的.另外比较老的PHP系统,大多是采用MYISAM引擎在MYSQL建表,似乎INNODB根本用不上场,这时候可以考虑将INNODB禁掉.在MYSQL 5.6中,直接skip-innodb前面的注释后,在my.ini中要设置一下:default-storage-engine=MYISAMdefault-tmp-storage-engine=MYISAM上面橙色字是必须要加的一行,否则MY

B-Tree索引在sqlserver和mysql中的应用

在谈论数据库性能优化的时候,通常都会提到“索引”,但很多人其实并没有真正理解索引,也没有搞清楚索引为什么就能加快检索速度,以至于在实践中并不能很好的应用索引.事实上,索引是一种廉价而且十分有效的优化手段,设计优良的索引对查询性能提升确实能起到立竿见影的效果. 相信很多读者,都了解和使用过索引,可能也看过或者听过”新华字典“.”图书馆“之类比较通俗描述,但是对索引的存储结构和本质任然还比较迷茫. 有数据结构和算法基础的读者,应该都学过或者写过“顺序查找,二分查找(折半)查找,二叉树查找”这几种很经

Mysql中的索引

众所周知,索引能够加快查询的速度,类似看书的时候先查目录之后再翻到具体那一页. 一.聚集索引和非聚集索引 1.聚集索引 聚集索引一张表只能存在一个. 聚集索引是物理上连续的(如果数据结构是btree的话,则数据直接存在叶子节点上),所以查询一个范围的数据会相当快. 2. 非聚集索引 非聚集索引一张表可以存在抖个. 非聚集索引在逻辑上是连续的(如果数据结构是btree的话,则叶子节点上存的的数据的位置信息),所以 mysql中的innodb引擎支持聚集索引,在mysql中主键索引就是聚集索引. M

InnoDB引擎索引大观

InnoDB是mysql处理OLTP(online transcation process)类型业务的存储引擎.为了加快数据查询速度,InnoDB引擎提供了丰富的索引实现. 1. 索引的分类 索引可以分为聚集索引和非聚集索引,聚簇索引(cluster) index)是指索引中键值的逻辑顺序和相应行的物理顺序一致,简单说就是索引中键值存储的是对应的行数据.非聚簇索引中索引的键值中存储的只是相应行的引用,并不代表行实际的存储.索引是在数据库的存储引擎中实现,不同的存储引擎索引的实现不一样.举例来说,

InnoDB引擎的索引和存储结构

InnoDB引擎的索引和存储结构 在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的.而MySql数据库提供了多种存储引擎.用户可以根据不同的需求为数据表选择不同的存储引擎,用户也可以根据自己的需要编写自己的存储引擎. 1.MySQL主要存储引擎的区别 MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB,另外还有MERGE.MEMORY(HEAP)等. (1)主要的几个存储引擎 MyISAM管理非事务表,提供高速存储和检索,以及全文搜

数据库:存储引擎+InnoDB+TokuDB+ MyIASM +Memory+索引+三范式等

存储引擎概念数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以 获得特定的功能.现在许多不同的数据库管理系统都支持多种不同的数据引擎. 存储引擎主要有: 1. MyIsam , 2. InnoDB, 3. Memory, 4. Archive, 5. Federated . InnoDB(B+树) InnoDB 底层存储结构为B+树, B树的每个节点对应i