0
下面是一個簡單的查詢,其中聯合運算符首先被執行,無論如何。 mysql執行這個查詢的方式太糟糕了。如何讓mysql更好地執行聯合查詢
mysql> explain select * from (select * from schema1.Application a union select * from schema2.Application b) c where c.SubmittedTime > now();
+----+--------------+------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+------+---------------+------+---------+------+--------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 559938 | Using where |
| 2 | DERIVED | a | ALL | NULL | NULL | NULL | NULL | 554541 | |
| 3 | UNION | b | ALL | NULL | NULL | NULL | NULL | 554541 | |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+------+---------------+------+---------+------+--------+-------------+
4 rows in set (1 min 1.57 sec)
在這種情況下的where子句可以在每個聯合操作數內部採用。這也是非常自然的做法。大家都知道
(a + b)/c = a/c + b/c
有沒有辦法強制mysql以最有效的方式執行查詢?
聯盟是一個集合操作,我真的不認爲你上面說的是真實的。換句話說,如果這對集合論有好處,這對於關係數據庫管理系統來說應該是非常好的。我真的相信優化應該是非常有可能的。但我很高興知道這不是在MySQL中的情況。 – Ram 2011-04-04 10:17:41