Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue while getting the latest record by time from cosmos database

I worked on simple web application, in that I used the cosmos database to save some data into it. Up to now everything working fine. But whenever I am trying to get the latest value by time from cosmos database, then it’s take more than one minute to give the latest record.

This is the code I wrote for getting the latest record from cosmos database.

        public static async Task<IEnumerable<IoTHubDataPoint>> GetItemsAsync()
    {
        IDocumentQuery<IoTHubDataPoint> query = client.CreateDocumentQuery<IoTHubDataPoint>(
            UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),               
            new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true}).OrderByDescending(x=>x.Time).Take(1)
            .AsDocumentQuery();

        List<IoTHubDataPoint> results = new List<ServiceMaxIoTHubDataPoint>();
        while (query.HasMoreResults)
        {
            results.AddRange(await Task.Run(async () => await query.ExecuteNextAsync<IoTHubDataPoint>()) );
        }
        //return results.OrderByDescending(x => x.Time).Take(1).ToArray();
        return results;

    }

This is the sample data avaialble inside the azure cosmos database.

{
"DeviceID": "IoT-1",
"Time": "2017-11-02T14:46:06.7846161",
"Temperature": 28.63403,
"Pressure": "95089.47",
"Altitude": "532.5873",
"LightStatus": "Too Bright",
"LightCDSValue": "193",
"LightCDSVoltageValue": "0.943304",
"EventProcessedUtcTime": "2017-11-02T14:46:40.3930989Z",
"PartitionId": 0,
"EventEnqueuedUtcTime": "2017-11-02T14:46:07.6Z",
"IoTHub": {
    "MessageId": null,
    "CorrelationId": null,
    "ConnectionDeviceId": "IoT-1",
    "ConnectionDeviceGenerationId": "636449561753440920",
    "EnqueuedTime": "2017-11-02T14:46:07.826Z",
    "StreamId": null
},
"id": "XXXXXXXXXXXXXX",
"_rid": "XXXXXXXXXXXXXx",
"_self": "dbs/4RM3AA==/colls/4RM3AOJ1XQA=/docs/XXXXXXXXXXXx==/",
"_etag": "\"XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXx\"",
"_attachments": "attachments/",
"_ts": XXXXXX
}

Can anyone please tell me how to resolve the above issue?

like image 266
Pradeep Avatar asked Nov 23 '17 12:11

Pradeep


People also ask

Why is Cosmos DB so slow?

Request throttling is the most common reason for slow requests. Azure Cosmos DB throttles requests if they exceed the allocated request units for the database or container. The SDK has built-in logic to retry these requests.

Which of the following is true about time to leave feature of Azure Cosmos DB?

The item will never expire.

What is latency in Cosmos DB?

For Azure Cosmos DB accounts configured with strong consistency with more than one region, the write latency is equal to two times round-trip time (RTT) between any of the two farthest regions, plus 10 milliseconds at the 99th percentile.


1 Answers

Here is the query you can execute in Azure SELECT top 1 * FROM c order by c._ts desc

like image 105
Robert Green MBA Avatar answered Oct 05 '22 12:10

Robert Green MBA