2016-06-14 50 views
1

我想弄清楚爲什麼我的MySQL查詢需要一分鐘才能運行。我無法弄清楚我是否低效地編程某些東西,或者我是否應該預期像這樣的滯後時間。以下是代碼。MySQL查詢的影響

請記住,在我搜索的大部分表格(貨件,shipment_financials,shipment_consignees,shipment_shippers)中,有10,000到30,000條記錄。

下面是代碼:

 
SELECT users.*, COUNT(DISTINCT shipments.shipment_id) as COUNT_SHIPMENTS, COUNT(DISTINCT clients.client_id) as COUNT_CLIENTS, SUM(shipment_financials.shipment_financial_revenue) as SUM_USER_REVENUE, SUM(shipment_financials.shipment_financial_cost) as SUM_USER_COST 
FROM users 
JOIN shipments ON shipments.shipment_details_sales_rep = users.user_id 
LEFT JOIN shipment_financials ON shipment_financials.shipment_financial_shipment_id = shipments.shipment_id 
JOIN clients ON clients.client_id = shipments.shipment_details_client 
JOIN shipment_consignees ON shipment_consignees.shipment_consignee_shipment_id = shipments.shipment_id 
JOIN shipment_shippers ON shipment_shippers.shipment_shipper_shipment_id = shipments.shipment_id 

WHERE shipment_consignees.shipment_consignee_id = (
     SELECT MAX(shipment_consignees.shipment_consignee_id) 
     FROM shipment_consignees 
     WHERE shipments.shipment_id = shipment_consignees.shipment_consignee_shipment_id 
    ) 
    AND shipment_shippers.shipment_shipper_id = (
     SELECT MIN(shipment_shippers.shipment_shipper_id) 
     FROM shipment_shippers 
     WHERE shipments.shipment_id = shipment_shippers.shipment_shipper_shipment_id 
    ) 
    AND users.user_account_id = $account_id 
    AND shipments.shipment_voided = 0 
GROUP BY users.user_id 
ORDER BY SUM_USER_REVENUE desc 
+0

因爲不是快速奔跑:大量的記錄,5連接,多個相關子查詢。 –

+0

'解釋'它,並做一些優化。 – Blank

+0

你使用索引嗎?它會大大提高性能 – andrew

回答

0

主要表現在對嵌入式查詢where語句也減慢了查詢的處理。但是,如果它不可避免並且必須使用內聯查詢,則可以通過執行「LEFT OUTER JOIN」來減少「JOIN」的輸出。

「users。*」也從表格中提取整個記錄,從而減慢了記錄速度,但仍然認爲加入是減慢查詢速度的主要問題。

0

這是連接和多個查詢。訣竅是將我需要的信息保存在一張表中(發貨表)。這樣我不必加入多個表格。

不是最有趣的方式來完成它,但人做它加快了速度!

+1

這聽起來不像是一個解決方案:-( – Strawberry

0

你可能可以解釋你的查詢使用以下命令

*explain SELECT 
     users.* 
     ,COUNT(DISTINCT shipments.shipment_id) as COUNT_SHIPMENTS 
     ,COUNT(DISTINCT clients.client_id) as COUNT_CLIENTS 
     ,SUM(shipment_financials.shipment_financial_revenue) as SUM_USER_REVENUE 
     ,SUM(shipment_financials.shipment_financial_cost) as SUM_USER_COST 
FROM users 
JOIN shipments 
    ON shipments.shipment_details_sales_rep = users.user_id 
LEFT JOIN shipment_financials 
    ON shipment_financials.shipment_financial_shipment_id = shipments.shipment_id 
JOIN clients 
    ON clients.client_id = shipments.shipment_details_client 
JOIN shipment_consignees 
    ON shipment_consignees.shipment_consignee_shipment_id = shipments.shipment_id 
JOIN shipment_shippers 
    ON shipment_shippers.shipment_shipper_shipment_id = shipments.shipment_id 
WHERE shipment_consignees.shipment_consignee_id = 
    (
     SELECT MAX(shipment_consignees.shipment_consignee_id) 
     FROM shipment_consignees 
     WHERE shipments.shipment_id = shipment_consignees.shipment_consignee_shipment_id 
    ) 
AND shipment_shippers.shipment_shipper_id = 
    (
     SELECT MIN(shipment_shippers.shipment_shipper_id) 
     FROM shipment_shippers 
     WHERE shipments.shipment_id = shipment_shippers.shipment_shipper_shipment_id 
    ) 
AND users.user_account_id = [TEST ACCOUNT ID] 
AND shipments.shipment_voided = 0 
GROUP BY users.user_id 
ORDER BY SUM_USER_REVENUE desc;* 

,將幫助您優化查詢