Skip to content

Performance issues when selecting megabytes of text from nvarchar(max) column using ExecuteScalarAsync #689

@alser

Description

@alser

Describe the bug

Selecting ~20 MB of text data from nvarchar(max) column using ExecuteScalarAsync has very poor performance, executing up to 10 minutes with 100% CPU consumption (1 core) and lots of garbage collections.

It happens probably because of DataReader is creating multiple byte arrays for each portion of data received from SQL.

Selecting the text in SSMS occures in less than a second.

To reproduce

Sample project to reproduce the issue: https://github.com/alser/SqlDataClientStringPerformanceIssue

Its console app connecting to localhost to SQL Server (no network issues involved)

Expected behavior

Selecting data should occur in few seconds with low CPU consumptions and without lots of GCs.

Probably DataReader should rent byte arrays when reading string fields instead of creating new ones for each portion of data.

Further technical details

Microsoft.Data.SqlClient version: 2.0.0 (nuget)
.NET target: Core 3.1.7
SQL Server version: SQL Server 2016 (13.0.1742.0)
Operating system: Windows 10 version 10.0.19041.450

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions