0

以下は、最適化する必要がある私のクエリです。

SELECT
        UPPER(IFNULL(op.supplier_payment_method,s.default_payment_method)) AS Payment_Method,
        op.supplier_payment_date AS Payment_Date,
        Date_format(IFNULL(op.supplier_payment_date,op.ship_date),'%M %Y') AS Payment_Month,
        s.supplier_name AS Farm,
        op.sub_order_id AS Order_num,
        Date_format(op.ship_date,'%b-%d-%Y') AS Ship_Date,
        op.farm_credit AS Farm_Credit,
        op.credit_memo AS Credit_Memo,
        op.credit_description AS Credit_Description,
        opb.boxes AS Box_Type,
        CONCAT('$',FORMAT(op.box_charge,2)) AS Box_Charge,
        op.invoice_num AS Invoice_num,
        CONCAT('$',FORMAT(op.invoice_amt,2)) AS Invoice_Amt,
        CONCAT('$',FORMAT(op.total_invoice_amt,2)) AS Total_Invoice_Amt,
        CONCAT(op.UM_qty,' ',op.UM_type) AS St_Bu_Qty,
        op.PO_Product_Name AS Invoice_desc,
        CONCAT('$',FORMAT((op.price_um*op.um_qty),2)) AS Cost_product_cms,
        op.supplier_invoice_note AS Supplier_Invoice_Notes,
        CONCAT('$',FORMAT(op.cms_invoice_cost,2)) AS CMS_Invoice_diff,
        CONCAT('$',FORMAT(op.total_farm_cost,2)) AS Farm_Cost
    FROM 
        orders_products op
        INNER JOIN 
            suppliers s ON s.supplier_id = op.supplier_name
        LEFT JOIN
            (
                SELECT 
                    sub_order_id, 
                    GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ') AS boxes
                FROM
                    order_products_boxes opb
                    INNER JOIN box_options bo ON bo.id=opb.box_type_id
                GROUP BY
                    opb.sub_order_id
            ) opb ON opb.sub_order_id = op.sub_order_id
    WHERE
        op.order_active=0
        AND op.ship_date>='2013-03-01'
        AND op.ship_date<='2013-04_01'
    ORDER BY op.ship_date DESC

ご覧のとおり、クエリは 4 つのテーブルで構成されており、それぞれに約 20k ~ 30k 行が含まれています。そのため、サブクエリを追加するとすぐに、クエリが非常に遅くなります。わずか 500 行のレコードを取得するのに約 1.5 分かかります。単一のクエリ内で高速化する方法はありますか?

4

2 に答える 2

0

サブボックスオプションを事前に連結する左結合クエリは、ENTIRE DATABASE FIRST をクエリしてから、外側の WHERE 句に対して制限している基準内にあるものだけに結合します。少し手間がかかるかもしれませんが、同じ外部基準を内部クエリに引き込むことは非常に役立ちます。行ごとにフィールドレベルのsql-selectを実行すると、パフォーマンスが低下する可能性があります。

「FROM」句でのみ変更する必要があります...

from
   orders_products op
      INNER JOIN suppliers s 
         ON op.supplier_name = s.supplier_id 
      LEFT JOIN
         ( SELECT 
                 opb2.sub_order_id, 
                 GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ') AS boxes
              FROM
                 orders_products op2
                    JOIN order_products_boxes opb2
                       on op2.sub_order_id = opb2.sub_order_id
                       INNER JOIN box_options bo 
                          ON opb2.box_type_id = bo.id
              WHERE
                     op2.order_active = 0
                 AND op2.ship_date >= '2013-03-01'
                 AND op2.ship_date <= '2013-04_01'
              GROUP BY
                 opb2.sub_order_id  ) opb 
         ON op.sub_order_id = opb.sub_order_id
WHERE
       op.order_active = 0
   AND op.ship_date >= '2013-03-01'
   AND op.ship_date <= '2013-04_01'
于 2013-04-04T12:55:24.663 に答える
0
SELECT
    UPPER(IFNULL(op.supplier_payment_method,s.default_payment_method)) AS Payment_Method,
    op.supplier_payment_date AS Payment_Date,
    Date_format(IFNULL(op.supplier_payment_date,op.ship_date),'%M %Y') AS Payment_Month,
    s.supplier_name AS Farm,
    op.sub_order_id AS Order_num,
    Date_format(op.ship_date,'%b-%d-%Y') AS Ship_Date,
    op.farm_credit AS Farm_Credit,
    op.credit_memo AS Credit_Memo,
    op.credit_description AS Credit_Description,
     (
            SELECT 
                GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ') 
            FROM
                order_products_boxes opb
                INNER JOIN box_options bo ON bo.id=opb.box_type_id
            GROUP BY
                opb.sub_order_id
        ) AS Box_Type,
    CONCAT('$',FORMAT(op.box_charge,2)) AS Box_Charge,
    op.invoice_num AS Invoice_num,
    CONCAT('$',FORMAT(op.invoice_amt,2)) AS Invoice_Amt,
    CONCAT('$',FORMAT(op.total_invoice_amt,2)) AS Total_Invoice_Amt,
    CONCAT(op.UM_qty,' ',op.UM_type) AS St_Bu_Qty,
    op.PO_Product_Name AS Invoice_desc,
    CONCAT('$',FORMAT((op.price_um*op.um_qty),2)) AS Cost_product_cms,
    op.supplier_invoice_note AS Supplier_Invoice_Notes,
    CONCAT('$',FORMAT(op.cms_invoice_cost,2)) AS CMS_Invoice_diff,
    CONCAT('$',FORMAT(op.total_farm_cost,2)) AS Farm_Cost
FROM 
    orders_products op
    INNER JOIN 
        suppliers s ON s.supplier_id = op.supplier_name
    LEFT JOIN

                order_products_boxes opb (
                INNER JOIN box_options bo ON bo.id=opb.box_type_id
        ) opb ON opb.sub_order_id = op.sub_order_id
WHERE
    op.order_active=0
    AND op.ship_date>='2013-03-01'
    AND op.ship_date<='2013-04_01'
ORDER BY op.ship_date DESC
于 2013-04-04T12:25:41.830 に答える