인덱스로 쿼리 최적화
이 페이지의 내용
개요
이 가이드 에서는 Mongoid에서 인덱스를 사용하는 방법을 학습 수 있습니다. 인덱스는 MongoDB 스캔해야 하는 문서 수를 제한하여 쿼리 효율성 향상시킬 수 있습니다. 애플리케이션 특정 필드에 대한 쿼리를 반복적으로 실행 경우 해당 필드에 인덱스 생성하여 쿼리 성능을 개선할 수 있습니다.
이 가이드 의 다음 섹션에서는 Mongoid를 사용하여 다양한 유형의 인덱스를 선언하고 생성하는 방법을 설명합니다. 이 예제에서는 sample_restaurants
데이터베이스 의 restaurants
컬렉션 에 매핑되는 Restaurant
모델을 사용합니다. Mongoid를 사용하여 이 데이터베이스 및 컬렉션 에 연결하는 방법을 학습 빠른 시작 - Ruby on Rails 또는 빠른 시작 - Sinatra 가이드를 참조하세요.
인덱스 선언 및 생성
Mongoid를 사용하는 경우 index
매크로를 사용하여 인덱스 선언한 다음 create_indexes
명령을 사용하여 인덱스를 생성할 수 있습니다.
다음 코드 예시 Restaurant
클래스의 cuisine
필드 에 이름이 cuisine_index
인 오름차순 인덱스 선언하고 생성하는 방법을 보여 줍니다.
class Restaurant include Mongoid::Document field :name, type: String field :cuisine, type: String field :borough, type: String index({ cuisine: 1}, { name: "cuisine_index", unique: false }) end Restaurant.create_indexes
index
매크로는 만들려는 인덱스 정의하고 create_indexes
명령은 restaurants
컬렉션 에 인덱스를 만듭니다.
인덱스 정의할 때 첫 번째 해시 객체 에는 인덱스 하려는 필드 와 그 방향이 포함됩니다. 1
은 오름차순 인덱스 나타내고 -1
은 내림차순 인덱스 나타냅니다. 두 번째 해시 객체 인덱스 옵션이 포함되어 있습니다. 인덱스 옵션에 대해 자세히 학습 API 설명서 섹션을 참조하세요.
별칭 및 인덱스 선언
인덱스 정의에 별칭이 지정된 필드 이름을 사용할 수 있습니다. 예시 들어, 다음 코드는 borough
필드 의 별칭인 b
필드 에 인덱스 생성합니다.
class Restaurant include Mongoid::Document field :borough, as: :b index({ b: 1}, { name: "borough_index" }) end
내장된 문서 필드에 인덱스 만들기
내장된 문서 필드에 인덱스 정의할 수 있습니다. 다음 코드 예시 Restaurant
모델의 address
필드 내에 포함된 street
필드 에 오름차순 인덱스 선언하는 방법을 보여 줍니다.
class Address include Mongoid::Document field :street, type: String end class Restaurant include Mongoid::Document embeds_many :addresses index({"addresses.street": 1}) end
복합 색인 만들기
여러 필드에 복합 인덱스 정의할 수 있습니다. 다음 코드 예시 borough
필드 에서는 오름차순, name
필드 에서는 내림차순인 복합 인덱스 선언하는 방법을 보여 줍니다.
class Restaurant include Mongoid::Document field :name, type: String field :borough, type: String index({borough: 1, name: -1}, { name: "compound_index"}) end
지리 공간적 인덱스 만들기
GeoJSON 객체 또는 좌표 쌍이 포함된 필드에 2dsphere 인덱스 정의할 수 있습니다. 다음 예시 GeoJSON 객체가 포함된 필드 에 2dsphere 인덱스 정의합니다.
class Restaurant include Mongoid::Document field :location, type: Array index({location: "2dsphere"}, { name: "location_index"}) end
2dsphere 인덱스에 대한 자세한 내용은 MongoDB Server 매뉴얼의 2dsphere 가이드 참조하세요.
GeoJSON 유형에 대한 자세한 내용은 MongoDB Server 매뉴얼의 GeoJSON 객체 가이드 참조하세요.
희소 인덱스 만들기
모든 문서에 존재하지 않는 필드에 희소 인덱스 정의할 수 있습니다. 다음 코드 예시 borough
필드 에 희소 인덱스 정의합니다.
class Restaurant include Mongoid::Document field :name, type: String field :cuisine, type: String field :borough, type: String index({ borough: 1}, { sparse: true }) end
희소 인덱스에 대한 자세한 내용은 MongoDB Server 매뉴얼의 희소 인덱스 가이드 참조하세요.
여러 인덱스 생성
모델 내에서 여러 인덱스를 정의하고 단일 create_indexes
호출을 사용하여 인덱스를 생성할 수 있습니다. 다음 예시 동시에 여러 인덱스를 생성하는 방법을 보여줍니다.
class Restaurant include Mongoid::Document field :name, type: String field :cuisine, type: String field :borough, type: String index({ name: 1}) index({ cuisine: -1}) end Restaurant.create_indexes
드롭 인덱스
컬렉션 의 모든 인덱스를 삭제할 수 있습니다. 다음 예시 Restaurant
모델의 모든 인덱스를 삭제합니다.
Restaurant.remove_indexes
참고
기본 인덱스
MongoDB 컬렉션 생성 중에 _id
필드 에 기본값 인덱스 생성합니다. 이 인덱스 클라이언트가 _id
필드 에 동일한 값을 가진 두 개의 문서를 삽입하는 것을 방지합니다. 이 인덱스 삭제할 수 없습니다.
Atlas search 인덱스
Mongoid를 사용하여 Atlas Search 인덱스를 선언하고 관리 할 수 있습니다.
검색 인덱스 선언하려면 모델 내에서 search_index
매크로를 사용합니다. 모델 내에서 선언된 검색 인덱스를 만들려면 create_search_indexes
명령을 사용합니다. 다음 코드 예시 my_search_index
라는 Atlas Search 인덱스 선언하고 생성하는 방법을 보여줍니다. 인덱스 는 name
및 cuisine
필드에 있으며 동적입니다.
class Restaurant include Mongoid::Document field :name, type: String field :cuisine, type: String field :borough, type: String search_index :my_search_index, mappings: { fields: { name: { type: "string" }, cuisine: { type: "string" } }, dynamic: true } end Restaurant.create_search_indexes
Atlas Search 인덱스 생성하는 구문에 대해 자세히 학습 MongoDB Atlas 문서에서 Atlas Search 인덱스 생성 가이드 를 참조하세요.
Atlas Search 인덱스 제거
Atlas Search 인덱스 제거 하려면 remove_search_indexes
명령을 사용합니다. 다음 코드 예시 restaurants
컬렉션 에서 Atlas Search 인덱스 제거 방법을 보여 줍니다.
Restaurant.remove_search_indexes
Atlas Search 인덱스 나열
search_indexes
명령을 사용하여 컬렉션 의 모든 Atlas Search 인덱스를 열거할 수 있습니다. 다음 예시 restaurants
컬렉션 의 모든 Atlas Search 인덱스를 열거하고 해당 정보를 출력합니다.
Restaurant.search_indexes.each { |index| puts index }
API 문서
Mongoid에서 인덱스를 사용하는 방법에 대해 자세히 학습 Mongoid::Indexable::ClassMethods 문서를 참조하세요.
인덱스 옵션에 대해 자세히 학습 Mongoid::Indexable::Validators::Options 문서를 참조하세요.
Mongoid에서 Atlas Search 인덱스를 사용하는 방법에 대해 자세히 학습 Mongoid::SearchIndexable::ClassMethods 문서를 참조하세요.