Azure Cosmos DB 为字符串数组添加复合索引

Azure Cosmos DB Add Composite Index for Array of String

我正在尝试添加一个新的复合索引来进行多字段搜索。

我想知道添加新的复合索引时需要考虑的事项,它对数组字符串有效吗?

示例 Cosmos 文档

{
        "id": "ed78b9b5-764b-4ebc-a4f2-6b764679",
        "OrderReference": "X000011380",
        "SetReferences": [
            "000066474884"
        ],
        "TransactionReference": "ed78b9b5-764b-4ebc-6b7644f06679",
        "TransactionType": "Debit",
        "Amount": 73.65,
        "Currency": "USD",
        "BrandCode": "TestBrand",
        "PartitionKey": "Test-21052020-255",
        "SettlementDateTime": "2020-05-21T04:35:35.133Z",
        "ReasonCode": "TestReason",
        "IsProcessed": true,       
    }

我现有的索引策略

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/PartitionKey/?"
        },
        {
            "path": "/BrandCode/?"
        }
    ],
    "excludedPaths": [
        {
            "path": "/*"
        },
        {
            "path": "/\"_etag\"/?"
        }
    ],
    "compositeIndexes": [
        [
            {
                "path": "/PartitionKey",
                "order": "ascending"
            },
            {
                "path": "/IsProcessed",
                "order": "ascending"
            }
        ]
    ]
}

从字符串 SettlementReferences、IsProcessed、ReasonCode 的数组中获取数据。

SELECT * FROM c WHERE ARRAY_CONTAINS(c.SettlementReferences, '00884') and c.IsProcessed = true and c.ReasonCode = 'TestReason'

我打算添加以下政策

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/PartitionKey/?"
        },
        {
            "path": "/BrandCode/?"
        }
    ],
    "excludedPaths": [
        {
            "path": "/*"
        },
        {
            "path": "/\"_etag\"/?"
        }
    ],
    "compositeIndexes": [
        [
            {
                "path": "/PartitionKey",
                "order": "ascending"
            },
            {
                "path": "/IsProcessed",
                "order": "ascending"
            }
        ],
        [
            {
                "path": "/SettlementReferences",
                "order": "ascending"
            },
            {
                "path": "/IsProcessed",
                "order": "ascending"
            },
            {
                "path": "/ReasonCode",
                "order": "ascending"
            }
        ]
    ]
}

请告诉我此更改是否足够?

另外,我试着比较了更改前后的RU。我没有看到任何巨大差异,两者都在 133.56 俄罗斯左右。

为了优化性能,我还需要考虑什么吗?

复合索引对此查询没有帮助,总体上对等式语句没有任何影响。在您的查询中进行排序时,它们很有用。这就是您在查询中看不到任何 RU/s 减少的原因。但是,您会注意到 RU/s 的写入增加。

如果您想提高查询性能,您应该将 where 子句中的任何属性添加到索引策略中的“includedPaths”中。

另一件需要指出的事情是,默认情况下索引所有内容并有选择地向 excludedPaths 添加属性通常是最佳做法。这样,如果您的架构发生变化,它将自动编入索引,而无需重建索引。

如标记所述,我们需要为数组“/SettlementReferences /[]/?”添加包含路径。添加我的 Ru 数量后,从 115 个减少到 5 个 Ru。