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

C# Azure Cosmos DB为字符串数组添加复合索引,c#,.net,azure,azure-cosmosdb,azure-cosmosdb-sqlapi,C#,.net,Azure,Azure Cosmosdb,Azure Cosmosdb Sqlapi,我正在尝试添加一个新的复合索引来进行多字段搜索 我想知道在添加一个新的复合索引时要考虑的事情,并且它对数组字符串是否有效?< /强> 宇宙样本文档 { "id": "ed78b9b5-764b-4ebc-a4f2-6b764679", "OrderReference": "X000011380", "SetReferences": [

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

我想知道在添加一个新的复合索引时要考虑的事情,并且<强>它对数组字符串是否有效?< /强>

宇宙样本文档

{
        "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"
            }
        ]
    ]
}
要从字符串结算引用数组中获取数据,请执行IsProcessed和ReasonCode

我计划增加以下政策

请让我知道这个变化是否足够

此外,我还试着比较了茹的变化前后。我看不出有什么大的区别,都在133.56卢比左右


对于优化的性能还有什么需要考虑的吗?

复合索引对这个查询没有帮助,总体上对等式语句没有任何影响。在查询中执行order by时,它们非常有用。这就是为什么您在查询中看不到任何RU/s缩减。但是,您会注意到写入的RU/s增加

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


另外需要指出的是,默认情况下为所有内容编制索引并有选择地向ExcludedPath添加属性通常是最佳做法。这样,如果您的架构发生更改,它将自动编制索引,而无需重新生成索引。

复合索引将无助于此查询,并且总体上不会对等式语句产生任何影响。在查询中执行order by时,它们非常有用。这就是为什么您在查询中看不到任何RU/s缩减。但是,您会注意到写入的RU/s增加

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


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

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

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

标记“不”,这没有多大作用。我为结算引用添加了一个include路径,比如{“path”:“/SettlementReferences/?”},得到了大约115个Ru。如果不加上索引,我得到的是完全相同的Ru。在实际情况下,我希望索引应该类似于“/SettlementReferences/0/?”、“/SettlementReferences/1/?”,但不确定如何以该格式定义。但当我通过PartitionKey搜索时,只需要5个RU。如果我添加像“/SettlementReferences/[]/?”这样的索引,则标记它有效。现在Ru大约是5。但是我必须看看写作的含义是什么。写作的含义也不多,因为它需要0.14马克。不,它没有多大作用。我为结算引用添加了一个include路径,比如{“path”:“/SettlementReferences/?”},得到了大约115个Ru。如果不加上索引,我得到的是完全相同的Ru。在实际情况下,我希望索引应该类似于“/SettlementReferences/0/?”、“/SettlementReferences/1/?”,但不确定如何以该格式定义。但当我通过PartitionKey搜索时,只需要5个RU。如果我添加像“/SettlementReferences/[]/?”这样的索引,则标记它有效。现在Ru大约是5。但我必须看看写的含义是什么。写的含义也不多,因为它需要0.14卢比
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"
            }
        ]
    ]
}