我有一个计数计算查询,我在我的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;
如果您有这么多记录,您担心将它们全部归还,那么请分批进行,但我只是不明白为什么您真的想要为每个客户运行此查询. 总结
以上是内存溢出为你收集整理的Rails MySQL查询时间混乱全部内容,希望文章能够帮你解决Rails MySQL查询时间混乱所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)