《MYSQL數據庫MySQL查詢全部數據集結果不一致問題解決方案》要點:
本文介紹了MYSQL數據庫MySQL查詢全部數據集結果不一致問題解決方案,希望對您有用。如果有疑問,可以聯系我們。
最近出現一個很奇怪的MySQL問題,使用不同select語句查詢全部數據集居然得到不同的記錄數.select * 得到4條記錄,select 字段得到的是3條記錄.
具體問題可以看下面的查詢結果:??
[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?? |?
+--------------------+-------+----------+-------------------------------------------------------+?
查詢數據不一致的原因是table_myisam的索引文件損壞了,對應的索引文件table_myisam.MYI與數據文件table_myisam.MYD不一致.select *并不需要遍歷每個索引項,只需要獲取第一條記錄,根據鏈表順序訪問,因此當前的索引損壞并沒有影響到select *的使用.而select uid需要遍歷所有索引項,因而只獲取到損壞狀態,三條索引記錄.
?? 解決方案是使用repair table進行表索引的修復.
[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)?
修復后使用check table可以看到表狀態變成正常,使用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)? MYSQL實例
《MYSQL數據庫MySQL查詢全部數據集結果不一致問題解決方案》是否對您有啟發,歡迎查看更多與《MYSQL數據庫MySQL查詢全部數據集結果不一致問題解決方案》相關教程,學精學透。維易PHP學院為您提供精彩教程。