简体   繁体   中英

mysql stuck in inner join query

i have a mysql db with around 200 tables & each tables have around 40,000 entries. im trying inner join, but on xamp environment, it takes around 4hrs ... as well as on cloud it simply gives error 500 after 10 min. here is the query

SELECT DISTINCT od.io_id, od.io_date, sm.style_code, cm.short_name, od.cust_po, od.mepl_no, od.season, btm.cat_art_no, odd.rev_del_date, od.total_qty, od.shipping_qty, um.short_name, od.order_value_fc, od.order_value_inr, odd.ex_factory_date, od.repeat_order, ot.order_type_name, od.agent_commission AS return_bonus, cm.bonus_discount, cm.other_discount, od.status, il.short_name AS terms, delm.delivery_mode_name, c.short_name AS cur
FROM orderdetails od
INNER JOIN bomtrimqty btm ON od.style_id = btm.style_id
INNER JOIN orderdeliverydetails odd ON od.io_no = odd.io_no
INNER JOIN ordertype ot ON od.order_type_id = ot.order_type_id
INNER JOIN stylemaster sm ON od.style_id = sm.style_id
INNER JOIN customermaster cm ON od.customer = cm.customer_id
INNER JOIN unitmaster um ON um.unit_id = sm.unit_id
INNER JOIN deliverymodelist delm ON delm.delivery_mode_id = odd.rev_del_mode
INNER JOIN incotermlist il ON il.inco_term_id = od.order_type_id
INNER JOIN currency c ON od.fc_unit = c.currency_id
ORDER BY od.io_id DESC

even i tried

GROUP BY od.io_id DESC
LIMIT 10

but no luck .... mysql doesnt give any error .. please help me out it

Thx in advance !

First you can add some indices:

ALTER TABLE orderdetails ADD INDEX abc (customer, style_id, io_no, order_type_id, fc_unit);
ALTER TABLE bomtrimqty ADD INDEX abc (style_id);
ALTER TABLE orderdeliverydetails ADD INDEX abc (io_no, rev_del_mode);
ALTER TABLE ordertype ADD INDEX abc (order_type_id);
ALTER TABLE stylemaster ADD INDEX abc (style_id, unit_id);
ALTER TABLE customermaster ADD INDEX abc (customer_id);
ALTER TABLE unitmaster ADD INDEX abc (unit_id);
ALTER TABLE deliverymodelist ADD INDEX abc (delivery_mode_id);
ALTER TABLE incotermlist ADD INDEX abc (inco_term_id);
ALTER TABLE currency ADD INDEX abc (currency_id);

then you can change the join order:

SELECT 
DISTINCT 
  od.io_id, od.io_date, sm.style_code, cm.short_name, od.cust_po, od.mepl_no, od.season, btm.cat_art_no, odd.rev_del_date, od.total_qty, od.shipping_qty, 
  um.short_name, od.order_value_fc, od.order_value_inr, odd.ex_factory_date, od.repeat_order, ot.order_type_name, od.agent_commission AS return_bonus, 
  cm.bonus_discount, cm.other_discount, od.status, il.short_name AS terms, delm.delivery_mode_name, c.short_name AS cur
FROM orderdetails od
INNER JOIN customermaster cm ON od.customer = cm.customer_id
INNER JOIN stylemaster sm ON od.style_id = sm.style_id
INNER JOIN unitmaster um ON um.unit_id = sm.unit_id
INNER JOIN bomtrimqty btm ON od.style_id = btm.style_id
INNER JOIN orderdeliverydetails odd ON od.io_no = odd.io_no
INNER JOIN ordertype ot ON od.order_type_id = ot.order_type_id
INNER JOIN deliverymodelist delm ON delm.delivery_mode_id = odd.rev_del_mode
INNER JOIN incotermlist il ON il.inco_term_id = od.order_type_id
INNER JOIN currency c ON od.fc_unit = c.currency_id
ORDER BY od.io_id DESC
;

What does your performance say now?

请尝试使用子查询而不是许多JOIN,并在那些将在WHERE条件中使用的字段上保留索引,此策略将起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM