Rails MySQL查询时间混乱

时间:2010-01-29 11:16:59

标签: mysql ruby-on-rails database innodb

我有一个计数计算查询,我在我的Rails应用程序中运行了数千次,一次为数据库中的每个客户运行。

当我在MySQL客户端中运行查询并禁用查询缓存时,查询的持续时间不超过1毫秒。

然而,当我从启用了查询输出的Rails控制台运行我的任务时,我注意到在前几次查询很快之后,其余查询的时间突然从不到1毫秒突然增加到大约180毫秒

我已经减少了innodb_buffer_pool_size,以便查看行为的变化,但没有发现任何事情。

以下是控制台的输出:

  EmailCampaignReport::Open Columns (143.2ms)   SHOW FIELDS FROM `email_campaign_report_opens`
  SQL (0.3ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332330) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333333) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332661) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332326) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332665) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 336027) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333001) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 331983) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332668) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332316) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332325) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 331995) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 334007) 
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333326) 
  SQL (0.1ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332998) 
  SQL (183.9ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 334673) 
  SQL (183.7ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 336751) 
  SQL (183.6ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333334) 
  SQL (186.3ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332663) 
  SQL (183.7ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332328) 
  SQL (186.3ms)   SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332659) 

该表中的customer_id列有一个索引。

有没有人对为什么会这样做有任何建议?

由于

4 个答案:

答案 0 :(得分:4)

为什么不运行一个查询?

SELECT customer_id, count(*) AS count_all FROM `email_campaign_report_opens` GROUP BY customer_id;

如果您有这么多记录,您担心将它们全部归还,那么请分批进行,但我只是不明白为什么您真的想要为每个客户运行此查询。

答案 1 :(得分:0)

这种情况是否也会发生在您的rails应用中,或者只是在控制台中运行时才会发生这种情况?另外,您使用的是Aptana等客户端,还是在shell中运行它?

答案 2 :(得分:0)

这是什么版本的Rails? 根据您的版本和您的Ruby / Rails代码,您可能会在不使用它的情况下缓存大量数据,并且在获取新数据之前必须进行垃圾收集,这可以解释延迟。 请注意,这是猜测。

答案 3 :(得分:0)

向关联添加counter cache是不是有意义(阅读:向email_campaign_report_opens_count模型添加Customer)?当然,您必须在迁移期间初始化计数器,但它应该非常快,您甚至不需要在遍历customers表时触摸关联的表。