쿼리를 지원하는 인덱스 생성
An index covers a query when the index contains all of the fields scanned by the query. A covered query scans the index and not the collection, which improves query performance.
Indexes can also partially support queries if a subset of the fields queried are indexed.
이 작업에 대하여
A single collection can have a maximum of 64 indexes. However, too many indexes can degrade performance before that limit is reached. For collections with a high write-to-read ratio, indexes can degrade performance because each insert must also update any indexes.
단계
Identify common queries
To identify common query patterns in your application, use the
$queryStats
aggregation stage. $queryStats
reports
metrics for query shapes, which group
queries based on shared fields.
Create indexes to support common queries
After you know which fields your application frequently queries, you can create indexes to support queries on those fields. For more information, see 예시.
Analyze index use
After your application begins using indexes, you can analyze your indexes' effectiveness. To see index statistics and usage, you can:
Use the
$indexStats
aggregation stage.For MongoDB Atlas deployments, view Indexes in the Atlas UI.
Consider deleting unused indexes to improve application performance. For more information, see 불필요한 인덱스 제거.
Repeat this procedure periodically to ensure that your indexes support your current workload.
예시
Create a Single-Key Index
If your application only queries on a single key in a given collection,
then you need to create a single-key index for that collection. For
example, you can create an index on category
in the product
collection:
db.products.createIndex( { category: 1 } )
The preceding index supports this query:
db.products.find( { category: "electronics" } )
복합 색인 만들기
If your application performs queries on both a single key and multiple
keys, a 복합 인덱스 is more efficient
than a single-key index. For example, you can create an index on the
category
, item
, and location
fields:
db.products.createIndex( { category: 1, item: 1, location: 1 } )
인덱스 접두사
A compound index supports queries on index prefixes, which are the beginning subsets of indexed fields. For example, the preceding index supports these queries:
db.products.find( { category: "electronics" } ) db.products.find( { category: "electronics", item: "television" } )
For more information and performance considerations on index prefixes, see 인덱스 접두사.
텍스트 검색을 지원하는 인덱스 생성
MongoDB Atlas에서 호스팅되는 데이터의 경우 Atlas Search 인덱스를 사용하여 전체 텍스트 검색을 지원할 수 있습니다. 자세한 내용은 Atlas Search 인덱스 생성을 참조하세요.
(Atlas가 아닌) 자체 관리 배포서버의 경우, MongoDB 는 컬렉션 에서 string 콘텐츠 검색을 지원하는 text
인덱스 유형을 제공합니다. 자체 관리형 텍스트 인덱스에 학습 보려면 자체 관리형 배포의 텍스트 인덱스를 참조하세요.
Create Vector Search Indexes
Vector Search Indexes support queries on vector embeddings. To create Vector Search Indexes, see Index Fields for Vector Search.
인덱스 사용 및 데이터 정렬
문자열 비교에 인덱스를 사용하려면 작업에서 동일한 데이터 정렬도 지정해야 합니다. 즉, 작업에서 다른 데이터 정렬을 지정하는 경우 데이터 정렬이 있는 인덱스는 인덱싱된 필드에서 문자열 비교를 수행하는 작업을 지원할 수 없습니다.
경고
데이터 정렬로 구성된 인덱스는 ICU 데이터 정렬 키를 사용하여 정렬 순서를 지정하므로, 데이터 정렬이 없는 인덱스의 경우 데이터 정렬 인식 인덱스 키가 인덱스 키보다 클 수 있습니다.
예를 들어, 컬렉션 myColl
에는 대조 국가 및 언어 설정이 "fr"
인 문자열 필드 category
에 대한 색인이 있습니다.
db.myColl.createIndex( { category: 1 }, { collation: { locale: "fr" } } )
인덱스와 동일한 데이터 정렬을 지정하는 다음 쿼리 작업에서는 인덱스를 사용할 수 있습니다.
db.myColl.find( { category: "cafe" } ).collation( { locale: "fr" } )
하지만 기본적으로 '단순' 바이너리 데이터 정렬기를 사용하는 다음 쿼리 작업에서는 인덱스를 사용할 수 없습니다.
db.myColl.find( { category: "cafe" } )
인덱스 접두사 키가 문자열, 배열 및 내장된 문서가 아닌 복합 인덱스의 경우, 다른 데이터 정렬을 지정하는 작업에서도 인덱스를 사용하여 인덱스 접두사 키에 대한 비교를 지원할 수 있습니다.
예를 들어, 컬렉션 myColl
에는 숫자 필드 score
및 price
와 문자열 필드 category
에 복합 인덱스가 있습니다. 인덱스는 문자열 비교를 위해 데이터 정렬 국가 및 언어 설정 "fr"
을 사용하여 만들어집니다.
db.myColl.createIndex( { score: 1, price: 1, category: 1 }, { collation: { locale: "fr" } } )
문자열 비교에 "simple"
이진 데이터 정렬을 사용하는 다음 작업에서는 인덱스를 사용할 수 있습니다.
db.myColl.find( { score: 5 } ).sort( { price: 1 } ) db.myColl.find( { score: 5, price: { $gt: NumberDecimal( "10" ) } } ).sort( { price: 1 } )
색인이 생성된 category
필드의 문자열 비교를 위해 "simple"
이진 데이터 정렬을 사용하는 다음 작업은 색인을 사용하여 쿼리의 score: 5
부분만 수행할 수 있습니다.
db.myColl.find( { score: 5, category: "cafe" } )
중요
내장된 문서 키를 비롯한 문서 키와의 일치는 단순 이진 비교를 사용합니다. 즉, "foo.bár"와 같은 키에 대한 쿼리는 "foo.bar" 키와 일치하지 않습니다. 이는 강도 매개변수에 설정한 값에 관계없이 적용됩니다.