您好,登錄后才能下訂單哦!
這篇文章主要介紹MySQL中查詢數(shù)據(jù)不一致怎么辦,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
最近出現(xiàn)一個很奇怪的MySQL問題,使用不同select語句查詢?nèi)繑?shù)據(jù)集居然得到不同的記錄數(shù).select * 得到4條記錄,select 字段得到的是3條記錄.
具體問題可以看下面的查詢結(jié)果:
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 |
+--------------------+-------+----------+-------------------------------------------------------+
查詢數(shù)據(jù)不一致的原因是table_myisam的索引文件損壞了,對應的索引文件table_myisam.MYI與數(shù)據(jù)文件 table_myisam.MYD不一致.select *并不需要遍歷每個索引項,只需要獲取第一條記錄,根據(jù)鏈表順序訪問,因此當前的索引損壞并沒有影響到select *的使用.而select uid需要遍歷所有索引項,因而只獲取到損壞狀態(tài),三條索引記錄.
解決方案是使用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可以看到表狀態(tài)變成正常,使用select *與select uid都能獲取到4條記錄.
mysql> check table table_myisam;
+--------------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+--------------------+-------+----------+----------+
| qitai.table_myisam | check | status | OK |
+--------------------+-------+----------+----------+
1 row in set (0.00 sec
以上是“MySQL中查詢數(shù)據(jù)不一致怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。