【问题标题】:Impact of MySQL QueryMySQL 查询的影响
【发布时间】:2016-10-14 16:34:20
【问题描述】:

我试图弄清楚为什么我的 MySQL 查询需要一分钟多的时间才能运行。我不知道我是否在编程效率低下,或者我是否应该期待这样的滞后时间。下面是代码。

请记住,在我搜索的大多数表(shipments、shipping_financials、shipping_consignees、shipping_shippers)中都有 10,000 到 30,000 条记录。

代码如下:

选择用户。*,COUNT(DISTINCT shippings.shipment_id) 作为 COUNT_SHIPMENTS,COUNT(DISTINCT clients.client_id) 作为 COUNT_CLIENTS,SUM(shipment_financials.shipment_financial_revenue) 作为 SUM_USER_REVENUE,SUM(shipment_financials.shipment_financial_cost) 作为 SUM_USER_COST 来自用户 在 shipping.shipment_details_sales_rep = users.user_id 上加入货件 左加入 shipping_financials ON shipping_financials.shipment_financial_shipment_id = shipping.shipment_id 在 clients.client_id = shipping.shipment_details_client 上加入客户 加入 shipping_consignees ON shipping_consignees.shipment_consignee_shipment_id = shipping.shipment_id 加入 shipping_shippers ON shipping_shippers.shipment_shipper_shipment_id = shipping.shipment_id WHERE shipping_consignees.shipment_consignee_id = ( 选择最大(shipment_consignees.shipment_consignee_id) FROM shipping_consignees WHERE shipping.shipment_id = shipping_consignees.shipment_consignee_shipment_id ) AND shipping_shippers.shipment_shipper_id = ( 选择最小值(shipment_shippers.shipment_shipper_id) FROM shipping_shippers WHERE shipping.shipment_id = shipping_shippers.shipment_shipper_shipment_id ) AND users.user_account_id = $account_id AND shipping.shipment_voided = 0 GROUP BY users.user_id ORDER BY SUM_USER_REVENUE desc

【问题讨论】:

  • 运行不快,因为:大量记录、5 个连接、多个相关子查询。
  • EXPLAIN它,做一些优化。
  • 你使用索引吗?它将大大提高性能
  • @andrew - 你能解释一下索引是什么意思吗?

标签: php mysql database join


【解决方案1】:

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

“users.*”也从表中获取全部记录,因此减慢了它的速度,但连接仍然是这里减慢查询的主要问题。

【讨论】:

    【解决方案2】:

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

    不是最有趣的方式来完成它,但人类确实加快了速度!

    【讨论】:

    • 这听起来不像是一个解决方案:-(
    【解决方案3】:

    您可以使用以下命令解释您的查询

    *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;*
    

    这将帮助您优化查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多