MySQL查询的影响

时间:2016-06-14 05:28:17

标签: php mysql database join

我试图找出为什么我的MySQL查询需要花费一分钟才能运行。我无法弄清楚我是否编写了低效编程的东西,或者我是否应该期待这样的延迟时间。下面是代码。

请记住,在我搜索的大多数表格中都有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

3 个答案:

答案 0 :(得分:0)

主要是where语句中的内联查询也会降低查询的处理速度。但是如果它是不可避免的并且您必须使用内联查询,那么您可以通过执行“LEFT OUTER JOIN”来减少“JOIN”的输出。

“users。*”也从表中获取整个记录,因此减慢了速度,但是加入仍然是减缓查询速度的主要问题。

答案 1 :(得分:0)

这是连接和多个查询。诀窍是将我需要的信息保存在一个表(货运表)中。这样我就不必加入多个表了。

不是最有趣的方法来实现它,但是人类做到了加快速度!

答案 2 :(得分: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;*

可帮助您优化查询

相关问题