Azure Cosmos DB의 인덱싱 정책 관리

적용 대상: NoSQL

Azure Cosmos DB에서 데이터는 각 컨테이너에 대해 정의된 인덱싱 정책에 따라 인덱싱됩니다. 새로 만든 컨테이너에 대한 기본 인덱싱 정책은 모든 문자열 또는 숫자에 대해 범위 인덱스를 적용합니다. 이 정책을 사용자 지정 인덱싱 정책으로 재정의할 수 있습니다.

참고 항목

이 문서에서 설명하는 인덱싱 정책 업데이트 방법은 Azure Cosmos DB for NoSQL에만 적용됩니다. Azure Cosmos DB for MongoDB의 인덱싱 및 의 보조 인덱싱에 대해 알아봅니다.

인덱싱 정책 예제

다음은 JSON 형식으로 표시되는 인덱싱 정책의 몇 가지 예입니다. JSON 형식으로 Azure Portal에 노출됩니다. Azure CLI 또는 임의의 SDK를 통해 같은 매개 변수를 설정할 수 있습니다.

일부 속성 경로를 선택적으로 제외하는 옵트아웃 정책

{
    "indexingMode": "consistent",
    "includedPaths": [
        {
            "path": "/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/path/to/single/excluded/property/?"
        },
        {
            "path": "/path/to/root/of/multiple/excluded/properties/*"
        }
    ]
}

일부 속성 경로를 선택적으로 포함하는 옵트인 정책

{
    "indexingMode": "consistent",
    "includedPaths": [
        {
            "path": "/path/to/included/property/?"
        },
        {
            "path": "/path/to/root/of/multiple/included/properties/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/*"
        }
    ]
}

참고 항목

일반적으로 거부 인덱싱 정책을 사용하는 것이 좋습니다. Azure Cosmos DB는 데이터 모델에 추가될 수 있는 모든 새 속성을 적극적으로 인덱싱합니다.

특정 속성 경로에 대해서만 공간 인덱스 사용

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/_etag/?"
        }
    ],
    "spatialIndexes": [
        {
            "path": "/path/to/geojson/property/?",
            "types": [
                "Point",
                "Polygon",
                "MultiPolygon",
                "LineString"
            ]
        }
    ]
}

복합 인덱싱 정책 예제

개별 속성에 대한 경로를 포함 또는 제외하는 것 외에, 복합 인덱스를 지정할 수도 있습니다. 여러 속성에 대한 ORDER BY 절이 있는 쿼리를 수행하려면 해당 속성에 대한 복합 인덱스가 필요합니다. 쿼리에 여러 속성에 대한 정렬과 함께 필터가 포함된 경우 둘 이상의 복합 인덱스가 필요할 수 있습니다.

또한 복합 인덱스는 여러 필터가 있거나 필터와 ORDER BY 절이 모두 있는 쿼리에 대한 성능상의 이점이 있습니다.

참고 항목

복합 경로는 해당 /? 경로의 스칼라 값만 인덱싱되므로 암시적입니다. /* 와일드카드는 복합 경로에서 지원되지 않습니다. /? 또는 /* 를 복합 경로에 지정하면 안 됩니다. 복합 경로도 대/소문자를 구분합니다.

(name asc, age desc)에 대해 정의된 복합 인덱스

{  
    "automatic":true,
    "indexingMode":"Consistent",
    "includedPaths":[  
        {  
            "path":"/*"
        }
    ],
    "excludedPaths":[],
    "compositeIndexes":[  
        [  
            {  
                "path":"/name",
                "order":"ascending"
            },
            {  
                "path":"/age",
                "order":"descending"
            }
        ]
    ]
}

다음 쿼리에는 이름과 나이에 대한 복합 인덱스가 필요합니다.

쿼리 #1:

SELECT *
FROM c
ORDER BY c.name ASC, c.age DESC

쿼리 #2:

SELECT *
FROM c
ORDER BY c.name DESC, c.age ASC

이 복합 인덱스는 다음 쿼리에 이점을 제공하고 필터를 최적화합니다.

쿼리 #3:

SELECT *
FROM c
WHERE c.name = "Tim"
ORDER BY c.name DESC, c.age ASC

쿼리 #4:

SELECT *
FROM c
WHERE c.name = "Tim" AND c.age > 18

(name ASC, age ASC) 및 (name ASC, age DESC)에 대해 정의된 복합 인덱스

동일한 인덱싱 정책 내에서 여러 복합 인덱스를 정의할 수 있습니다.

{  
    "automatic":true,
    "indexingMode":"Consistent",
    "includedPaths":[  
        {  
            "path":"/*"
        }
    ],
    "excludedPaths":[],
    "compositeIndexes":[  
        [  
            {  
                "path":"/name",
                "order":"ascending"
            },
            {  
                "path":"/age",
                "order":"ascending"
            }
        ],
        [  
            {  
                "path":"/name",
                "order":"ascending"
            },
            {  
                "path":"/age",
                "order":"descending"
            }
        ]
    ]
}

(name ASC, age ASC)에 대해 정의된 복합 인덱스

순서를 지정하는 것은 선택 사항입니다. 지정하지 않으면 순서는 오름차순입니다.

{  
    "automatic":true,
    "indexingMode":"Consistent",
    "includedPaths":[  
        {  
            "path":"/*"
        }
    ],
    "excludedPaths":[],
    "compositeIndexes":[  
        [  
            {  
               "path":"/name"
            },
            {  
               "path":"/age"
            }
        ]
    ]
}

모든 속성 경로를 제외하고 인덱싱을 활성 상태로 유지

TTL(Time-to-Live) 기능이 활성화되어 있지만 Azure Cosmos DB를 순수 키-값 저장소로 사용하는 데 다른 인덱스가 필요하지 않은 경우 이 정책을 사용할 수 있습니다.

{
    "indexingMode": "consistent",
    "includedPaths": [],
    "excludedPaths": [{
        "path": "/*"
    }]
}

인덱싱 안 함

이 정책은 인덱싱을 해제합니다. indexingModenone으로 설정된 경우 컨테이너에 TTL을 설정할 수 없습니다.

{
    "indexingMode": "none"
}

인덱싱 정책 업데이트

Azure Cosmos DB에서 다음 방법 중 하나를 사용하여 인덱싱 정책을 업데이트할 수 있습니다.

  • Azure Portal에서
  • Azure CLI 사용
  • PowerShell 사용
  • SDK 중 하나 사용

인덱싱 정책 업데이트는 인덱스 변환을 트리거합니다. 이 변환의 진행률을 SDK에서 추적할 수도 있습니다.

참고 항목

인덱싱 정책을 업데이트하면 Azure Cosmos DB에 대한 쓰기가 중단되지 않습니다. 자세한 정보는 인덱싱 변환을 참조하세요.

Important

인덱스를 제거하면 즉시 영향을 받는 반면 새 인덱스를 추가하려면 인덱싱 변환이 필요하므로 다소 시간이 걸립니다. 인덱스 하나를 다른 인덱스로 바꾸는 경우(예: 단일 속성 인덱스를 복합 인덱스로 바꾸기) 먼저 새 인덱스를 추가한 다음, 인덱싱 정책에서 이전 인덱스가 제거되기 전에 인덱스 변환이 완료되기를 기다려야 합니다. 그렇지 않으면 이전 인덱스를 쿼리하는 기능에 부정적인 영향을 미치며 이전 인덱스를 참조하는 활성 워크로드가 중단될 수 있습니다.

Azure Portal 사용

Azure Cosmos DB 컨테이너는 자체의 인덱싱 정책을 Azure Portal에서 직접 편집할 수 있는 JSON 문서로 저장합니다.

  1. Azure Portal에 로그인합니다.

  2. 새 Azure Cosmos DB 계정을 만들거나 기존 계정을 선택합니다.

  3. Data Explorer 창을 열고 작업할 컨테이너를 선택합니다.

  4. 크기 조정 및 설정을 선택합니다.

  5. 와 같이 인덱싱 정책 JSON 문서를 수정합니다.

  6. 완료되면 저장을 선택합니다.

Manage Indexing using Azure portal

Azure CLI 사용

사용자 지정 인덱싱 정책을 사용하여 컨테이너를 만들려면 CLI를 사용하여 사용자 지정 인덱스 정책을 사용한 컨테이너 만들기를 참조하세요.

PowerShell 사용

사용자 지정 인덱싱 정책을 사용하여 컨테이너를 만들려면 PowerShell을 사용하여 사용자 지정 인덱스 정책을 사용한 컨테이너 만들기를 참조하세요.

.NET SDK 사용

ContainerProperties 개체는 .NET SDK v3에서 IndexingPolicy 속성을 노출해 IndexingMode를 변경하고 IncludedPathsExcludedPaths를 추가하거나 제거할 수 있게 합니다. 자세한 내용은 빠른 시작: .NET용 Azure Cosmos DB for NoSQL 클라이언트 라이브러리를 참조하세요.

// Retrieve the container's details
ContainerResponse containerResponse = await client.GetContainer("database", "container").ReadContainerAsync();
// Set the indexing mode to consistent
containerResponse.Resource.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
// Add an included path
containerResponse.Resource.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" });
// Add an excluded path
containerResponse.Resource.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/name/*" });
// Add a spatial index
SpatialPath spatialPath = new SpatialPath
{
    Path = "/locations/*"
};
spatialPath.SpatialTypes.Add(SpatialType.Point);
containerResponse.Resource.IndexingPolicy.SpatialIndexes.Add(spatialPath);
// Add a composite index
containerResponse.Resource.IndexingPolicy.CompositeIndexes.Add(new Collection<CompositePath> { new CompositePath() { Path = "/name", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/age", Order = CompositePathSortOrder.Descending } });
// Update container with changes
await client.GetContainer("database", "container").ReplaceContainerAsync(containerResponse.Resource);

인덱스 변환 진행률을 추적하려면 PopulateQuotaInfo 속성을 true로 설정하는 RequestOptions 개체를 전달합니다. x-ms-documentdb-collection-index-transformation-progress 응답 헤더에서 값을 검색합니다.

// retrieve the container's details
ContainerResponse containerResponse = await client.GetContainer("database", "container").ReadContainerAsync(new ContainerRequestOptions { PopulateQuotaInfo = true });
// retrieve the index transformation progress from the result
long indexTransformationProgress = long.Parse(containerResponse.Headers["x-ms-documentdb-collection-index-transformation-progress"]);

새 컨테이너를 만드는 동안 사용자 지정 인덱싱 정책을 정의할 때 SDK V3의 흐름 API를 사용하여 간결하고 효율적인 방식으로 이 정의를 작성할 수 있습니다.

await client.GetDatabase("database").DefineContainer(name: "container", partitionKeyPath: "/myPartitionKey")
    .WithIndexingPolicy()
        .WithIncludedPaths()
            .Path("/*")
        .Attach()
        .WithExcludedPaths()
            .Path("/name/*")
        .Attach()
        .WithSpatialIndex()
            .Path("/locations/*", SpatialType.Point)
        .Attach()
        .WithCompositeIndex()
            .Path("/name", CompositePathSortOrder.Ascending)
            .Path("/age", CompositePathSortOrder.Descending)
        .Attach()
    .Attach()
    .CreateIfNotExistsAsync();

Java SDK 사용

Java SDKDocumentCollection 개체는 getIndexingPolicy()setIndexingPolicy() 메서드를 노출합니다. 해당 메서드가 조작하는 IndexingPolicy 개체를 사용하여 인덱싱 모드를 변경하고 포함된 경로 및 제외된 경로를 추가 또는 제거할 수 있습니다. 자세한 내용은 빠른 시작: Azure Cosmos DB for NoSQLL 데이터를 관리하기 위한 Java 앱 빌드를 참조하세요.

// Retrieve the container's details
Observable<ResourceResponse<DocumentCollection>> containerResponse = client.readCollection(String.format("/dbs/%s/colls/%s", "database", "container"), null);
containerResponse.subscribe(result -> {
DocumentCollection container = result.getResource();
IndexingPolicy indexingPolicy = container.getIndexingPolicy();

// Set the indexing mode to consistent
indexingPolicy.setIndexingMode(IndexingMode.Consistent);

// Add an included path

Collection<IncludedPath> includedPaths = new ArrayList<>();
IncludedPath includedPath = new IncludedPath();
includedPath.setPath("/*");
includedPaths.add(includedPath);
indexingPolicy.setIncludedPaths(includedPaths);

// Add an excluded path

Collection<ExcludedPath> excludedPaths = new ArrayList<>();
ExcludedPath excludedPath = new ExcludedPath();
excludedPath.setPath("/name/*");
excludedPaths.add(excludedPath);
indexingPolicy.setExcludedPaths(excludedPaths);

// Add a spatial index

Collection<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
Collection<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();

SpatialSpec spec = new SpatialSpec();
spec.setPath("/locations/*");
collectionOfSpatialTypes.add(SpatialType.Point);
spec.setSpatialTypes(collectionOfSpatialTypes);
spatialIndexes.add(spec);

indexingPolicy.setSpatialIndexes(spatialIndexes);

// Add a composite index

Collection<ArrayList<CompositePath>> compositeIndexes = new ArrayList<>();
ArrayList<CompositePath> compositePaths = new ArrayList<>();

CompositePath nameCompositePath = new CompositePath();
nameCompositePath.setPath("/name");
nameCompositePath.setOrder(CompositePathSortOrder.Ascending);

CompositePath ageCompositePath = new CompositePath();
ageCompositePath.setPath("/age");
ageCompositePath.setOrder(CompositePathSortOrder.Descending);

compositePaths.add(ageCompositePath);
compositePaths.add(nameCompositePath);

compositeIndexes.add(compositePaths);
indexingPolicy.setCompositeIndexes(compositeIndexes);

// Update the container with changes

 client.replaceCollection(container, null);
});

컨테이너에서 인덱스 변환 진행률을 추적하려면 채워질 할당량 정보를 요청하는 RequestOptions 개체를 전달합니다. x-ms-documentdb-collection-index-transformation-progress 응답 헤더에서 값을 검색합니다.

// set the RequestOptions object
RequestOptions requestOptions = new RequestOptions();
requestOptions.setPopulateQuotaInfo(true);
// retrieve the container's details
Observable<ResourceResponse<DocumentCollection>> containerResponse = client.readCollection(String.format("/dbs/%s/colls/%s", "database", "container"), requestOptions);
containerResponse.subscribe(result -> {
    // retrieve the index transformation progress from the response headers
    String indexTransformationProgress = result.getResponseHeaders().get("x-ms-documentdb-collection-index-transformation-progress");
});

Node.js SDK 사용

Node.js SDKContainerDefinition 인터페이스는 indexingMode를 변경하고 includedPathsexcludedPaths를 추가하거나 제거할 수 있는 indexingPolicy 속성을 노출합니다. 자세한 내용은 빠른 시작 - Node.js용 Azure Cosmos DB for NoSQL 클라이언트 라이브러리를 참조하세요.

컨테이너의 세부 정보 검색:

const containerResponse = await client.database('database').container('container').read();

인덱싱 모드를 일관되게 설정:

containerResponse.body.indexingPolicy.indexingMode = "consistent";

공간 인덱스를 포함하여 포함된 경로 추가:

containerResponse.body.indexingPolicy.includedPaths.push({
    includedPaths: [
      {
        path: "/age/*",
        indexes: [
          {
            kind: cosmos.DocumentBase.IndexKind.Range,
            dataType: cosmos.DocumentBase.DataType.String
          },
          {
            kind: cosmos.DocumentBase.IndexKind.Range,
            dataType: cosmos.DocumentBase.DataType.Number
          }
        ]
      },
      {
        path: "/locations/*",
        indexes: [
          {
            kind: cosmos.DocumentBase.IndexKind.Spatial,
            dataType: cosmos.DocumentBase.DataType.Point
          }
        ]
      }
    ]
  });

제외된 경로 추가:

containerResponse.body.indexingPolicy.excludedPaths.push({ path: '/name/*' });

변경 내용으로 컨테이너 업데이트:

const replaceResponse = await client.database('database').container('container').replace(containerResponse.body);

컨테이너에서 인덱스 변환 진행률을 추적하려면 populateQuotaInfo 속성을 true로 설정하는 RequestOptions 개체를 전달합니다. x-ms-documentdb-collection-index-transformation-progress 응답 헤더에서 값을 검색합니다.

// retrieve the container's details
const containerResponse = await client.database('database').container('container').read({
    populateQuotaInfo: true
});
// retrieve the index transformation progress from the response headers
const indexTransformationProgress = replaceResponse.headers['x-ms-documentdb-collection-index-transformation-progress'];

Python SDK 사용

Python SDK V3를 사용하면 컨테이너 구성이 사전으로 관리됩니다. 이 사전에서 인덱싱 정책 및 모든 특성에 액세스할 수 있습니다. 자세한 내용은 빠른 시작: Python용 Azure Cosmos DB for NoSQL 클라이언트 라이브러리를 참조하세요.

컨테이너의 세부 정보 검색:

containerPath = 'dbs/database/colls/collection'
container = client.ReadContainer(containerPath)

인덱싱 모드를 일관되게 설정:

container['indexingPolicy']['indexingMode'] = 'consistent'

포함된 경로 및 공간 인덱스를 통해 인덱싱 정책 정의:

container["indexingPolicy"] = {

    "indexingMode":"consistent",
    "spatialIndexes":[
                {"path":"/location/*","types":["Point"]}
             ],
    "includedPaths":[{"path":"/age/*","indexes":[]}],
    "excludedPaths":[{"path":"/*"}]
}

제외된 경로를 사용하여 인덱싱 정책 정의:

container["indexingPolicy"] = {
    "indexingMode":"consistent",
    "includedPaths":[{"path":"/*","indexes":[]}],
    "excludedPaths":[{"path":"/name/*"}]
}

복합 인덱스 추가:

container['indexingPolicy']['compositeIndexes'] = [
                [
                    {
                        "path": "/name",
                        "order": "ascending"
                    },
                    {
                        "path": "/age",
                        "order": "descending"
                    }
                ]
                ]

변경 내용으로 컨테이너 업데이트:

response = client.ReplaceContainer(containerPath, container)

다음 단계

다음 문서에서 인덱싱에 대해 자세히 알아보세요.