2014-06-18 44 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秒