2016-09-25 83 views
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秒完成。

如何優化此查詢以節省時間?

+3

您需要使用MySQL的EXPLAIN謂詞來運行查詢分析器並告訴您查詢將執行什麼操作。它將取決於你在模式中定義了哪些索引。 http://dev.mysql.com/doc/refman/5.7/en/using-explain.html –

回答

1

問題是數據庫模式中的索引配置錯誤。 剛剛在ads.recycle_bin,ads.hidden和ads.subcat_id中添加了索引

謝謝!