Skip to content

O(N^2) performance when reading XML with SequentialAccess #1877

Open

Description

@yaakov-h

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

  1. On Windows, install .NET 7 and .NET Framework 4.8 or higher.
  2. Install SQL Server. I used SQL Server 2022 Developer Edition.
  3. Make sure that the local user has permissions to create and drop databases with Integrated Security (Windows Authentication).
  4. Clone and build https://github.com/yaakov-h/SqlXmlBenchmark
  5. Run the executable as follows:
    1. SqlBenchmarker.exe Microsoft 1
    2. SqlBenchmarker.exe Microsoft 2
    3. SqlBenchmarker.exe Microsoft 3
    4. SqlBenchmarker.exe Microsoft 4
    5. SqlBenchmarker.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).

Activity

  1. lcheunglci commented on Jan 4, 2023

    @lcheunglci
    Contributor

    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.

  2. Wraith2 commented on Jan 5, 2023

    @Wraith2
    Contributor

    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.

  3. yaakov-h commented on Jan 5, 2023

    @yaakov-h
    MemberAuthor

    We’ve been running the CONVERT to NVARCHAR(MAX) workaround since 2015 😄

  4. added
    Up-for-Grabs 🙌
    Performance 📈
    and removed on Jan 10, 2023
  5. added
    Repro Available ✔️
    on Jan 8, 2026
  6. moved this from To triage to Backlog in SqlClient Boardon Jan 8, 2026
  7. jimhblythe commented on Feb 19, 2026

    @jimhblythe

    I am requesting to work on this issue. Please assign it to me if approved.

    Proposed Changes

    Performance Validation

    • Add a new benchmark test comparing 1M vs. 5M reads to validate linear time growth.
    • To prevent flaky failures, the 5M execution time will be allowed up to 6× the 1M time (i.e., no more than 20% over the ideal 5× linear increase).

    Implementation Changes

    • Refactor XmlWriter to internally use a MemoryStream instead of a StringBuilder.
      • The MemoryStream will be reused per XML element, ensuring the internal buffer never exceeds the size of a single element.
      • This aligns with XmlReader behavior: 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.
      • Proper disposal logic will be included.

    Code Cleanup

    • Correct StringBuilder-based int usage where the interface uses long.
    • Eliminate repetitive property reads inside loops.
    • Remove redundant int casts in both SqlStream and SqlCachedStream (identified during formatting/cleanup).

    The primary changes are confined to the SqlStreamingXml class.

    Validation

    Quick validation using a modified SqlBenchmarker shows:

    • Linear performance scaling behavior.
    • Expected memory buffer bounded to largest element.
    • 50–60% of the execution time compared to the NVARCHAR(MAX) workaround.

    Please let me know if you’d like any adjustments to the proposed approach.

  8. moved this from Backlog to In progress in SqlClient Boardon Feb 20, 2026
  9. 3 remaining items

  10. removed this from the 7.1.0-preview2 milestone on Jun 16, 2026
  11. moved this from In review to Investigating in SqlClient Boardon Jun 16, 2026
  12. self-assigned this
    on Jun 16, 2026
  13. changed the issue type fromtoon Jun 16, 2026
  14. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  15. github-actions commented on Sep 17, 2026

    @github-actions

    Test comment

    Generated by SqlClient Issue Auto-Triage for #1877 · copilot · auto · 59.5 AIC · ⌖ 13 AIC · ⊞ 11.9K · ◷

  16. moved this from Investigating to Backlog in SqlClient Boardon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Performance 📈Repro Available ✔️

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions