MySQL查詢?nèi)繑?shù)據(jù)集結(jié)果不一致問題解決方案
最近出現(xiàn)一個(gè)很奇怪的MySQL問題,使用不同select語(yǔ)句查詢?nèi)繑?shù)據(jù)集居然得到不同的記錄數(shù)。select * 得到4條記錄,select 字段得到的是3條記錄。
具體問題可以看下面的查詢結(jié)果:
[sql]
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,才找到問題的所在。
[sql]
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 |
+--------------------+-------+----------+-------------------------------------------------------+
查詢數(shù)據(jù)不一致的原因是table_myisam的索引文件損壞了,對(duì)應(yīng)的索引文件table_myisam.MYI與數(shù)據(jù)文件table_myisam.MYD不一致。select *并不需要遍歷每個(gè)索引項(xiàng),只需要獲取第一條記錄,根據(jù)鏈表順序訪問,因此當(dāng)前的索引損壞并沒有影響到select *的使用。而select uid需要遍歷所有索引項(xiàng),因而只獲取到損壞狀態(tài),三條索引記錄。
解決方案是使用repair table進(jìn)行表索引的修復(fù)。
[sql]
mysql> repair table table_myisam;
+--------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+--------------------+--------+----------+----------+
| qitai.table_myisam | repair | status | OK |
+--------------------+--------+----------+----------+
1 row in set (0.00 sec)
修復(fù)后使用check table可以看到表狀態(tài)變成正常,使用select *與select uid都能獲取到4條記錄。
[sql]
mysql> check table table_myisam;
+--------------------+-------+----------+----------+
| Table | Op| Msg_type | Msg_text |
+--------------------+-------+----------+----------+
| qitai.table_myisam | check | status | OK |
+--------------------+-------+----------+----------+
1 row in set (0.00 sec)
相關(guān)文章
Mysql GROUP BY查詢每組某值最大的一條數(shù)據(jù)
這篇文章主要介紹了Mysql GROUP BY查詢每組某值最大的一條數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
mysql中的join和where優(yōu)先級(jí)順序解讀
這篇文章主要介紹了mysql中的join和where優(yōu)先級(jí)順序解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
如何區(qū)分MySQL的innodb_flush_log_at_trx_commit和sync_binlog
這篇文章主要介紹了如何區(qū)分MySQL的innodb_flush_log_at_trx_commit和sync_binlog,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2021-02-02
mysql 5.7.18 安裝配置方法圖文教程(CentOS7)
這篇文章主要為大家詳細(xì)介紹了CentOS 7下mysql 5.7.18 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
mysql默認(rèn)編碼為UTF-8 通過修改my.ini實(shí)現(xiàn)方法
這篇文章主要介紹了mysql默認(rèn)編碼為UTF-8 通過修改my.ini實(shí)現(xiàn)方法的相關(guān)資料,為了防止出現(xiàn)亂碼,Latin1是不支持漢字的,所以要將其改為UTF-8或GBK,需要的朋友可以參考下2017-01-01

