2014-04-25 61 views
0

我發現,當我把下面的表的訂單,它導致查詢從0秒到3.5秒(這隻有100,000行)任何人都可以幫助我優化這個?優化SQL查詢 - 訂單造成緩慢

SELECT  offer_id, 
     offer_cheapest_rental, 
     offer_cashback, 
     offer_offertext, 
     network_name, 
     merchant_image, 
     network_image, 
     tariff_contractlength, 
     offer_link, 
     tariff_freemins, 
     tariff_freetxts, 
     tariff_dataallowance, 
     offer_phonecost, 
     offer_offerental, 
     offer_monthlycost, 
     tariff_rental, 
     tariff_nicename, 
     tariff_name, 
     model_imagelarge, 
     model_popularity, 
     model_make, 
     model_name, 
     model_colour, 
     offer_freegift, 
     offer_clearance, 
     model_basename, 
     p.model_id as model_id, 
     type 
     FROM deals_temp d 
     INNER JOIN phones p ON d.model_id = p.model_id 
     INNER JOIN tariffs t ON d.tariff_id = t.tariff_id 
     INNER JOIN networks n ON d.network_id = n.network_id 
     INNER JOIN free_gift fg ON d.freegift_id = fg.freegift_id 
     INNER JOIN merchants m ON d.merchant_id = m.merchant_id 
     INNER JOIN type ty ON ty.type_id = d.type_id 
      ORDER BY deal_popularity DESC LIMIT 0,10 
+1

你對deal_popularity的指數?如果不是,那就是你的答案,儘管你當然應該期望排序後的查詢花費的時間比未排序的要長。 –

+0

http://www.sitepoint.com/using-explain-to-write-better-mysql-queries/ –

+0

在InnoDB中使用索引。 –

回答

0

你應該試試這個

SELECT * FROM (
    SELECT  offer_id, 
    offer_cheapest_rental, 
    offer_cashback, 
    offer_offertext, 
    network_name, 
    merchant_image, 
    network_image, 
    tariff_contractlength, 
    offer_link, 
    tariff_freemins, 
    tariff_freetxts, 
    tariff_dataallowance, 
    offer_phonecost, 
    offer_offerental, 
    offer_monthlycost, 
    tariff_rental, 
    tariff_nicename, 
    tariff_name, 
    model_imagelarge, 
    model_popularity, 
    model_make, 
    model_name, 
    model_colour, 
    offer_freegift, 
    offer_clearance, 
    model_basename, 
    p.model_id as model_id, 
    type 
    FROM deals_temp d 
    INNER JOIN phones p ON d.model_id = p.model_id 
    INNER JOIN tariffs t ON d.tariff_id = t.tariff_id 
    INNER JOIN networks n ON d.network_id = n.network_id 
    INNER JOIN free_gift fg ON d.freegift_id = fg.freegift_id 
    INNER JOIN merchants m ON d.merchant_id = m.merchant_id 
    INNER JOIN type ty ON ty.type_id = d.type_id 
) a 
ORDER BY deal_popularity DESC LIMIT 0,10 
0

我解決它自己使用FORCE INDEX (deal_popularity)和它的運行現在快很多:

SELECT offer_id, 
     offer_cheapest_rental, 
     offer_cashback, 
     offer_offertext, 
     network_name, 
     merchant_image, 
     network_image, 
     tariff_contractlength, 
     offer_link, 
     tariff_freemins, 
     tariff_freetxts, 
     tariff_dataallowance, 
     offer_phonecost, 
     offer_offerental, 
     offer_monthlycost, 
     tariff_rental, 
     tariff_nicename, 
     tariff_name, 
     model_imagelarge, 
     model_popularity, 
     model_make, 
     model_name, 
     model_colour, 
     offer_freegift, 
     offer_clearance, 
     model_basename, 
     p.model_id as model_id, 
     type 
     FROM deals_temp d FORCE INDEX (deal_popularity) 
     INNER JOIN phones p ON d.model_id = p.model_id 
     INNER JOIN tariffs t ON d.tariff_id = t.tariff_id 
     INNER JOIN networks n ON d.network_id = n.network_id 
     INNER JOIN free_gift fg ON d.freegift_id = fg.freegift_id 
     INNER JOIN merchants m ON d.merchant_id = m.merchant_id 
     INNER JOIN type ty ON ty.type_id = d.type_id