2014-06-18 46 views
0

我有一個非常大的MYSQL查詢處理超過10分鐘顯示記錄的問題。加快MYSQL LARGE查詢

查詢看起來是這樣的:

SELECT SQL_CALC_FOUND_ROWS * FROM (
     SELECT 't-0' as typo, sh_inquiry.id as ref, sh_inquiry.pid as pid, partner, sh_inquiry.order, if(p_status IS NULL OR p_status = 0,'not paid','paid') as payment, p_voucher, country, city, delivery_date, sh_inquiry.amount as c_amount, currency as c_currency, NULL as s_amount, NULL as s_currency, IF(confirmed=0,if(delivered=0,"not confirmed","delivered"),if(delivered=0,"confirmed","delivered")) as conf 
     FROM sh_inquiry 
     LEFT JOIN sh_orders ON sh_inquiry.id = sh_orders.i_id 
       LEFT JOIN sh_partners ON sh_partners.pid = sh_inquiry.pid 
       LEFT JOIN sh_currency ON sh_currency.id = sh_inquiry.curr_id 
       LEFT JOIN sh_country ON sh_country.id = sh_inquiry.cid 
       LEFT JOIN sh_debts ON sh_inquiry.id = sh_debts.i_id 
     WHERE sh_inquiry.del = 0 AND sh_inquiry.type = 1 AND sh_orders.del = 0 
     UNION 
       SELECT 't-1' as typo, sh_orders.i_id as ref, sid as pid, partner, sh_inquiry.order, if(p_status IS NULL OR p_status = 0,'not paid','paid') as payment, p_voucher, country, city, delivery_date, NULL as c_amount, NULL as c_currency, sh_orders.amount as s_amount, currency as s_currency, IF(confirmed=0,if(delivered=0,'not confirmed','delivered'),if(delivered=0,'confirmed','delivered')) as conf FROM sh_orders 
       LEFT JOIN sh_partners ON sh_partners.pid = sh_orders.sid 
       LEFT JOIN sh_currency ON sh_currency.id = sh_orders.curr_id 
       LEFT JOIN sh_inquiry ON sh_inquiry.id = sh_orders.i_id 
       LEFT JOIN sh_country ON sh_country.id = sh_inquiry.cid 
       LEFT JOIN sh_debts ON sh_orders.id = sh_debts.o_id 
       WHERE sh_orders.del = 0 AND sh_inquiry.del = 0 
     ) AS U 

     ORDER BY typo ASC, delivery_date 
        asc 
     LIMIT 0, 10 

的問題是,我使用的2臺必須在一個單一的一個(那2個表是非常大的)被加入,這就是爲什麼我使用UNION聲明。我正在jquery datatables serverside頁面中顯示記錄,因此限制顯示的記錄不會幫助原因。

任何人都可以想出一個關於加速這個查詢的ideea? 如果您需要更多關於數據庫結構的信息,請詢問。

的EXPLAIN statment觸發:

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 PRIMARY  <derived2> ALL  NULL NULL NULL NULL 17630 Using filesort 
2 DERIVED  sh_orders ALL  i_id NULL NULL NULL 8696 Using where 
2 DERIVED  sh_inquiry eq_ref PRIMARY,id PRIMARY  4 reur3918_sh.sh_orders.i_id 1 Using where 
2 DERIVED  sh_partners  eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_inquiry.pid 1 
2 DERIVED  sh_currency  eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_inquiry.curr_id 1 
2 DERIVED  sh_country eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_inquiry.cid 1 
2 DERIVED  sh_debts ALL  NULL NULL NULL NULL 18678 
3 UNION sh_orders ALL  i_id NULL NULL NULL 8696 Using where 
3 UNION sh_partners  eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_orders.sid 1 
3 UNION sh_currency  eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_orders.curr_id 1 
3 UNION sh_inquiry eq_ref PRIMARY,id PRIMARY  4 reur3918_sh.sh_orders.i_id 1 Using where 
3 UNION sh_country eq_ref PRIMARY  PRIMARY  4 reur3918_sh.sh_inquiry.cid 1 
3 UNION sh_debts ALL  NULL NULL NULL NULL 18678 
NULL UNION RESULT <union2,3> ALL  NULL NULL NULL NULL NULL  

索引的所有ID後它查詢花費約133秒..

+0

基本經驗法則:將索引放在決策上下文中使用的任何字段上。 '哪裏','加入','訂單','組合等等... –

+0

每個表中有多少行?它會給我們一個想法,你可能已經嘗試過哪些類型的優化,以及你真正面臨的問題。 –

+1

爲您的查詢執行EXPLAIN,查看它實際使用的索引是如何執行的 –

回答

0

謝謝大家對你的想法! 其實,運行EXPLAIN語句幫助我找到至少沒有定義INDEX的表。 將所有已用於查詢的字段(主要在JOIN ON子句中)中添加INDEX後,運行時間減少到0.83秒