• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

解决MySQL查询数据不一致诟病_MySQL

mysql 搞代码 4年前 (2022-01-09) 27次浏览 已收录 0个评论

gaodaima.com

最近出现一个很奇怪的MySQL问题,使用不同select语句查询全部数据集居然得到不同的记录数.select * 得到4条记录,select 字段得到的是3条记录。

具体问题可以看下面的查询结果:
mysql> select * from table_myisam;
+———-+——-+———–+——+
| datetime | uid | content | type |
+———-+——-+———–+——+
| 1 | uid_1 | content_1 | 1 |
| 2 | uid_2 | content_2 | 1 |
| 4 | uid_4 | content_4 | 1 |
| 3 | uid_3 | content_3 | 1 |
+———-+——-+———–+——+
4 rows in set (0.00 sec)
mysql> select uid from table_myisam;
+——-+
| uid |
+——-+
| uid_1 |
| uid_2 |
| uid_4 |
+——-+
3 rows in set (0.00 sec)

通过select uid只得到3行记录,丢失了其中uid='uid_3‘的记录.本来百思不得其解,后来在同事的提醒下使用了check table,才找到问题的所在.
mysql> check table table_myisam;
+——————–+——-+———-+——————————————————-+
| Table | Op | Msg_type | Msg_text |
+——————–+——-+———-+——————————————————-+
| qitai.table_myisam | check | warning | 1 client is using or hasn't closed the table properly |
| qitai.table_myisam | check | warning | Size of indexfile is: 2049 Should be: 2048 |
| qitai.table_myisam | check | error | Found 3 keys of 4 |
| qitai.table_myisam | check | error | Corrupt |
+——————–+——-+———-+——————————————————-+

查询数据不一致的原因是table_myisam的索引文件损坏了,对应的索引文件table_myisam.MYI与数据文件 table_myisam.MYD不一致.select *并不需要遍历每个索引项,只需要获取第一条记录,根据链表顺序访问,因此当前的索引损坏并没有影响到select *的使用.而select uid需要遍历所有索引项,因而只获取到损坏状态,三条索引记录。

解决方案是使用repair table进行表索引的修复。
mysql> repair table table_myisam;
+——————–+——–+———-+———-+
| Table | Op | Msg_type | Msg_text |
+——————–+——–+———-+———-+
| qitai.table_myisam | repair | status | OK |
+——————–+——–+———-+———-+
1 row in set (0.00 sec)

修复后使用check table可以看到表状态变成正常,使用select *与select uid都能获取到4条记录.
mysql> check table table_myisam;

本文来源gao!daima.com搞$代!码#网#

+——————–+——-+———-+———-+
| Table | Op | Msg_type | Msg_text |
+——————–+——-+———-+———-+
| qitai.table_myisam | check | status | OK |
+——————–+——-+———-+———-+
1 row in set (0.00 sec)

gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解决MySQL查询数据不一致诟病_MySQL

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址