WAQS: Query parallelization

7 reasons to use WAQS

WAQS documentation

 

In some cases, like ViewModel initialization, we need to execute many queries.

Of course you can basically execute them sequentially but you also can execute each of them using only one HTTP request.

To dot it with WAQS, you can use the ExecuteQueries method:

var result = await _context.ExecuteQueriesAsync(
_context.Suppliers.AsAsyncQueryable(),
_context.Products.AsAsyncQueryable().FirstOrDefault()); var suppliers = ((IEnumerable<object>)result[0]).Cast<Supplier>(); var product = (Product)result[1];


Now, in the server, if the database is not a contention point, it’s better to parallelize the n (2 in our sample) queries.



But, unfortunately, the Entity Framework context is not thread safe and you can’t execute many queries in parallel with the same context (still true with EF6 and async methods).



So the tip is to create many contexts.



In our case, server contexts are stateless and only used to querying / saving data.



So using many context instances is not problem.



 



So we can execute queries in parallel on the server.



However, if the database is a contention point, parallelizing queries could be an issue for scalability.



 



So, with WAQS, it’s an option.



The server.waqs file (generated when we ran NuGet package commands) describes generation configuration.



It contains a service element with an option for parallelization:



<Service Parallel="true" />


If Parallel is set to false, queries will be executed sequentially in the server using the same context instance.

This entry was posted in 16868, 7674. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>