0

嗨,这个查询需要 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

难道我做错了什么?

查询点是选择所有发布状态标记为“建议”的用户,并按每个用户上个月的建议状态计数对用户进行排序。

4

1 回答 1

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

如何向 MySQL 表添加索引?

您可以将索引添加到教义注释(或 yaml/xml): http ://docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-index

于 2013-09-17T21:22:40.803 回答