2011-08-10 54 views
0
SELECT a.tag,CONCAT(u.first_name,' ',u.last_name) 
FROM assets a 
LEFT JOIN (SELECT asset_id,assigned_to_id 
     FROM asset_activity 
     WHERE assigned IN (SELECT MAX(assigned) 
          FROM asset_activity 
          GROUP BY asset_id)) 
     v ON v.asset_id = a.id 
LEFT JOIN users u ON v.assigned_to_id = u.id 
WHERE ($1 IS NULL OR u.last_name LIKE $1)  

由於MySQL在子查詢上執行了左連接,所以我需要找到一些其他的方法來做到這一點。我可以在select中選擇我需要的子查詢,但它需要有條件。它只應該返回與LIKE匹配的記錄,並且對於子查詢,它仍然會返回來自assets的記錄,其中assign_to的值爲null,所以我不能這樣做。在MySQL中使用條件的子查詢左連接?

執行計劃:

id select_type  table   type  possible_keys key  key_len ref rows    Extra  
1 PRIMARY   a    ALL  null   null  null null    1,447 
1 PRIMARY   <derived2>  ALL  null   null  null null    1,396 
1 PRIMARY   u    eq_ref PRIMARY  PRIMARY 4  v.assigned_to_id 1 
2 DERIVED   asset_activity ALL  null   null  null null    1,400 Using where 
3 DEPENDENT SUBQUERY asset_activity index null   asset_id 4  null    1,400 Using filesort 

指標:

Table Non_unique  Key_name Seq_in_index  Column_name  Collation Cardinality Sub_part Packed Null Index_type Comment 
assets 0     PRIMARY 1     id    A   144   ""      BTREE  ""  
assets 1     serial 1     serial   A   1447       YES BTREE  "" 
assets 1     serial 2     cal_num   A   1447       YES BTREE  "" 

Table   Non_unique Key_name   Seq_in_index Column_name  Collation Cardinality Sub_part Packed Null Index_type Comment 
asset_activity 0   PRIMARY   1   id    A   1400  ""     BTREE  "" 
asset_activity 1   asset_id   1   asset_id   A      ""     BTREE  "" 
asset_activity 1   location_id  1   location_id  A          YES BTREE  "" 
asset_activity 1   assigned_to_id 1   assigned_to_id A          YES BTREE  "" 
asset_activity 1   assigned_to_table 1   assigned_to_table A          YES BTREE  "" 
asset_activity 1   created   1   created   A      ""     BTREE  "" 
+0

請提供表格架構,索引,執行計劃結果 – ajreal

+0

I' m不知道如何格式化這些數據,所以它的可讀性。 – Kyro

+0

http://meta.stackexchange.com/questions/72082/changes-to-syntax-highlighting http://google-code-prettify.googlecode.com/ svn/trunk/README.html – ajreal

回答

0

OK,下面是我終於得到它執行:

SELECT a.tag,CONCAT(u.first_name,' ',u.last_name) 
FROM assets a  
LEFT JOIN asset_activity v ON v.asset_id = a.id 
LEFT JOIN asset_activity v2 ON v2.asset_id = a.id 
    AND (v.assigned < v2.assigned OR v.assigned = v2.assigned AND v.id < v2.id) 
LEFT JOIN users u ON v.assigned_to_id = u.id    
WHERE v2.id IS NULL 
    AND ($1 IS NULL OR u.last_name LIKE $1) 

這執行遠遠比左邊更優的連接上帶有子查詢的子查詢。 .0064秒,而另一種方法則爲5秒或更長時間,並且在將其綁定到我的代碼中的網格時用於尋呼等,將其變爲更多。