嗨这个查询需要100毫秒,我觉得它太多了?
SELECT
COUNT(
s0_.id
) AS sclr0,
s0_.id AS id1,
s0_.status AS status2,
...
s0_.public AS public22,
t1_.username AS username23,
t1_.username_canonical AS username_canonical24,
...
t1_.old_new AS old_new57,
t1_.image_name AS image_name58,
s0_.user_id AS user_id59
FROM
statuses s0_
INNER JOIN
users t1_ ON s0_.user_id = t1_.id
WHERE
s0_.time >= ? AND s0_.suggested_status = 1
GROUP BY
t1_.id
ORDER BY
sclr0 DESC
LIMIT
10
Parameters: [1376784000]
[Display runnable query]
Time: 108.53 ms [ - Explain query ]
并解释:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1_ index PRIMARY PRIMARY 4 12132 Using temporary; Using filesort
1 SIMPLE s0_ ref IDX_4BF01E11A76ED395 IDX_4BF01E11A76ED395 4 db.t1_.id 3 Using where
我做错了吗?
查询点是选择已发布标记为“已建议”状态的所有用户,并按上个月每个用户的建议状态计数对用户进行排序。
答案 0 :(得分:2)
尝试将索引添加到表s0_.suggested_status和s0_.time。
ALTER TABLE `statuses` ADD INDEX `suggested_status` (`suggested_status`);
ALTER TABLE `statuses` ADD INDEX `time` (`time`);
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
How do I add indices to MySQL tables?
您可以将索引添加到学说注释(或yaml / xml): http://docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-index