개요
이 가이드 에서는 C 운전자 에서 인덱스를 사용하는 방법을 학습 수 있습니다. 인덱스는 쿼리의 효율성 높이고 문서 쿼리 및 저장에 기능을 추가할 수 있습니다.
인덱스가 없으면 MongoDB는 컬렉션의 모든 문서를 스캔하여 각 쿼리와 일치하는 문서를 찾아야 합니다. 이러한 컬렉션 스캔은 속도가 느리고 애플리케이션 성능에 부정적인 영향을 미칠 수 있습니다. 그러나 쿼리에 적합한 인덱스가 있는 경우 MongoDB는 인덱스를 사용하여 검사해야 하는 문서를 제한할 수 있습니다.
운영 고려 사항
쿼리 성능을 향상시키려면 애플리케이션의 쿼리와 정렬된 결과를 반환하는 작업에 자주 나타나는 필드에 인덱스를 빌드 하세요. 추가하는 각 인덱스 는 활성 상태일 때 디스크 공간과 메모리를 사용하므로 용량 계획을 위해 인덱스 메모리와 디스크 사용량을 추적 하는 것이 좋습니다. 또한 쓰기 (write) 작업이 인덱싱된 필드 를 업데이트할 때 MongoDB 는 관련 인덱스 를 업데이트하므로 쓰기 (write) 작업의 성능에 부정적인 영향 을 미칠 수 있습니다.
MongoDB 애플리케이션 에서 와일드카드 인덱스 를 사용하여 이름이 미리 알려지지 않았거나 임의적인 필드에 대해 쿼리 할 수 있습니다. 와일드카드 인덱스는 워크로드 기반 인덱스 계획을 대체하도록 설계되지 않았습니다.
데이터 모델 을 설계하고 애플리케이션 에 적합한 인덱스를 선택하는 방법에 대한 자세한 내용은 MongoDB Server 매뉴얼의 데이터 모델링 및 인덱스 가이드 를 참조하세요.
샘플 데이터
이 가이드 의 예제에서는 Atlas 샘플 데이터 세트의 데이터베이스 에 있는 및 컬렉션 사용합니다.movies theaters sample_mflix 무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 Atlas 시작하기를 참조하세요.
인덱스 만들기
MongoDB 는 데이터 쿼리 에 도움이 되는 여러 가지 인덱스 유형을 지원합니다. 다음 페이지에서는 가장 일반적인 인덱스 유형을 설명하고 각 인덱스 유형을 생성하기 위한 샘플 코드를 제공합니다.
다음 섹션에서는 다른 일반적인 인덱스 유형에 대해 설명하고 각 인덱스 유형을 생성하기 위한 샘플 코드를 제공합니다.
지리 공간적 인덱스
다음 예시 theaters 컬렉션 의 GeoJSON 객체를 포함하는 location.geo 필드 에 2dsphere 인덱스 생성합니다.
bson_error_t error; bson_t *keys = BCON_NEW("location.geo", BCON_UTF8("2dsphere")); mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL); if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) { printf("Successfully created index\n"); } else { fprintf(stderr, "Failed to create index: %s", error.message); } bson_destroy(keys); mongoc_index_model_destroy(index_model);
GeoJSON 데이터 유형 에 학습 보려면 MongoDB Server 매뉴얼에서 GeoJSON 객체 를 참조하세요.
고유 인덱스
다음 예시 title 필드 에 오름차순 고유 인덱스 생성합니다.
bson_error_t error; bson_t *keys = BCON_NEW("title", BCON_INT32(1)); bson_t *opts = BCON_NEW("unique", BCON_BOOL(true)); mongoc_index_model_t *index_model = mongoc_index_model_new(keys, opts); if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) { printf("Successfully created index\n"); } else { fprintf(stderr, "Failed to create index: %s", error.message); } bson_destroy(keys); bson_destroy(opts); mongoc_index_model_destroy(index_model);
와일드카드 인덱스
다음 예시 지정된 컬렉션 에 오름차순 와일드카드 인덱스 생성합니다.
bson_error_t error; bson_t *keys = BCON_NEW("$**", BCON_INT32(1)); mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL); if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) { printf("Successfully created index\n"); } else { fprintf(stderr, "Failed to create index: %s", error.message); } bson_destroy(keys); mongoc_index_model_destroy(index_model);
클러스터된 인덱스
다음 예시 _id 필드 에 오름차순 클러스터형 인덱스 사용하여 새 컬렉션 을 만듭니다.
bson_error_t error; bson_t *opts = BCON_NEW("clusteredIndex", "{", "key", "{", "_id", BCON_INT32(1), "}", "unique", BCON_BOOL(true), "}"); mongoc_database_t *database = mongoc_client_get_database(client, "sample_mflix"); if (mongoc_database_create_collection(database, "clustered_collection", opts, &error)) { printf("Successfully created collection\n"); } else { fprintf(stderr, "Failed to create collection: %s", error.message); } mongoc_database_destroy(database); bson_destroy(opts);
Text Index
다음 예시 title 문자열 필드 에 텍스트 인덱스 생성합니다.
bson_error_t error; bson_t *keys = BCON_NEW("title", BCON_UTF8("text")); mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL); if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) { printf("Successfully created index\n"); } else { fprintf(stderr, "Failed to create index: %s", error.message); } bson_destroy(keys); mongoc_index_model_destroy(index_model);
인덱스 제거
경고
검색 인덱스 및 클러스터 삭제는 돌이킬 수 없습니다.
검색 인덱스 및 관련 클러스터를 삭제하는 것은 영구적인 조치입니다. MongoDB는 삭제된 검색 인덱스 또는 데이터 복구를 지원하지 않습니다. 진행하기 전에 백업 생성과 같은 적절한 조치를 취하여 데이터 손실을 방지하세요.
MongoDB는 삭제된 검색 인덱스 또는 데이터를 복구하도록 요청하는 것을 지원하지 않습니다. 클러스터의 데이터 무결성과 구성에 대한 책임은 사용자에게 있습니다.
_id 필드 에서 기본값 고유 인덱스 제외한 사용하지 않는 인덱스 제거 할 수 있습니다. mongoc_collection_t 구조체, 인덱스 이름 및 선택적 bson_error_t 구조체를 mongoc_collection_drop_index() 함수에 전달하여 컬렉션 에서 인덱스 를 제거 .
다음 예에서는 movies 컬렉션에서 이름이 "_title_" 인 인덱스를 제거합니다.
bson_error_t error; if (mongoc_collection_drop_index(collection, "_title_", &error)) { printf("Successfully dropped index\n"); } else { fprintf(stderr, "Failed to drop index: %s", error.message); }
참고
복합 텍스트 인덱스에서 단일 필드를 제거할 수 없습니다. 인덱스된 필드를 업데이트하려면 인덱스를 전부 제거하고 새로운 인덱스를 생성해야 합니다.
API 문서
이 가이드 에서 설명하는 함수에 대해 자세히 학습 다음 API 문서를 참조하세요.