runtime: SQL Reader Async 10 times slower than sync

Hi,

we got a problem with the async sql reader. We can reproduce that in ef core and with the sql data reader. Its so slow, that it’s not usable. I also tried 4.5.0-preview2-26225-02, but the speed tests give the same result. Example: Async Sql Client

using (var conn = new System.Data.SqlClient.SqlConnection(getConnectionstring(_config)))
                    {
                        using (var cmd = new System.Data.SqlClient.SqlCommand("SELECT TOP 350 * FROM users.Users;", conn))
                        {
                            cmd.CommandType = System.Data.CommandType.Text;

                            conn.Open();
                            var reader = await cmd.ExecuteReaderAsync();

                            while (await reader.ReadAsync())
                            {
                                Guid id = reader.GetGuid(0);
                                stringBuilder2.AppendLine(id.ToString());
                            }
                        }
                    }

Result: 218ms

Sync Sql Client

    using (var conn = new System.Data.SqlClient.SqlConnection(getConnectionstring(_config)))
                    {
                        using (var cmd = new System.Data.SqlClient.SqlCommand("SELECT TOP 350 * FROM users.Users;", conn))
                        {
                            cmd.CommandType = System.Data.CommandType.Text;
                            conn.Open();
                            var reader = cmd.ExecuteReader();

                            while (reader.Read())
                            {
                                Guid id = reader.GetGuid(0);
                                stringBuilder.AppendLine(id.ToString());
                            }
                        }
                    }

Result: 28 ms

I always tried 10 or more times to exclude warmup.

The same happens using ef:

Sync ef example

                    using (var db = new TestContext(_config))
                    {
                       
                        db.ConfigureLogging(s => stringLogger.AppendLine(stopwatch.ElapsedMilliseconds.ToString() + " " + s));
                        var blogs = db.Users.Take(350)
                            .ToList();

                    
                    }

                    break;`

Result: ef-client took 22ms 0 Opening connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’. 0 Opened connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’. 0 Executing DbCommand [Parameters=[@__p_0=‘?’], CommandType=‘Text’, CommandTimeout=‘30’] SELECT TOP(@__p_0) [u].[Id], [u].[FirstName], [u].[LastName] FROM [users].[Users] AS [u] 21 Executed DbCommand (21ms) [Parameters=[@__p_0=‘?’], CommandType=‘Text’, CommandTimeout=‘30’] SELECT TOP(@__p_0) [u].[Id], [u].[FirstName], [u].[LastName] FROM [users].[Users] AS [u] 22 A data reader was disposed. 22 Closing connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’. 22 Closed connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’.

Async ef example:

using (var db = new TestContext(_config))
                    {
                      db.ConfigureLogging(s => stringLogger.AppendLine(stopwatch.ElapsedMilliseconds.ToString() + " " + s));
                        var blogs = await db.Users.Take(350)
                            .ToListAsync();

                
                    }

Result: ef-client-async took 197ms 0 Opening connection to database ‘users-service’ on server ‘xx.cloudapi.de’. 0 Opened connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’. 0 Executing DbCommand [Parameters=[@__p_0=‘?’], CommandType=‘Text’, CommandTimeout=‘30’] SELECT TOP(@__p_0) [u].[Id], [u].[FirstName], [u].[LastName] FROM [users].[Users] AS [u] 125 Executed DbCommand (125ms) [Parameters=[@__p_0=‘?’], CommandType=‘Text’, CommandTimeout=‘30’] SELECT TOP(@__p_0) [u].[Id], [u].[FirstName], [u].[LastName] FROM [users].[Users] AS [u] 197 A data reader was disposed. 197 Closing connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’. 197 Closed connection to database ‘users-service’ on server ‘xx.database.cloudapi.de’.

I would expect the async code to be slower but not 5 to 10 times.

I tried all available versions (preview 1, 2 and normal version).

My tests are generated with vs code on mac and in our docker images, e.g dotnet-nightly:2.1.0-preview1-runtime-stretch

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 37 (22 by maintainers)

Most upvoted comments

With Azure Sql db, setting Encrypt=False doesn’t help. For Sql server and client protocol, the server has the last say in encryption. Even if the client says it doesn’t want to encrypt, and the server says that it wants encryption, encryption will be used. In case of Azure SQL DB, encryption is always forced by the server.