mysql - 为什么 Rails "touch"我所有的数据库条目?

标签 mysql ruby-on-rails-3.2

我正在本地计算机上使用 MySQL 数据库开发 Ruby on Rails 3.2 应用程序。为了测试我添加的一些新功能,我编写了一个脚本来手动将大约 50,000 条记录插入数据库中。到目前为止,一切顺利。

添加这些记录后,我第一次使用新功能加载页面时,Rails 加载了每条记录,然后更新它们,一次一个。这是我的日志文件的样子:

  (0.1ms)  BEGIN
 MyModel Exists (0.3ms)  SELECT 1 AS one FROM `my_models` WHERE `my_models`.`part_id` = BINARY 1 LIMIT 1
 SQL (0.1ms)  INSERT INTO `my_models` (`answer_date_time`, `call_date_time`, `caller_id`, `part_id`, `created_at`, `destination`, `employee_id`, `end_date_time`, `length`, `part_user_id`, `price`, `routing`, `source`, `updated_at`) VALUES (NULL, '2013-06-28 13:12:06', NULL, 1, '2013-06-28 13:12:06', NULL, 4, NULL, NULL, NULL, NULL, 'incoming', NULL, '2013-06-28 13:12:06')
  (52.6ms)  COMMIT
  (0.0ms)  BEGIN
 MyModel Exists (0.2ms)  SELECT 1 AS one FROM `my_models` WHERE `my_models`.`part_id` = BINARY 2 LIMIT 1
 SQL (0.2ms)  INSERT INTO `my_models` (`answer_date_time`, `call_date_time`, `caller_id`, `part_id`, `created_at`, `destination`, `employee_id`, `end_date_time`, `length`, `part_user_id`, `price`, `routing`, `source`, `updated_at`) VALUES (NULL, '2013-06-28 13:12:06', NULL, 2, '2013-06-28 13:12:06', NULL, 4, NULL, NULL, NULL, NULL, 'outgoing', NULL, '2013-06-28 13:12:06')
  (70.4ms)  COMMIT

等等,对于所有 50,000 多条记录。花了几分钟才完成。不过,此后我多次尝试重新加载有问题的页面,并且 Rails 没有再次运行这些查询。

这看起来正常吗? Rails 是否偶尔需要像这样“戳”一次新记录,然后不管它们?或者我的代码似乎有问题会导致性能问题?

如果存在潜在问题,不幸的是,我是 Rails 新手,并且我从其他人那里继承了这个应用程序。我应该从哪里开始寻找会产生这种行为的代码?

编辑:这是 MyModel 的定义。我不知道这个评论是什么意思...

class MyModel < ActiveRecord::Base
  # This model is :dependent => :delete elsewhere
  #  so no :dependents or before/after destroy callbacks without changing other models!
  belongs_to :employee

  validates_uniqueness_of :parts_id

  # snip - class and instance methods
end

编辑2:这就是我将记录添加到数据库的方式:

我编写了一个 Ruby 脚本 create_db_data.rb ,如下所示(这不是实际的脚本,因为我不想共享我公司的代码,但它的工作方式几乎完全相同):

n = 0
for employee in Employee
  for i in (1..10000)
    obj = MyModel.new
    obj.employee_id = employee.id
    obj.answer_date_time = Date.today - i

    n += 1
    obj.part_id = n

    obj.save
  end
end

然后我从命令行执行了rails r create_db_data.rb。此后,下次我尝试从我的开发计算机上的应用程序加载页面时,Rails 执行了我所询问的奇怪查询 - Rails 没有执行任何操作因为我添加了记录,但是之后

最佳答案

我最好的猜测是它与 MyModel 定义中的 validates_uniqueness_of parts_id 行有关。根据the documentation for the function ,

When [a] record is created [using validates_uniqueness_of parts_id], a check is performed to make sure that no record exists in the database with the given value for the specified attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.

我的猜测是,由于我使用 rails r 添加记录并且没有正确启动服务器,Rails 仍然希望在我第一次实际在 Web 应用程序中使用记录时执行检查。

关于mysql - 为什么 Rails "touch"我所有的数据库条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371715/

相关文章:

php - 如何从今天、上周或上个月插入的 MySQL 数据库中选择所有 Eloquent 模型?

mysql - 为本地环境设置中央MySQL服务器

ruby-on-rails - Rails 3.2 和 Sass 不呈现 Application.css

ruby-on-rails - Ruby on Rails - f.label 上的 Completed 500 内部错误

ruby - 如何使用有效? rails 3中数组的方法

Mysql COUNT 和 IF 用法

java - 出于想法,MySQLNonTransientConnectionException : Could not create connection to database server

internet-explorer-8 - Rails、Foundation 4、Respond.js 在 IE8 中无法正常工作

ruby-on-rails - 生成重置密码 token 不保存在模型上

PHP:在将 SQL 处理到 Mysql 之前检查 SQL 的有效性