How can Improve inconsistency performance of retrieving data from Cosmos DB? [C# Library - LowCode Environment]

61 views Asked by At

My scenario is slightly different than normal

The tech stack is using something called OutSystems a 'low-code' platform. We have a C# library for connecting to Cosmos DB, but this is only initiated when it's in use. It isn't a constant running API, therefor each time we call the DB, the Cosmos connection is initiated for the first time.

I am running a query which returns about 8k records which has 10 columns of small data. When running my query I am getting really inconsistent timings ranging from 800ms - 3000ms. I am pretty sure I am on the only person using and querying it at the moment.

My code:

    private async Task<string> QueryItemsAsync(string filter, string partitionKey, int offset, int limit, string columns = null)
    {
        var sqlQuery = QueryBuilder(filter, partitionKey, offset, limit, columns);
        var results = new List<Dictionary<string, string>>();

        using (var feedIterator = this.container.GetItemQueryIterator<Dictionary<string, string>>(sqlQuery, null, new QueryRequestOptions() { }))
        {
            while (feedIterator.HasMoreResults)
            {
                foreach (var item in await feedIterator.ReadNextAsync().ConfigureAwait(false))
                {
                    results.Add(item);
                }
            }
        }
        return JsonConvert.SerializeObject(results);
    }

I've noticed that once I query over 1000 rows the performance seems to be the same as if it was 8000.

If I query 'Top 1' the return time is < 40ms. If I query 'Top 1000' the return time is > 1200ms

  1. Is this the most efficient loop?
  2. If anyone can refactor it, I will love you forever

With the tech stack, it makes debugging super difficult as I cannot live debug. I have to write logs and to console... It takes about 5 minutes to make a change and debug so it's not a quick task hence why I am asking SO.

Thanks in advance, Sam

edit: The query returns a flat JSON structure

0

There are 0 answers