Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why asynchronous code is so much slower than synchronous code

I create a test table in SQL Server like this:

CREATE TABLE Person
(
    PersonID INT NOT NULL IDENTITY,
    PersonName NCHAR(500) NOT NULL,
    PersonFamily NCHAR(500) NOT NULL,
 
    CONSTRAINT PK_Person PRIMARY KEY(PersonID)
)

Then I create two methods to test async.

The first method uses an async approach to read all person, and the second one uses a sync approach.

Async method:

private async Task<List<Person>> ReadPersonsAsync()
{
    List<Person> result = new List<Person>();

    using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
    {
        using (SqlCommand sqlCommand = new SqlCommand())
        {
            sqlCommand.CommandText = "SELECT * FROM Person";
            sqlCommand.CommandType = System.Data.CommandType.Text;
            sqlCommand.Connection = sqlConnection;

            await sqlConnection.OpenAsync();

            using (var reader = await sqlCommand.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    result.Add(new Person
                    {
                        ID = int.Parse(reader[0].ToString()),
                        Name = reader[1].ToString(),
                        Family = reader[2].ToString()
                    });
                }
            }
        }
    }

    return result;
}

Sync method:

private List<Person> ReadPersons()
{
    List<Person> result = new List<Person>();

    using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
    {
        using (SqlCommand sqlCommand = new SqlCommand())
        {
            sqlCommand.CommandText = "SELECT * FROM Person";
            sqlCommand.CommandType = System.Data.CommandType.Text;
            sqlCommand.Connection = sqlConnection;

            sqlConnection.Open();

            using (var reader = sqlCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    result.Add(new Person
                    {
                        ID = int.Parse(reader[0].ToString()),
                        Name = reader[1].ToString(),
                        Family = reader[2].ToString()
                    });
                }
            }
        }
    }

    return result;
}

When I use the async approach it takes 10 seconds, but the sync approach is run in 2 seconds.

Which part of my code is wrong? Thanks.

like image 903
Reza 110 Avatar asked Sep 29 '26 16:09

Reza 110


1 Answers

SqlClient implementation, today, is sync first. The async implementation of it has a lot of workarounds to make it work, some of them even work synchronously under the hood, so, the problem is the SqlClient design and the weight of its legacy code.

Recently, they started a project, SqlClientX to address this problem heavily. You can take a look here and follow the evolution of it. https://github.com/dotnet/SqlClient/discussions/2603#discussioncomment-9868402

like image 155
Thiago Oliveira Santos Avatar answered Oct 01 '26 06:10

Thiago Oliveira Santos