在一個MySQL 5.1.34服務器,我有以下困窘的形勢:爲什麼mysql決定這個子查詢是相關的?
mysql> explain select * FROM master.ObjectValue WHERE id IN (SELECT id FROM backup.ObjectValue) AND timestamp < '2008-04-26 11:21:59';
+----+--------------------+-------------+-----------------+-------------------------------------------------------------+------------------------------------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------------+-----------------+-------------------------------------------------------------+------------------------------------+---------+------+--------+-------------+
| 1 | PRIMARY | ObjectValue | range | IX_ObjectValue_Timestamp,IX_ObjectValue_Timestamp_EventName | IX_ObjectValue_Timestamp_EventName | 9 | NULL | 541944 | Using where |
| 2 | DEPENDENT SUBQUERY | ObjectValue | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using index |
+----+--------------------+-------------+-----------------+-------------------------------------------------------------+------------------------------------+---------+------+--------+-------------+
2 rows in set (0.00 sec)
mysql> select * FROM master.ObjectValue WHERE id IN (SELECT id FROM backup.ObjectValue) AND timestamp < '2008-04-26 11:21:59';
Empty set (2 min 48.79 sec)
mysql> select count(*) FROM master.ObjectValue;
+----------+
| count(*) |
+----------+
| 35928440 |
+----------+
1 row in set (2 min 18.96 sec)
- 怎麼會需要3分鐘檢查50萬個記錄時,只需要 2分鐘訪問所有記錄?
- 如何將 上的子查詢分開獨立的數據庫進行分類?
- 我該怎麼做才能加快 這個查詢?
UPDATE:
是花了很長一段時間的實際查詢是DELETE,但你不能做的解釋; DELETE是我爲什麼使用子選擇。我現在有閱讀文檔,發現有關語法「DELETE FROM T使用......」從改寫查詢:
DELETE FROM master.ObjectValue
WHERE timestamp < '2008-06-26 11:21:59'
AND id IN (SELECT id FROM backup.ObjectValue) ;
到:
DELETE FROM m
USING master.ObjectValue m INNER JOIN backup.ObjectValue b ON m.id = b.id
WHERE m.timestamp < '2008-04-26 11:21:59';
減少時間從幾分鐘.01秒空的備份.ObjectValue。
謝謝大家的好建議。
一個COUNT(*)要快很多。是master.ObjectValue表或視圖? – Johan 2012-04-26 09:54:30