2014-02-06 29 views
0

使用Rails。我嘗試使用下面的行刪除database_a.shops其中database_b.properties未發現:比較來自不同表格的兩大組ID以進行刪除

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

問題與此是database_a.shopsdatabase_b.properties都有超過50萬的記錄進行比較,導致在第二次查詢以下錯誤:

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來銷燬關聯。

+0

您是否嘗試過沒有數組?像這樣:'Shop.where(「property_id NOT IN(SELECT id FROM properties」)。destroy_all' – MurifoX

+0

@MurifoX'properties'在另一個數據庫中 – Victor

+0

哦,這是一個問題! – MurifoX

回答

0

如果這是一個時間的操作,也許你可以接受一些性能下降,並做一些事情,如:

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

如果它不是一次性的操作,我建議你重新設計來避免這個問題。您可以將數據完整性添加到Property類:

has_many shops, :dependent => :destroy 
相關問題