【问题标题】:mysql query taking too long to executemysql查询执行时间过长
【发布时间】:2012-03-11 23:34:30
【问题描述】:

我有一个查询执行时间过长(4 秒),即使我查询的所有字段都已编入索引。以下是查询和解释结果。任何想法是什么问题? (执行查询时,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 |                                              |
+----+-------------+--------------+--------+-------------------------+---------+---------+--------------------------+------+----------------------------------------------+

【问题讨论】:

  • JOIN 做了什么样的连接?
  • 请发布 SHOW CREATE TABLE 结果的 TEXT 输出;您似乎缺少结果表中名为 set 的列的索引。

标签: mysql query-optimization


【解决方案1】:
|  1 | SIMPLE      | result       | ALL    | resultset               | NULL    | NULL    | NULL                     | 5322 | Using where; Using join buffer               |

看起来它没有在最大的表上使用索引。我猜不出这个查询应该做什么,但看起来你在result.set 上有一个索引,所以我会尝试在result.did 上添加一个索引,看看是否有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多