Rails MySQL查询时间混乱

前端之家收集整理的这篇文章主要介绍了Rails MySQL查询时间混乱前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个计数计算查询,我在我的Rails应用程序中运行了数千次,对于db中的每个客户.

当我在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列有一个索引.

有没有人有任何关于为什么会发生这种情况的建议?

谢谢

最佳答案
为什么不运行一个查询

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

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

原文链接:https://www.f2er.com/mysql/432928.html

猜你在找的MySQL相关文章