简体   繁体   中英

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...

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

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.

Any ideas how to make this query faster?

You might try using correlated subqueries instead of 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.

You could even move the LIMIT to a subquery, assuming that the joins are not filtering any rows:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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