优化SQL查询 - 按缓慢排序

时间:2014-04-25 16:06:05

标签: mysql

我发现当我在下表中下订单时导致查询从0秒到3.5秒(这只有100,000行)有人可以帮我优化吗?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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