【问题标题】:Azure Cosmos DB Add Composite Index for Array of StringAzure Cosmos DB 为字符串数组添加复合索引
【发布时间】:2020-08-25 16:48:34
【问题描述】:

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

我想知道在添加新的复合索引时要考虑的事项以及它是否适用于数组字符串?

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 Rus 左右。

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

【问题讨论】:

    标签: c# .net azure azure-cosmosdb azure-cosmosdb-sqlapi


    【解决方案1】:

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

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

    还要指出的另一件事是,通常最好的做法是默认索引所有内容并有选择地向 excludePaths 添加属性。这样,如果您的架构发生更改,它将自动被索引,而无需重建您的索引。

    【讨论】:

    • 标记不,它没有做太多。我为 SettlementReferences 添加了一个包含路径,例如 { "path": "/SettlementReferences /?" } 我得到了大约 115 Ru。如果没有添加该索引,我将得到完全相同的 Ru。实际上,我希望索引应该类似于“/SettlementReferences /0/?” , "/SettlementReferences /1/?"但不确定如何以这种格式定义。但是当我通过 PartitionKey 搜索时只需要 5 rus。
    • 如果我添加诸如“/SettlementReferences /[]/?”之类的索引,则标记它有效。现在 Ru's 大约是 5。但我必须看看 write 的含义是什么。
    • 写入含义也不多,因为它需要 0.14 rus 更多
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      相关资源
      最近更新 更多