简体   繁体   English

Cosmos DB日期索引无效

[英]Cosmos DB Date Index Not Efficient

I have a collection with a Date field that is populated by a C# application using a DateTime object. 我有一个带有Date字段的集合,该集合由C#应用程序使用DateTime对象填充。 This field is serialized to the following format "2018-06-10T17:32:48.3285735Z". 该字段被序列化为以下格式“ 2018-06-10T17:32:48.3285735Z”。

I haven't touched the Index Policy in the collection, so strings are using the Range index type. 我没有触及集合中的索引策略,因此字符串使用的是Range索引类型。 From what I've read in the documentation, that's the most efficient way to index dates, however, when I use the Date field in an ORDER BY clause, the query consumes at least 10x more RUs than if I were to query using the timestamp (_ts) number field. 根据我在文档中阅读的内容,这是对日期建立索引的最有效方法,但是,当我在ORDER BY子句中使用Date字段时,与使用时间戳进行查询相比,查询消耗的RU至少多10倍。 (_ts)数字字段。 That means paying 10x more for this single collection. 这意味着要为此单次收藏多付10倍。

To illustrate the issue: 为了说明这个问题:

SELECT TOP 100 * FROM c ORDER BY c.Date DESC
//query consumes a minimum of 500 RUs

SELECT TOP 100 * FROM c ORDER BY c._ts DESC
//query consumes 50 RUs

Is this how it is supposed to work or am I missing something? 这是应该工作的方式还是我错过了什么? I suspect that if this was the expected behavior, it would be emphasized in the index documentation, and storing dates as numbers would be highlighted as the best practice. 我怀疑如果这是预期的行为,那么它会在索引文档中得到强调,并且将日期存储为数字会被强调为最佳做法。

EDIT: This is the index policy for the collection (I never changed it). 编辑:这是集合的索引策略(我从未更改过)。

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/*",
            "indexes": [
                {
                    "kind": "Range",
                    "dataType": "Number",
                    "precision": -1
                },
                {
                    "kind": "Range",
                    "dataType": "String",
                    "precision": -1
                },
                {
                    "kind": "Spatial",
                    "dataType": "Point"
                }
            ]
        }
    ],
    "excludedPaths": []
}

This may have to do with index collisions (multiple values map to the same index term). 这可能与索引冲突有关(多个值映射到相同的索引项)。 You may want to narrow the range of the filed Date and see if that helps. 您可能想缩小提交日期的范围,看看是否有帮助。 Basically, try this query: 基本上,请尝试以下查询:

SELECT TOP 100 * FROM c WHERE (c.Date BETWEEN '2000-01-01' AND '2100-01-01') ORDER BY c.Date DESC

Please note that the added filter should not charge the query result set. 请注意,添加的过滤器不应对查询结果集收费。

Did you try specifically configuring for Range Queries? 您是否尝试过专门配置范围查询?

I think by default strings are hashed and you have to specify indexing for range queries. 我认为默认情况下字符串是散列的,您必须为范围查询指定索引。

I found this in the documentation: 我在文档中找到了这个:

By default, Azure Cosmos DB indexes all string properties within documents consistently with a Hash index. 默认情况下,Azure Cosmos DB使用散列索引一致地索引文档中的所有字符串属性。

Documentation link 文档链接

For setting up a range query index on the collection: 在集合上设置范围查询索引:

DocumentCollection collection = new DocumentCollection { Id = "orders" }; 

collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) 
                                { Precision = -1 });     

await client.CreateDocumentCollectionAsync("/dbs/orderdb", collection);

The document they are querying against looks like this: 他们要查询的文档如下所示:

{
 "id": "09152014101",
 "OrderDate": "2014-09-15T23:14:25.7251173Z",
 "ShipDate": "2014-09-30T23:14:25.7251173Z",
 "Total": 113.39 
}

Documentation link 文档链接

I believe this is an optimisation deficiency when the query uses TOP and ORDER BY. 我相信当查询使用TOP和ORDER BY时,这是优化方面的不足。 I've found that whilst there is not much difference in RU for a range query using timestamp as number and timestamp as string, in scenarios such as yours the range index on string appears to be ignored. 我发现,虽然使用时间戳记作为数字和使用时间戳记作为字符串的范围查询的RU差异不大,但是在诸如您这样的方案中,字符串上的范围索引似乎被忽略了。

User Voice issue here: https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/32345410-optimise-top-with-order-by-clause-queries 此处的用户语音问题: https : //feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/32345410-optimise-top-with-order-by-clause-queries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM