tartar
tartar

Reputation: 149

Faster Query to fetch details from Cosmos DB

I am fetching the list of persons from Cosmos DB using a LINQ expression mentioned below from an array(String[] getPersons) as input.

var container = db.GetContainer(containerId);
var q = container.GetItemLinqQueryable<Person>();
var iterator = q.Where(p => getPersons.Contains(p.Name)).ToFeedIterator();
var results = await iterator.ReadNextAsync();

I could able to get result but it is taking more time(>15 sec) to retrieve data(more than 1K records) from CosmosDB. I need to get the data with in < 1 sec. Is there any way to optimise the above query to achieve this?

Upvotes: 0

Views: 1131

Answers (1)

Mark Brown
Mark Brown

Reputation: 8763

There could be multiple things that you can do but my intuition thinks this is not an SDK issue but one of design.

Is the partition key person name? If not then you are running a cross-partition query which will never scale. In fact, your performance will worsen as you add more data.

I suggest taking a look at Choosing a Partition key to learn more about how to create a database that can scale easily.

Also, if you're new to this type of database, this article is super helpful too, How to model and partition data on Azure Cosmos DB using a real-world example

Once you have a design that can scale where queries are answered from a single (or a bounded set of) partitions, your query performance will drastically increase.

Upvotes: 1

Related Questions