【问题标题】:symfony2 doctrine query optimalizationsymfony2 学说查询优化
【发布时间】:2013-09-17 20:54:58
【问题描述】:

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

我做错了吗?

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

【问题讨论】:

  • 您可能会考虑创建一个命令来缓存此查询的结果并使用 cronjob 定期执行该命令。然后访问应用程序中的缓存值。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

尝试为表 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2014-12-20
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多