2014-02-06 2 views
0

레일 사용. 이와삭제를 위해 다른 테이블에서 2 개의 큰 ID 집합을 비교합니다.

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

문제가 database_a.shopsdatabase_b.properties 각각 두 번째 쿼리에서 다음 오류, 비교 결과에 50 만 기록을 가지고있다 : database_b.properties에서 찾을 수 없습니다 어디 database_a.shops를 제거하려면 다음 라인을 사용하려고

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을 사용해 보았지만 여전히 작동하지 않으며 같은 오류가 발생합니다. 나는 세트가 너무 커서 비교할 수 없다고 생각한다. 이 문제를 어떻게 해결할 수 있습니까?

레일스에서 ​​destroy_all을 사용하여 연관을 파기하기 때문에 원시 SQL을 사용할 수 없습니다.

+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 
관련 문제