Join Chander Dhall for an in-depth discussion in this video Parallel query, part of Cosmos DB: Import, Manipulate, Index and Query.
- [Instructor] Now that we've seen how to do some queries, what we could do is get rid of some of this code and show you a really cool way of doing parallel queries. So for example, I have something like var companies = create a query which has some options since we changed this to DefaultOptions. We can make that change here. Now, this is a very sequential query. So the first thing I'd like to do is convert this to AsDocumentQuery.
And in order to resolve that, we're going to use Azure.Documents.Linq. And once we do that, we have the DocumentQuery, but what we'd like to do is create a parallel query that have a list of companies and then I want to add some of my options here and this is going to be FeedOptions. And in my FeedOptions, what I could do is MaxDegreeOfParallelism.
And if I set zero, that would mean that this is pretty much a sequential query like the three queries we just did. However, I'd like to make it 10. And then you could do something like MaxBufferedItemCount. MaxBufferedItemCount means no matter what your client is, in this case we have the .net client, it can only hold up to 100 values at one point of time so that's like caching 100 values at the client. And then of course you're going to make sure that we're enabling the CrossPartitionQuery.
That's all we have to do. And then we can get the results and we can say while... And this is a query right now so I'm going to go change this name to Query so it's not confusing. So we'll say while Query has more results, we'll each rate through this, and we'll say Company company in, notice that this is where you use await and that's one important aspect and that's it.
So we execute next Async and this is where we can say parallelQuery.Add(company). And in order to check the results, what we could do is we could use query.toolList and then see all the results here. I meant parallelQuery.toolList. Just to bring these results out, what I'll do is I'll just do a ReadLine here and do a Control + F5.
So as you can see, we got a good amount of results, except this time the query ran parallelly.
- Data import scenarios
- Creating a database
- Creating a partitioned collection
- Data manipulation
- Importing documents with a stored procedure
- User-defined functions
- Excluding indexing at a document level
- Range indexing on strings
- Querying with SQL parameters
- Range operations