MySQL:优化300多秒的查询?

时间:2016-09-25 03:10:58

标签: mysql

我有一个classiffieds网站,本月我们有很多新访客。

该网站简单地爆炸并开始提供“无法与DB"连接”。我使用codeigniter,这是跟踪mysql Logs:

# 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秒完成。

如何优化此查询以缩短时间?

1 个答案:

答案 0 :(得分:1)

问题是数据库架构中的索引配置错误。 只需在ads.recycle_bin,ads.hidden和ads.subcat_id

中添加索引即可

谢谢!