首页 > 解决方案 > 为什么 Cosmos DB 在索引属性上显示索引利用率 0.00 %?

问题描述

你能说出为什么索引利用率是 0.00 % 吗?

我在索引策略中包含了这条路径:

{
    "path": "/receivedDateTime/?",
    "indexes": [
        {
            "kind": "Range",
            "dataType": "Number",
            "precision": -1
        },
        {
            "kind": "Hash",
            "dataType": "String",
            "precision": 3
        }
    ]
}

这是我的文件的一个例子:

{
    "id": "",
    "userId": "XYZ",
    ...
    "receivedDateTime": 635903262620000000,
    ...
}

当我运行以下查询时,索引利用率为零。笔记:

  1. 我在索引策略中包含的任何其他属性/路径都不会发生这种情况
  2. UserId 是分区键

    client.CreateDocumentQuery<Message>(UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
                    new FeedOptions
                    {
                        PopulateQueryMetrics = true,
                        MaxItemCount = maxItemCount
                    })
                .Where(item => item.UserId == Guid.Parse("XYZ"))
                .OrderBy(m => m.ReceivedDateTime)
                .AsDocumentQuery();
    

图片

另一方面,如果我添加item.ReceivedDateTime >= 0到 where 子句,即使item.ReceivedDateTime >= 0所有文档都为 true,我也会得到 98.02% 的索引利用率。

client.CreateDocumentQuery<Message>(UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
                    new FeedOptions
                    {
                        PopulateQueryMetrics = true,
                        MaxItemCount = maxItemCount
                    })
                .Where(item => item.UserId == Guid.Parse("XYZ") && item.ReceivedDateTime >= 0)
                .OrderBy(m => m.ReceivedDateTime)
                .AsDocumentQuery();

图片

谢谢

标签: azureazure-cosmosdbazure-cosmosdb-sqlapi

解决方案


像我的其他数据库一样,索引属性可能在存储时已经排序。因此,使用OrderBy不会像实际对其进行索引那样密集Where

这里有更多关于它的信息,给你一个提示:https ://azure.microsoft.com/en-gb/blog/order-query-results-with-azure-documentdb/


推荐阅读