2010-07-29 168 views
1

我有以下查詢:優化SQL查詢在MySQL數據庫

SELECT * , r.id, x.real_name AS u_real_name, u.real_name AS v_real_name, y.real_name AS v_real_name2 
FROM url_urlaube r 
LEFT JOIN g_users u ON (r.v_id = u.id) 
LEFT JOIN g_users x ON (r.u_id = x.id) 
LEFT JOIN g_users y ON (r.v_id2 = y.id) 
WHERE (
(
FROM_UNIXTIME(1283205600) >= r.from 
AND FROM_UNIXTIME(1283205600) <= r.to 
) 
OR (
FROM_UNIXTIME(1280613600) >= r.from 
AND FROM_UNIXTIME(1280613600) <= r.to 
) 
OR (
FROM_UNIXTIME(1280613600) < r.from 
AND FROM_UNIXTIME(1283205600) > r.to 
) 
) 
ORDER BY r.from ASC 

我試圖通過增加對(r.from,r.to)的指數,以優化這個查詢,但我並沒有奏效。

當我讓我解釋一下這個查詢它說:

+----+-------------+-------+--------+---------------+---------+---------+-------------+------+-----------------------------+ 
| id | select_type | table | type | possible_keys | key  | key_len | ref   | rows | Extra      | 
+----+-------------+-------+--------+---------------+---------+---------+-------------+------+-----------------------------+ 
| 1 | SIMPLE  | r  | ALL | from   | NULL | NULL | NULL  | 42 | Using where; Using filesort | 
| 1 | SIMPLE  | u  | eq_ref | PRIMARY  | PRIMARY | 4  | res.r.v_id | 1 |        | 
| 1 | SIMPLE  | x  | eq_ref | PRIMARY  | PRIMARY | 4  | res.r.u_id | 1 |        | 
| 1 | SIMPLE  | y  | eq_ref | PRIMARY  | PRIMARY | 4  | res.r.v_id2 | 1 |        | 
+----+-------------+-------+--------+---------------+---------+---------+-------------+------+-----------------------------+ 
4 rows in set (0.00 sec) 

爲什麼呢?

謝謝你的幫助。

+0

url_urlaube表中有多少條記錄 - 以及查詢返回多少條記錄? – 2010-07-29 07:12:17

+0

現在很少,但數據庫將在生產中增長 – 2010-07-29 07:13:21

回答

0

複合指數不會幫助你,因爲你有不同的比較功能的條件。嘗試在r.from和r.to上添加單獨的索引。實際上,我認爲r.from上的一個索引就足夠了。