我试图弄清楚为什么我的MySQL查询要花一分钟多的时间才能运行。我不知道我是在编程效率低下的东西,还是期望这样的滞后时间。下面是代码。

请记住,我要搜索的大多数表(出货量,shipping_financials,shipping_consignees,shipping_shippers)之间都有10,000至30,000条记录。

这是代码:

SELECT用户。*,COUNT(DISTINCT出货量.shipment_id)为COUNT_SHIPMENTS,COUNT(DISTINCT客户端.client_id)为COUNT_CLIENTS,SUM(shipment_financials.shipment_financial_revenue)为SUM_USER_REVENUE,SUM(shipment_financials_cost_Cost_COST)
来自用户
加入货件上的货件。shipment_details_sales_rep= users.user_id
左联接shipment_financials上shipping_financials.shipment_financial_shipment_id =发货.shipment_id
在客户端上加入客户端。client_id=发货.shipment_details_client
在shipping_consignees.shipment_consignee_shipment_id =上将JOINships_consignees加入到shipments.shipment_id
JOINshipment_shippers上shipping_shippers.shipment_shipper_shipment_id =发货.shipment_id

WHERE shipping_consignees.shipment_consignee_id =(
        SELECT MAX(shipment_consignees.shipment_consignee_id)
        来自货件收货人
        WHEREships.shipment_id =货件收货人.shipment_consignee_shipment_id
    )
    AND shipping_shippers.shipment_shipper_id =(
        SELECT MIN(shipment_shippers.shipment_shipper_id)
        来自shipping_shippers
        在哪里shippings.shipment_id = shipping_shippers.shipment_shipper_shipment_id
    )
    AND users.user_account_id = $ account_id
    AND shippings.shipment_voided = 0
GROUP BY users.user_id
按SUM_USER_REVENUE命令排序

最佳答案

主要是where语句中的内联查询也减慢了查询的处理速度。但是,如果不可避免,则必须使用内联查询,则可以通过执行“ LEFT OUTER JOIN”来减少“ JOIN”的输出。

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

关于php - MySQL查询的影响,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37803618/

10-16 12:16
查看更多