简体   繁体   中英

Azure Cosmos Query against Date By Range

I am querying data against one field where the datatype is Datetime for example my query looks like this,

Select * from c where c.datetime >= '19/04/2021' and c.datetime <= '23/04/2021'and c.age = 25

there is no issue while reading the data but it's taking too much time and RU's.

can someone suggest me or help me by giving me a example code to add the range or composite indexes in cosmos indexing policy for the above query.

When you have a slow or high request unit (RU) query, you can optimize it with a composite index. By default, no composite indexes are defined so you would have to add composite indexes as needed.

Query:

Select * from c where c.age = 25 and c.datetime >= '19/04/2021' and c.datetime <= '23/04/2021';

Composite indexe:

(age ASC, datetime ASC)

As explained in the MS doc here :

Note: When you add a composite index, the query will utilize existing range indexes until the new composite index addition is complete. Therefore, when you add a composite index, you may not immediately observe performance improvements. It is possible to track the progress of index transformation by using one of the SDKs .

For more details checkout: Composite indexing policy examples and Updating indexing policy using the Azure portal

{  
        "automatic":true,
        "indexingMode":"Consistent",
        "includedPaths":[  
            {  
                "path":"/*"
            }
        ],
        "excludedPaths":[],
        "compositeIndexes":[  
            [  
                {  
                    "path":"/age"
                },
                {  
                    "path":"/datetime"
                }
            ]
        ]
}

It is optional to specify the order. If not specified, the order is ascending.

在此处输入图像描述

Working with datetimes

I am querying data against one field where the datatype is Datetime..

First of all, CosmosDB is a document storage for JSON documents. JSON notation does NOT have a data type DateTime . If you look at your query, you are actually passing strings. If you look at your stored documents, you are storing strings. CosmosDB does not know nor care if they represent points in time in your app.

Hence the ordering of your datetimes in your query (and index) IS NOT chronological, but literal. Most likely you wanted the former, though.

To make your strings with datetimes indexable as time, you must use a format in which the literal order and chronological order is the same. Usually ISO-8601.

You should start by reading this for more details: Working with Dates in Azure Cosmos DB .

About composite indexes

It heavily depends how you model your data, data distribution and optimization priorities, but...

... usually a simpler and more universal single-path index is preferred due to smaller index size and higher reusability. While you could add a lot of composite indexes to get small gains in specific queries, but each added index does bring a cost in index maintenance (inserts, updates, deletes) + dev maintenance + index storage. DO add only add indexes you actually use and test the RU cost before adding heavier indexes.

See also: Performance tips for Azure Cosmos DB and .NET - Indexing policy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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