mysql卡在内连接查询中

时间:2016-08-22 07:18:15

标签: php mysql database-administration mysql5

我有一个大约200个桌子的mysql数据库&每个表有大约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

即使我试过

GROUP BY od.io_id DESC
LIMIT 10

但没有运气.... mysql没有给出任何错误.. 请帮帮我吧

提前谢谢!

2 个答案:

答案 0 :(得分:0)

首先你可以添加一些索引:

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
;

你的表现现在说了什么?

答案 1 :(得分:0)

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