2016-08-22 70 views
1

我有一個大約200個表的mysql db &每個表都有大約40,000個條目。 我嘗試了內部連接,但是在xamp環境下,大約需要4小時 ...以及在雲上,它只會在10分鐘後給出錯誤500。 這裏是查詢mysql卡在內連接查詢

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的犯規給任何錯誤.. 請幫我出這

Thx提前!

+4

你有加入所有列的索引嗎?還有一個關於'orderdetails.io_id'的索引? – Barmar

+0

同意@Barmar - 這可能是與某些列缺少索引有關的問題。 – Cyclonecode

+0

我建議由於腳本時間限制500錯誤...檢查php error_reporting設置以獲取更多信息.. – simialbi

回答

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 
; 

什麼是你的表現可說的?

+0

while ALTER ---'錯誤代碼:1072.表' –

+0

表中不存在關鍵列'rev_del_mode'抱歉... ALTER工作正常......但連接訂單查詢需要5小時 –

+0

WHERE是'EXPLAIN的輸出SELECT yourQuery'? – Benvorth

0

請嘗試使用子查詢而不是許多JOIN,並且對那些將在WHERE條件下使用的字段保留索引,這種策略將起作用。