2012-04-19 65 views
0

我有一個查詢:使用Filesort和Query的MySQL非常慢?

SELECT listings.*, listingagents.agentid 
FROM listings 
LEFT JOIN listingagents ON (listingagents.id = listings.listingagentid) 
LEFT JOIN ignore ON (ignore.system_key = listings.listingid) 
WHERE ignore.id IS NULL 
ORDER BY listings.id ASC 

我試圖改善查詢的性能,因爲它是非常緩慢的,它是把MySQL服務器上一個沉重的負擔。

當我做了MySQL的解釋,輸出顯示:

 
+--------+-------------+---------------+--------+---------------+------------+---------+----------------------------+--------+-------------------------+ 
|  id | select_type |  table  | type | possible_keys | key  | key_len |   ref    | rows |   Extra   | 
+--------+-------------+---------------+--------+---------------+------------+---------+----------------------------+--------+-------------------------+ 
|  1 | SIMPLE  | listings  | ALL | NULL   | NULL  | NULL | NULL      | 383360 | Using filesort   | 
|  1 | SIMPLE  | listingagents | eq_ref | PRIMARY  | PRIMARY | 4  | db.listings.listingagen... |  1 |       | 
|  1 | SIMPLE  | ignore  | ref | system_key | system_key | 1  | const      | 404 | Using where; Not exists | 
+--------+-------------+---------------+--------+---------------+------------+---------+----------------------------+--------+-------------------------+ 

我試圖做一個簡單的查詢:

SELECT listings.* 
FROM listings 
ORDER BY listings.id ASC 

而且該查詢也有 「使用文件排序;」。

「listing.id」,「listingagents.id」和「ignore.id」是主鍵 「listingagents.id」和「ignore.system_key」字段有索引。

我能做些什麼來改善第一個查詢?

+0

如果你對'select * from list order by id asc'做'desc'會發生什麼? – ethrbunny 2012-04-19 17:00:46

回答

0

嘗試通過添加一些條件來減少列表範圍(目前爲383360行)。例如id> x或限制。