1

My knowledge around async behaviour in C# is a little rusty and I'm wondering whether changing some code to by async will be of benefit to us or whether it may in fact reduce the efficiency of the code.

I have a collection of methods that each execute a stored procedure through entity framework. We return an IEnumerable<SomeDataType>.

Each method in this collection can run independently of the others and so I got to thinking about running them in parallel.

Something a former colleague told me about DB queries all running synchronously, and wrapping DB calling code in Tasks slowing down the system, popped into my head.

  • Can there be benefits to wrapping DB calling code in Tasks? or...
  • Will I end up slowing our system down by wrapping DB calls in Tasks?
  • Are DB queries all executed in a synchronous fashion?

Our methods look like this:

private IEnumerable<Models.Permission> PermissionsForUser(Context entities,int userId)
{
    var userperms = entities.sssp_GetUserPermissions(userId);
    return ...
}

2 Answers 2

2

Can there be benefits to wrapping DB calling code in Tasks?

That depends. If by Task you mean "using a naturally asynchronous API exposed by the data provider", such as XXXAsync exposed by ADO.NET or Entity Framework, then it might benefit if you have concurrent requests hitting your database and you want to free your threads to process other requests in the meanwhile. It will most certainly not speed up your queries.

Will I end up slowing our system down by wrapping DB calls in Tasks?

Again, that depends. Same thing goes for your first question and what you mean by "Tasks". You have to remember that Task != Thread. If you want to wrap your database query with a call to Task.Run, you'll effectively be using the "async over sync" anti-pattern. It might end up also slowing your queries, you'll have to benchmark to be sure.

Are DB queries all executed in a synchronous fashion?

Database queries can be executed either synchronously or asynchronously, depending on the API exposed by the provider. Making database calls is naturally asynchronous, which means there is no need for any extra threads to be used for these queries in order to free the thread being used. If you have multiple queries which can be executed concurrently, then you might benefit from doing that using those asynchorouns API's.

Sign up to request clarification or add additional context in comments.

Comments

0

Task parallel library doesn't slow down the code , library introduce in .net for taking advantage of the multiple core processor exist today.

So if you firing two query to database both are not depend on each other, than you can create two task and make call to database that will execute your code parallel.

Default running task make use of threadpool if you are create long running task slow down things, As calling to database is might be long running task i suggest you make long running task by using TaskCreationOptions.LongRunning

Example code like :

 Task[] tasks = new Task[2];

task[0] = Task.Factory.StartNew(calltodatabase1
                      , TaskCreationOptions.LongRunning
                      , cancellationTokenSource.Token); 
task[1] = Task.Factory.StartNew(calltodatabase2
                      , TaskCreationOptions.LongRunning
                      , cancellationTokenSource.Token); 

Task.WaitAll(tasks);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.