比较来自不同表的2组大型ID以进行删除

时间:2014-02-06 17:45:02

标签: ruby-on-rails

使用Rails。我尝试使用以下行删除database_a.shops中未找到的database_b.properties

properties = Property.pluck(:id)
Shop.where("property_id NOT IN (?)", properties).destroy_all

问题是database_a.shopsdatabase_b.properties每个都有超过500,000条记录需要比较,导致第二次查询出现以下错误:

from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:215:in `exec_query'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `select'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `block in find_by_sql'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/explain.rb:41:in `logging_query_plan'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:37:in `find_by_sql'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:171:in `exec_queries'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:160:in `block in to_a'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/explain.rb:41:in `logging_query_plan'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:159:in `to_a'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:498:in `inspect'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/abc/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

我尝试使用find_each,但它仍然不起作用并给出了同样的错误。我猜这些集合太大而无法比较。我该如何解决这个问题?

请注意,我不能使用原始SQL,因为我想在Rails中使用destroy_all来销毁关联。

1 个答案:

答案 0 :(得分:0)

如果这是一次性操作,也许您可​​以接受一些缓慢的性能并执行以下操作:

Shop.all.each do |shop|
    shop.destroy unless Property.find(shop.property_id)
end

如果不是一次性操作,我建议你重新设计以避免这个问题。您可以向Property类添加数据完整性:

has_many shops, :dependent => :destroy