Mysql 通过引起慢速查询优化SQL查询顺序
我发现,当我在下表中输入order by时,它会导致查询从0秒变为3.5秒(这只有100000行),有人能帮我优化吗Mysql 通过引起慢速查询优化SQL查询顺序,mysql,Mysql,我发现,当我在下表中输入order by时,它会导致查询从0秒变为3.5秒(这只有100000行),有人能帮我优化吗 SELECT offer_id, offer_cheapest_rental, offer_cashback, offer_offertext, network_name, merchant_image, network_image, tariff_contr
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
你应该试试这个
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
我通过使用
FORCE INDEX(deal\u 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
你有关于deal_受欢迎程度的指数吗?如果没有,这就是你的答案,尽管你肯定会认为排序查询比未排序查询需要更长的时间。使用InnoDB的索引。是的,deal\u popularity有一个索引…我使用InnoDB