0
我有一个classiffieds网站,本月我们有很多新访客。MySQL:优化300多秒查询?
该网站只是爆炸,并开始提供“无法连接数据库”。我使用笨,这是跟踪未mysql的日志:
# Time: 160924 22:50:20
# Thread_id: 1914 Schema: erich33_asd QC_hit: No
# Query_time: 378.515875 Lock_time: 375.061742 Rows_sent: 75 Rows_examined: 152644
SET timestamp=1474771820;
SELECT `category`.`id` as `category_id`, `category`.`subcat` as `category_name`, `category`.`desc` as `category_desc`, `category`.`name` as `category_pretty_name`, `ads`.`id`, `ads`.`header`, `ads`.`price`, SUBSTR(ads.body, 1, 160) as body, `ads`.`sellfast`, `ads`.`foto1`, `ads`.`foto2`, `ads`.`foto3`, `ads`.`foto4`, `ads`.`foto5`, `ads`.`updated`, `ads`.`created`, `ads`.`hits`, `promo`.`promotype` as `promo_type`, `provinces`.`prov_name`, `supercategory`.`name` as `supercategory_name`, `supercategory`.`id` as `supercategory_id`
FROM `ads`
JOIN `category` ON `category`.`id` = `ads`.`subcat_id`
JOIN `supercategory` ON `supercategory`.`id` = `category`.`cat`
JOIN `provinces` ON `provinces`.`id` = `ads`.`province_id`
LEFT OUTER JOIN `promo` ON `promo`.`ads_id` = `ads`.`id`
WHERE `ads`.`subcat_id` = '22'
AND `recycle_bin` != 1
AND `hidden` =0
ORDER BY `promo_type` DESC, `updated` DESC
LIMIT 833, 75;
正如你可以看到它了378.515875秒完成。
如何优化此查询以节省时间?
您需要使用MySQL的EXPLAIN谓词来运行查询分析器并告诉您查询将执行什么操作。它将取决于你在模式中定义了哪些索引。 http://dev.mysql.com/doc/refman/5.7/en/using-explain.html –