Save Records in Database asynchronously Or Parallely in .net c#

15,975

Solution 1

I hope you are using .NET 4.5.

So let's get started. First mark your SearchItems function with async:

public async List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
{
}

Then you can await for saveToDb task:

var result = await Task.Factory.StartNew(()=> saveToDb(_list));

Solution 2

Asynchronsous or parallel execution of database commands will not speed-up bad database code, but it can hurt performance. 100 records is a very small number which means something weird is going on in the database access code. This won't be fixed if you execute multiple statements in parallel.

First, in order for operations to execute in parallel, you need a separate connection for each operation - which results in more locks, more blocking, more time spent going over the wire.

Second, much more time is spent in establishing connections and sending data over the wire than is spent executing an actual INSERT or UPDATE. Even if you execute one statement per record, you shouldn't have any performance problems. If you do, it's probably because saveToDb does something strange, or you don't have the proper indexes, or the statement performs really badly.

Finally, the best way to increase database inserts is to use bulk operations, like SQL Server's BULK INSERT to insert a large number of records in one go, or the SqlBulkCopy class. These operations are optimized to handle a large number of records (many thousands).

In any case, it pays much more to fix the statements and the database access code than to run the statements in parallel.

Solution 3

I would advise against using async-await unless the operation is really asynchronous. If your database client supports async operations then go ahead, but using Task.Factory.StartNew or Task.Run is just offloading the work to a different thread.

You could however run those tasks in parallel, but that will only be faster if your database works in parallel well. If let's say it's an SQL database and you're inserting data to the same table, that will probably lock the table and make your tasks run even slower.

If, though, all you want is to not block the caller and return the result before it was fully inserted to the database you could do this:

public List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
{
    var list = getRecords from Api
    Task.Run(() => SaveToDB(list));
    return list;
}
Share:
15,975
Gurmeet
Author by

Gurmeet

Updated on June 23, 2022

Comments

  • Gurmeet
    Gurmeet almost 2 years

    I have a service in which i have to save a large number of records to database after getting from an API. At the same time i have to return those records to from service to the caller. But problem is that i saving records in DB is taking long time so Service is getting slow. I have searched about this and found some conecept of Parallel task OR async & await.

    I am new to this concept and getting confused in its usage

    I looked into :

    Running multiple C# Task Async http://msdn.microsoft.com/en-us/library/hh191443.aspx

    But i am not getting what to do.Please help me:

    below is code:

    public List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
    {
        var _list= getRecords from Api //100 records    
    
         //Task<int>.Factory.StartNew(() => _objBLLNutritionLog.FillNutritionTable(_tempList)); // also tried this
    
        saveToDb(_list); // need to run this asynchronously Or parallel (Taking long time)
    
        return _list;
    
    }
    

    i want to return result to caller and on other side want to fill db. Please suggest.

    Thank you

  • rjv
    rjv over 7 years
    Is it advisable to use this to log information to DB in a web application? Will there be any advantage in performance/response times?
  • i3arnon
    i3arnon over 7 years
    @rjv I wouldn't advise to start tasks and not wait for them.