0
使用Rails。我嘗試使用下面的行刪除database_a.shops
其中database_b.properties
未發現:比較來自不同表格的兩大組ID以進行刪除
properties = Property.pluck(:id)
Shop.where("property_id NOT IN (?)", properties).destroy_all
問題與此是database_a.shops
和database_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
來銷燬關聯。
您是否嘗試過沒有數組?像這樣:'Shop.where(「property_id NOT IN(SELECT id FROM properties」)。destroy_all' – MurifoX
@MurifoX'properties'在另一個數據庫中 – Victor
哦,這是一個問題! – MurifoX