简体   繁体   English

MySQL查询极慢

[英]MySQL Query extremely slow

The following query takes too much time (0.8169sec at the moment) to show 20 entries and i can't find the reason why... 以下查询花费太多时间(此刻为0.8169sec)来显示20个条目,但我找不到原因...

SELECT
        `Order`.ID,
        Order_Type.Description as OrderTypeDescription,
        `Order`.OrderType as OrderType,
        DATE_FORMAT(`Order`.InsertDate, '%d.%m.%Y') as OrderInsertDate,
        round(sum(Item_Price.Amount),2) as OrderAmount,
        c1.ID as buyerCustomerId,
        c2.ID as sellerCustomerId,
        c1.CompanyName as BuyerCompany,
        c2.CompanyName as SellerCompany,
        c1.ID as BuyerCustomer,
        c2.ID as SellerCustomer
    FROM `Order`
        INNER JOIN Order_Type ON Order_Type.ID=`Order`.OrderType
        INNER JOIN Customer as c1 ON c1.ID=`Order`.BuyerCustomer
        INNER JOIN Customer as c2 ON c2.ID=`Order`.SellerCustomer
        INNER JOIN Item ON Item.`Order`=`Order`.ID
        INNER JOIN Item_Price ON Item_Price.Item=Item.ID
    GROUP BY `Order`.ID,OrderType,OrderTypeDescription,buyerCustomerId,sellerCustomerId,BuyerCustomer,SellerCustomer
    ORDER BY `Order`.ID DESC
    LIMIT 20

EXPLAIN shows the following output: http://pastebin.com/5f9QYizq EXPLAIN显示以下输出: http : //pastebin.com/5f9QYizq

I am not really good in optimizing queries, but i think the reason for the bad performance could be the join (and the sum) on the item and the item_price table, because there are aa lot of rows in both tables (item: 16974, item_price: 23981) as each item has one or more item_prices which sum up to order amount. 我并不是非常擅长优化查询,但是我认为性能不佳的原因可能是项目和item_price表上的联接(和总和),因为两个表中都有很多行(项目:16974, item_price:23981),因为每个项目都有一个或多个item_prices,它们总计为订单金额。

Any ideas how to make this query faster? 任何想法如何使此查询更快?

You might try using correlated subqueries instead of group by : 您可以尝试使用相关子查询,而不要使用group by

SELECT o.ID, ot.Description as OrderTypeDescription, ot.OrderType as OrderType,
       DATE_FORMAT(o.InsertDate, '%d.%m.%Y') as OrderInsertDate,
       (SELECT round(sum(ip.Amount), 2)
        FROM Item i INNER JOIN 
             Item_Price ip
             ON ip.Item = i.ID
        WHERE i.`Order` = o.ID
       ) as OrderAmount,
       c1.ID as buyerCustomerId,
       c2.ID as sellerCustomerId,
       c1.CompanyName as BuyerCompany,
       c2.CompanyName as SellerCompany,
       c1.ID as BuyerCustomer,
       c2.ID as SellerCustomer
FROM `Order` o INNER JOIN
      Order_Type ot
      ON ot.ID = o.OrderType INNER JOIN
      Customer c1
      ON c1.ID = o.BuyerCustomer INNER JOIN
      Customer c2
      ON c2.ID = `Order`.SellerCustomer
ORDER BY o.ID DESC
LIMIT 20;

This should save on the GROUP BY overhead. 这样可以节省GROUP BY开销。

You could even move the LIMIT to a subquery, assuming that the joins are not filtering any rows: 您甚至可以将LIMIT移至子查询,假设联接未过滤任何行:

SELECT o.ID, ot.Description as OrderTypeDescription, ot.OrderType as OrderType,
       DATE_FORMAT(o.InsertDate, '%d.%m.%Y') as OrderInsertDate,
       (SELECT round(sum(ip.Amount), 2)
        FROM Item i INNER JOIN 
             Item_Price ip
             ON ip.Item = i.ID
        WHERE i.`Order` = o.ID
       ) as OrderAmount,
       c1.ID as buyerCustomerId,
       c2.ID as sellerCustomerId,
       c1.CompanyName as BuyerCompany,
       c2.CompanyName as SellerCompany,
       c1.ID as BuyerCustomer,
       c2.ID as SellerCustomer
FROM (SELECT o.*
      FROM `Order` o 
      ORDER BY o.id DESC
     ) o INNER JOIN
     Order_Type ot
     ON ot.ID = o.OrderType INNER JOIN
     Customer c1
     ON c1.ID = o.BuyerCustomer INNER JOIN
     Customer c2
     ON c2.ID = `Order`.SellerCustomer
ORDER BY o.ID DESC
LIMIT 20;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM