'Run queries concurrently using Cosmos DB SQL API

I ran into a problem where the Cosmos SDK was generating a query, that had too many characters


// A list with over 100k items
var carPositionIds = new List<CarPositionsIds>()

...

var iterator = CarContainer
.GetItemLinqQueryable<Car>(true)
.Where(x => carPositionIds.Contain(x.Id))
.ToFeedIterator();

The error: The SQL query text exceeded the maximum limit of 30720 characters in Azure CosmosDB

I understood it was because of the .Contains. So rather then have one big query, i split the carPositionIds list into smaller lists and run many smaller queries concurrently

var ListOfIds = carPositionIds.Split(10000); // Returns a IEnumerable<IEnumerable<T>>, where each list has up to 10000 items


var conTasks = new List<Task<FeedResponse<Car>>>();

foreach(var ids in listOfIds)
{
    var iterator = CarContainer
      .GetItemLinqQueryable<Car>(true)
      .Where(x => ids.Contain(x.Id))
      .ToFeedIterator();

 while(iterator.HasMoreResults)
       {
         var i = iterator.ReadNextAsync();
         conTasks.Add(i);
       {
}

await Task.WhenAll(conTasks);
    
    var cars = new List<Cars>();
    foreach(var t in conTasks)
    {
        cars.AddRange(t.Result);
    }


Everytime I run this, I get different amount of Cars in the Cars list. How can I run multiple queries concurrently?



Solution 1:[1]

  • Thank you and glad that you have cleared the issue.

  • Posting the answer here so that it can be helpful for other community members.

  • Below is the resolution steps:

public async Task<List<Cars>> GetCarsByIds(int ids)
{
    var result = new List<Cars>();

    var iterator = CarContainer
        .GetItemLinqQueryable<Car>(true)
        .Where(x => ids.Contain(x.Id))
        .ToFeedIterator();   

    while(iterator.HasMoreResults) 
    {          
        var i = await iterator.ReadNextAsync();          
        result.AddRange(i);        
    }  

    return result;
}

//...

var conTasks = new List<Task<FeedResponse<Car>>>();  
foreach(var ids in listOfIds) {     
    conTasks.Add(GetCarsByIds(ids))
}

// Add results into array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Theodor Zoulias