Repository navigation
Description
Describe the bug
We have an issue with an Azure Function that runs a query against a Azure SQL Server. The function uses a SqlDataReader, and the command / query execution is pretty fast, but reading the results is sometimes slow. It does not happen very often, but when it happens, there is a certain pattern, the TDS packets seem to be delayed. For example, this is a log snippet:
8/6/2021, 8:46:55.128 AM 20288 - ScopeEnter - SqlDataReader.ReadAsync | API | Object Id 1
8/6/2021, 8:46:55.128 AM 20288 - ScopeEnter - SqlDataReader.TryReadInternal | API | Object Id 1
8/6/2021, 8:46:55.128 AM 20288 - ScopeLeave - Exit Scope 214540
8/6/2021, 8:46:55.128 AM 20288 - ScopeLeave - Exit Scope 214539
8/6/2021, 8:46:55.128 AM 20288 - ScopeEnter - SqlDataReader.ReadAsync | API | Object Id 1
8/6/2021, 8:46:55.128 AM 20288 - ScopeEnter - SqlDataReader.TryReadInternal | API | Object Id 1
8/6/2021, 8:46:55.128 AM 20288 - ScopeLeave - Exit Scope 214542
8/6/2021, 8:46:55.129 AM 20288 - ScopeLeave - Exit Scope 214541
8/6/2021, 8:46:55.374 AM 20288 - SNITrace - SNITCPHandle.Receive | INFO | Connection Id e8b7430d-7f47-410e-83dc-f7bccbe47f49, Data read from stream synchronously
8/6/2021, 8:46:55.375 AM 20288 - Trace - TdsParserStateObjectManaged.ReadSyncOverAsync | Info | State Object Id 4, Session Id e8b7430d-7f47-410e-83dc-f7bccbe47f49
8/6/2021, 8:46:55.375 AM 20288 - Trace - TdsParserStateObjectManaged.ReleasePacket | Info | State Object Id 4, Session Id e8b7430d-7f47-410e-83dc-f7bccbe47f49, Packet DataLeft 8000
8/6/2021, 8:46:55.375 AM 20288 - ScopeEnter - SqlDataReader.ReadAsync | API | Object Id 1
8/6/2021, 8:46:55.375 AM 20288 - ScopeEnter - SqlDataReader.TryReadInternal | API | Object Id 1
8/6/2021, 8:46:55.375 AM 20288 - ScopeLeave - Exit Scope 214544
8/6/2021, 8:46:55.375 AM 20288 - ScopeLeave - Exit Scope 214543
8/6/2021, 8:46:55.375 AM 20288 - ScopeEnter - SqlDataReader.ReadAsync | API | Object Id 1
8/6/2021, 8:46:55.375 AM 20288 - ScopeEnter - SqlDataReader.TryReadInternal | API | Object Id 1
If I understand the log correctly, there is a ~250ms delay when receiving the TDS packet, and, for this particular query, there are 100 packets which adds up to 20-30 seconds.
This seems to happen sporadically without any clear cause. Also, the SQL server isn't busy at all at this time (<5% CPU and IO usage).
Further technical details
Microsoft.Data.SqlClient version: 3.0.0
.NET target: Core 3.1
SQL Server version: Azure SQL Server
Operating system: Azure Function v3 on Windows
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
JRahnama commentedon Aug 6, 2021
@valentiniliescu Hi,
Logs are not saying much in this case, What I understood from the log:
1- you are using async calls
2- you are using mangedSNI
Is it possible to make a repro including the connection string (including the options you are using) and the query that you run?
Do yo have any concurrent/parallel runs on the app?
valentiniliescu commentedon Aug 6, 2021
Unfortunately, I cannot create a repro, but this is the sanitized version of the code that we run:
I used the managed SNI only for tracing purposes, otherwise I don't see SNI traces in the logs.
Also, this is an Azure Function, so there might be concurrent runs, but, as you can see from the code snippet above, there are no shared objects.
Wraith2 commentedon Aug 7, 2021
Are you reading large strings or binary when the problem occurs?
valentiniliescu commentedon Aug 7, 2021
No, the strings are pretty small. For example, for this particular query, the result is 12000 rows, with one column NVARCHAR and each value 25-30 characters and one DECIMAL(6,5)
JRahnama commentedon Aug 10, 2021
@valentiniliescu I made a Benchmark testing for this matter. The sample code could be found in this gist.
I have tested different scenarios.
Synccall with noCommandBehaviorSynccall withCommandBehavior.SequentialAccessAsynccall with noCommandBehaviorAsynccall with noCommandBehavior.SequentialAccessAsynccall withCommandBehavior.SequentialAccessand noConfigureAwait(false)Asynccall with noCommandBehaviorand noConfigureAwait(false)The results are as below:
// * Summary *
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19043.1151 (21H1/May2021Update)
Intel Core i7-8665U CPU 1.90GHz (Coffee Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.4.21255.9
[Host] : .NET Core 3.1.17 (CoreCLR 4.700.21.31506, CoreFX 4.700.21.31502), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.17 (CoreCLR 4.700.21.31506, CoreFX 4.700.21.31502), X64 RyuJIT
Job=.NET Core 3.1 Runtime=.NET Core 3.1
It seems reading with Sequential access is causing the delay. Can you test it please?
For those who want to follow:
Run the following script on SSMS and make a database with 12000 rows
to create Stored procedure:
Can you also confirm that the stored procedure is something similar to what you have?
valentiniliescu commentedon Aug 10, 2021
Thank you @JRahnama for your code and benchmark.
For the stored procedure, mine is pretty complicated, but the shape and count of the result is pretty similar.
Unfortunately, I tested without sequential access before and it does not make any difference. I tested with sync and async, tested with the newest package (Microsoft.Data.SqlClient 3.0.0) and the original one (System.Data.SqlClient 4.6.0) with no luck.
Regarding the benchmark, I would be interested in the outliers more than the mean. In my case, most of the time the code is fast, but sometimes it gets slow to the point the Azure Function times out (after 5 minutes) and it's not the query execution (takes only 10 seconds).
At this point I am going to try a workaround - to reduce the size of the results from the stored procedure, so the random slowness in the TDS packets will not influence that much.
Wraith2 commentedon Oct 10, 2021
I've done some investigation. The difference in speed between sequential and non-sequential reads is caused by the extra overhead of using an
SqlSequentialTextReaderthat can handle forward only uncached reads from the tds data stream. In the default case the entire field can be read as a stream and a standard StringReader returned.For the case where individual reads are extremely slow there isn't much that can be done without a reproduction.
MichelZ commentedon Nov 8, 2024
Related to #593
cheenamalhotra commentedon Jan 8, 2026
Closing this as duplicate of #593, please follow the same for further updates on the same.