简体   繁体   English

Rails控制台vs MySQL控制台-计算巨大表中的行

[英]Rails console vs MySQL console - Counting rows in a huge table

I'd like to show a graph on how many records a table has, for the last 30 days. 我想显示一张图表,显示过去30天中一个表有多少条记录。 I'm going to do a rake task that I will run each day from a cron job. 我将执行一项耙任务,该任务每天将由cron作业执行。

I just made a test, and I found a weird "issue". 我刚刚进行了测试,发现了一个奇怪的“问题”。 I ran this code in the Rails console: 我在Rails控制台中运行了以下代码:

SeenEpisode.count
#=> (57135.2ms)  SELECT COUNT(*) FROM `seen_episodes`

I then tested this in the MySQL console: 然后,我在MySQL控制台中对此进行了测试:

use my_database;
SELECT COUNT(*) FROM seen_episodes;
1 row in set (3.94 sec)

This table has 21 million records. 该表有2100万条记录。

I ran both tests on my production server, to get real performance numbers. 我在生产服务器上运行了这两个测试,以获得真实的性能数字。

Other tables with around 500k records, take 80-300ms in the Rails console. 其他具有约500k记录的表在Rails控制台中花费80-300ms。

Why does counting (using the same query) the 21m big table in Rails take so much longer than in the MySQL console? 为什么在Rails中计数(使用相同的查询)21m大表比在MySQL控制台中花费这么长的时间?

我猜您是在INNODB上,其中不缓存count(*)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM