【问题标题】:MySQL query takes time to runMySQL 查询需要时间来运行
【发布时间】:2012-05-24 14:21:49
【问题描述】:

我有一个 MySQL 查询需要很长时间才能运行。

这是我的查询:

SELECT DISTINCT *,ROUND((total_gross_profit- (payment_processor_holdout+payment_processor_fee)-(affiliate_commission_base+affiliate_commission_markup)),2) as net_profit FROM (SELECT  O.id as oid
,M.last_name
,M.first_name
,D.product_name
,D.dosage_name
,O.date
,O.delivery
,O.aff_id
,IFNULL(O.product_total,0) as product_price
,IFNULL(O.discount_amount,0) as purchase_discount 
,IFNULL((O.product_total - O.discount_amount),0) as net_sales
,IFNULL(ROUND((O.total - O.product_total),2),0) as shipping_fee
,IFNULL(ROUND(((O.product_total - O.discount_amount) - (O.total - O.product_total)),2),0)  as gross_sales
,IFNULL(P.unit_cost,0) as product_unit_cost
,IFNULL((P.unit_cost * D.dosage_name),0) as costofgoods
,aset.setting_value as shipping_cost
,IFNULL(ROUND(((O.product_total - O.discount_amount) - (O.total - O.product_total) - (P.unit_cost * D.dosage_name)),2),0)  as goods_gross_profit
,IFNULL(ROUND(((O.total - O.product_total) - aset.setting_value),2),0) as shipping_gross_profit
,(IFNULL(ROUND(((O.product_total - O.discount_amount) - (O.total - O.product_total) - (P.unit_cost * D.dosage_name)),2),0)+ IFNULL(ROUND(((O.total - O.product_total) - aset.setting_value),2),0)) as total_gross_profit
,IFNULL(ROUND(IF(E.category_id IN (95,96,97,98,99),E.price*.25,E.price*.40),2),0) AS affiliate_commission_base
,IFNULL(ROUND((F.price_markup*.50),2),0) AS affiliate_commission_markup
,IFNULL((SELECT ppf.value FROM `aff_settings` ap
LEFT JOIN `payment_processor_fee` ppf
ON ap.setting_value=ppf.payment_processor_type
WHERE ap.setting_name='payment_processor'),0) as payment_processor_fee
,IFNULL(ROUND(((SELECT pph.value FROM `aff_settings` ap
LEFT JOIN `payment_processor_holdout` pph
ON ap.setting_value=pph.payment_processor_type
WHERE ap.setting_name='payment_processor')* O.total),2),0) as payment_processor_holdout
    FROM  `order` as O 
    LEFT JOIN `order_detail` as D 
    ON O.id = D.order_id
    LEFT JOIN `members` as M
    ON M.id = O.buyer_id
    LEFT JOIN `products` as P
    ON P.generic_name = D.product_name
    LEFT JOIN `aff_settings` as aset
    ON aset.setting_name = O.delivery 
    LEFT JOIN `aff_order_details` as E
    ON D.id = E.order_detail_ref_id
    LEFT JOIN `aff_group_product_prices` as F
    ON F.aff_id = O.aff_id
    AND F.product_id =P.Id
    AND F.dosage_id=D.dosage_name
ORDER BY M.last_name) AS x

我有很多连接的表。有什么方法可以让查询运行得更快吗?如果有,怎么做?

感谢您的帮助。

【问题讨论】:

  • 您可能希望格式化您的查询,以便我们也可以查看所有子查询的位置,而无需搜索它。一点缩进可以为理解创造奇迹......
  • this 会给你一些想法。

标签: mysql performance


【解决方案1】:

对该查询运行 EXPLAIN PLAN。您可能会看到 TABLE SCAN,这意味着您必须添加索引和/或重写查询。

【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多