Querrel을 사용하면 마치 하나의 데이터베이스를 쿼리하는 것처럼 ActiveRecord를 사용하여 여러 데이터베이스를 병렬(스레드)로 쉽게 쿼리할 수 있습니다.
애플리케이션의 Gemfile에 다음 줄을 추가하세요.
gem 'querrel'
그런 다음 다음을 실행합니다.
$ bundle
또는 다음과 같이 직접 설치하십시오.
$ gem install querrel
최상위 네임스페이스를 통해 직접 또는 인스턴스를 생성하여 Querrel과 함께 사용할 수 있습니다.
all_brands = Querrel . query ( Brand . all , on : [ 'db1' , 'db2' , 'db3' ] )
# is the same as
all_brands = Querrel . new ( [ 'db1' , 'db2' , 'db3' ] ) . query ( Brand . all )
위 중 하나는 주어진 모든 데이터베이스의 모든 브랜드 개체 배열을 제공합니다. 레코드는 읽기 전용으로 표시됩니다.
query
ActiveRecord::Relation
에 전달된 블록을 생성하므로 결과가 병합되기 전에 결과에 대해 추가 작업을 수행할 수 있습니다. 예를 들어 pluck을 사용할 수 있습니다.
all_brand_names = q . query ( Brand . all ) do | s |
s . pluck ( :name )
end
미리 정의된 범위를 실행하고 병합하는 대신 지정된 데이터베이스 연결로 원하는 모든 작업을 수행할 수 있는 블록을 사용하는 run
메서드도 있습니다. 예를 들면 다음과 같습니다.
require 'thread'
all_brands = [ ]
b_s = Mutex . new
all_products = [ ]
p_s = Mutex . new
Querrel . run ( on : dbs ) do | db |
b_s . synchronize { all_brands += Brand . all . to_a }
p_s . synchronize { all_products += Product . all . to_a }
end
Querrel에 사용할 데이터베이스를 지시할 수 있는 세 가지 방법이 있습니다.
환경 배열을 전달합니다(예: Querrel.new([:customer1, :customer2])
데이터베이스 이름 배열을 전달합니다(예: Querrel.new(['dbs/customer1.sqlite3', 'dbs/customer2.sqlite3'], db_names: true)
명명된 연결 구성의 해시를 전달합니다. 예:
Querrel . new ( {
one : {
adapter : "sqlite3" ,
database : "test/dbs/test_db_1.sqlite3"
} ,
two : {
adapter : "sqlite3" ,
database : "test/dbs/test_db_2.sqlite3"
}
} )
기본적으로 Querrel은 최대 20개의 스레드를 사용하지만 :threads
옵션을 사용하여 이를 조정할 수 있습니다:
q = Querrel . new ( dbs , threads : 50 )
git checkout -b my-new-feature
).git commit -am 'Add some feature'
)git push origin my-new-feature
)