Repository navigation
Description
Describe the bug
When reading from an XML column with CommandBehaviour.SequentialAccess, read times of a value balloon to O(N^2).
I first observed this bug about 7.5 years ago on .NET Framework and wrote a blog post at https://codingrange.com/blog/performance-optimization-or-why-does-my-sql-query-take-an-hour-to-execute
I recently re-tested this on both .NET Framework and .NET 7, as well as on both System.Data.SqlClient and Microsoft.Data.SqlClient. This behaviour is present in all four combinations.
To reproduce
- On Windows, install .NET 7 and .NET Framework 4.8 or higher.
- Install SQL Server. I used SQL Server 2022 Developer Edition.
- Make sure that the local user has permissions to create and drop databases with Integrated Security (Windows Authentication).
- Clone and build https://github.com/yaakov-h/SqlXmlBenchmark
- Run the executable as follows:
SqlBenchmarker.exe Microsoft 1SqlBenchmarker.exe Microsoft 2SqlBenchmarker.exe Microsoft 3SqlBenchmarker.exe Microsoft 4SqlBenchmarker.exe Microsoft 5
Note that the insertion times grow fairly linearly, but the read times grow exponentially.
Expected behavior
Read times grow linearly with a linear increase in data size.
Further technical details
Microsoft.Data.SqlClient version: v5.0.1
.NET target: .NET Framework (multiple versions), .NET 7.0.1 (x64, 97203d38ba), probably others
SQL Server version: SQL Server 2014, 2022, probably others
Operating system: Windows 10, Windows Server 2022, probably others.
Additional context
If the SQL SELECT statement is altered from:
SELECT [{0}] FROM [{1}] WHERE [{2}] = @pkvalue;
to:
SELECT CONVERT(NVARCHAR(max), [{0}]) FROM [{1}] WHERE [{2}] = @pkvalue;
then the application will demonstrate the expected linear increase in performance.
This bug was filed on Microsoft Connect (RIP) in 2015, but seems to have been lost to the sands of time.
A performance trace with dotTrace suggests that SqlStreamingXml.GetChars is the function to blame here, spending a large amount of time in StringBuilder.get_Chars(int) and StringBuilder.Remove(int, int).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
lcheunglci commentedon Jan 4, 2023
Hi @yaakov-h , thanks for bringing this to our attention and thanks for providing the repro sample. We'll take a look at it after our release.
Wraith2 commentedon Jan 5, 2023
This is an interesting one. The reason that the problem exists for xml and not strings is that the xml route goes through a dedicated reader type,
SqlStreamingXml. The problem is that in GetChars there is a bunch of code that writes to a stringbuilder and then removes the chars it has just written to keep the memory used by the builder low. This isn't a good use of a string builder and that's what is consuming the time.How important is fixing this to you? It's certainly something that it would be nice to change but it's a quite niche use-case and the obvious workaround of using a string and wrapping it in your own xml reader seems reasonable.
yaakov-h commentedon Jan 5, 2023
We’ve been running the CONVERT to NVARCHAR(MAX) workaround since 2015 😄
jimhblythe commentedon Feb 19, 2026
I am requesting to work on this issue. Please assign it to me if approved.
Proposed Changes
Performance Validation
Implementation Changes
XmlWriterto internally use aMemoryStreaminstead of aStringBuilder.MemoryStreamwill be reused per XML element, ensuring the internal buffer never exceeds the size of a single element.XmlReaderbehavior: a 1M node uses a 1M buffer; a 5M node uses a 5M buffer. Reading five 1M nodes within a single XML will still only require a 1M buffer.Code Cleanup
intusage where the interface useslong.intcasts in bothSqlStreamandSqlCachedStream(identified during formatting/cleanup).The primary changes are confined to the
SqlStreamingXmlclass.Validation
Quick validation using a modified
SqlBenchmarkershows:NVARCHAR(MAX)workaround.Please let me know if you’d like any adjustments to the proposed approach.
Modify SqlStreamingXml XmlWriter to internally use a MemoryStream ins…
3 remaining items
Add non-ASCII XML streaming tests for SqlDataReader.GetChars
priyankatiwari08 commentedon Sep 17, 2026
/triage
github-actions commentedon Sep 17, 2026
Test comment