简体   繁体   English

mysql查询执行时间太长

[英]mysql query taking too long to execute

I have a query that is taking way too long to execute (4 seconds) even though all the fields i am querying against are indexed. 我有一个执行时间太长的查询(4秒),即使我查询的所有字段都已编入索引。 Below are the query and the explain results. 以下是查询和解释结果。 Any ideas what the problem is? 有什么想法是什么问题? (mysql CPU usage shoots up to 100% when executing the query (执行查询时,MySQL CPU使用率高达100%

EXPLAIN SELECT count(hd.did) as NumPo, `hd`.`sid`, `src`.`Name`
FROM (`hd`)
JOIN `result` ON `result`.`did` = `hd`.`did`
JOIN `sf` ON `sf`.`fid` = `hd`.`fid`
JOIN `src` ON `src`.`sid` = `hd`.`sid`
WHERE `sf`.`tid` =  2
AND `result`.`set` =  'xxxxxxx'
GROUP BY `hd`.`sid`
ORDER BY `NumPo` DESC
LIMIT 10;

+----+-------------+--------------+--------+-------------------------+---------+---------+--------------------------+------+----------------------------------------------+
| id | select_type | table        | type   | possible_keys           | key     | key_len | ref                      | rows | Extra                                        |
+----+-------------+--------------+--------+-------------------------+---------+---------+--------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | sf           | ref    | PRIMARY,type            | type    | 2       | const                    |    4 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | hd           | ref    | PRIMARY,sid,fid         | FeedID  | 4       | f2.sf.fid                |    3 |                                              |
|  1 | SIMPLE      | result       | ALL    | resultset               | NULL    | NULL    | NULL                     | 5322 | Using where; Using join buffer               |
|  1 | SIMPLE      | src          | eq_ref | PRIMARY                 | PRIMARY | 4       | f2.hd.sid                |    1 |                                              |
+----+-------------+--------------+--------+-------------------------+---------+---------+--------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | result       | ALL    | resultset               | NULL    | NULL    | NULL                     | 5322 | Using where; Using join buffer               |

It looks like it's not using an index on the biggest table. 看起来它没有在最大的表上使用索引。 I'm having trouble guessing what this query is supposed to do, but it looks like you have an index on result.set , so I'd try adding one to result.did and see if it helps. 我在猜测此查询应该执行的操作时遇到了麻烦,但是看起来您在result.set上有一个索引,因此我尝试在result.did添加一个result.did ,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM