Mysql卡在了内连接查询中



我有一个mysql数据库,大约有200个表&每个表大约有40,000个条目。我正在尝试内连接,但在xamp环境中,它需要大约4hrs…以及在云上,它只是在10分钟后给出错误500。下面是查询

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 try

GROUP BY od.io_id DESC
LIMIT 10

但没有运气....Mysql不给任何错误…请帮帮我吧

请尝试使用子查询而不是许多join,并在那些您将在WHERE条件下使用的字段上保留索引,这种策略将有效。

首先可以添加一些索引:

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);

则可以更改连接顺序:

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
;

你现在的表现如何?

最新更新