Skip to content

Reading JSON column appears to be 5x to 8x slower than reading VARCHAR(MAX) column #3499

Open

Description

@frankbuckley

Given two identical tables other than a column defined as either JSON or VARCHAR(MAX), reading the same data from the JSON column appears to be 5-6 times slower than from the VARCHAR column.

To reproduce

using System.Text.Json;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Data.SqlClient;

BenchmarkRunner.Run<SqlJsonTest>();

[MemoryDiagnoser]
public class SqlJsonTest
{
    private readonly SqlConnection _sqlConnection;

    public SqlJsonTest()
    {
        _sqlConnection = new SqlConnection("Server=(local);Database=SqlJsonBenchmark;Trusted_Connection=True;TrustServerCertificate=true");

        _sqlConnection.Open();

        string[] tables =
        [
            "ItemJson",
            "ItemVarchar"
        ];

        foreach (string table in tables)
        {
            string tagsColumn = table.EndsWith("Varchar")
                ? "VARCHAR(MAX) COLLATE Latin1_General_100_BIN2_UTF8 NOT NULL"
                : "JSON NOT NULL";

            var createTableCommand = _sqlConnection.CreateCommand();

            createTableCommand.CommandText = $"""
                IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.{table}') AND type in (N'U'))
                    DROP TABLE dbo.{table};

                CREATE TABLE {table} (
                    Id INT IDENTITY(1,1) PRIMARY KEY,
                    Title VARCHAR(200) NOT NULL,
                    strings {tagsColumn}
                );
                """;

            _ = createTableCommand.ExecuteNonQuery();
        }

        foreach (string table in tables)
        {
            for (int i = 0; i < 500; i++)
            {
                var insertCommand = _sqlConnection.CreateCommand();

                insertCommand.CommandText = $"""
                    INSERT INTO {table} (Title, strings)
                    VALUES (@Title, @strings);
                    """;

                _ = insertCommand.Parameters.AddWithValue("@Title", $"Entry {i}");

                string[] tags = [.. Enumerable.Range(1, 20).Select(j => new string($"tag{j}"))];

                _ = insertCommand.Parameters.AddWithValue("@strings", JsonSerializer.Serialize(tags));

                _ = insertCommand.ExecuteNonQuery();
            }
        }
    }

    [Benchmark]
    public Entry[] ReadEntriesJson()
    {
        var selectCommand = _sqlConnection.CreateCommand();

        selectCommand.CommandText = $"SELECT Id, Title, strings FROM ItemJson";

        using var reader = selectCommand.ExecuteReader();

        var entries = new List<Entry>();

        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string title = reader.GetString(1);
            var tagsJson = reader.GetSqlJson(2);
            string[] tags = JsonSerializer.Deserialize<string[]>(tagsJson.Value) ?? [];
            entries.Add(new Entry { Id = id, Title = title, Tags = tags });
        }

        return [.. entries];
    }

    [Benchmark(Baseline = true)]
    public Entry[] ReadEntriesVarchar()
    {
        var selectCommand = _sqlConnection.CreateCommand();

        selectCommand.CommandText = $"SELECT Id, Title, strings FROM ItemVarchar";

        using var reader = selectCommand.ExecuteReader();

        var entries = new List<Entry>();

        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string title = reader.GetString(1);
            string tagsJson = reader.GetString(2);
            string[] tags = JsonSerializer.Deserialize<string[]>(tagsJson) ?? [];
            entries.Add(new Entry { Id = id, Title = title, Tags = tags });
        }

        return [.. entries];
    }
}

public class Entry
{
    public int Id { get; set; }

    public required string Title { get; set; }

    public required string[] Tags { get; set; }
}

Actual behaviour

// * Summary *

BenchmarkDotNet v0.15.2, Windows 11 (10.0.26120.4741)
Unknown processor
.NET SDK 10.0.100-preview.6.25358.103
  [Host]     : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX2
  DefaultJob : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX2

| Method             | Mean       | Error    | StdDev   | Ratio | RatioSD | Gen0    | Gen1    | Allocated | Alloc Ratio |
|------------------- |-----------:|---------:|---------:|------:|--------:|--------:|--------:|----------:|------------:|
| ReadEntriesJson    | 2,516.4 us | 30.39 us | 31.21 us |  5.82 |    0.13 | 58.5938 | 39.0625 |   1.11 MB |        1.04 |
| ReadEntriesVarchar |   432.8 us |  7.77 us |  8.32 us |  1.00 |    0.03 | 59.0820 | 38.5742 |   1.07 MB |        1.00 |

Expected behaviour

Similar, if not better, performance for reading JSON.

Further technical details

Microsoft.Data.SqlClient version: 6.1.0-preview2.25178.5
.NET target: 10.0.100-preview.6.25358.103
SQL Server version: SQL Server 2025 (17.0.800.3)
Operating system: Windows 11 (10.0.26120.4741)

Discussion

Is this perhaps due to the cost of parsing JSON every time a SqlJson instance is created?

// Ask JsonDocument to parse it for validity, or throw.
//
// Note that we do not support trailing commas or comments in the
// JSON.
//
JsonDocument.Parse(jsonString).Dispose();

Activity

  1. added
    Performance 📈
    on Jul 22, 2025
  2. apoorvdeshmukh commented on Jul 22, 2025

    @apoorvdeshmukh
    Contributor

    Storage layout of varchar(max) and JSON column differs significantly at the server side. There may be some conversion happening from the native storage layout to the format expected on the TDS and that may be causing this performance issue. I need to check.
    I'll investigate this with the server team and update my findings here.
    JsonDocument.Parse doesn't seem to be causing this issue as I see similar numbers without it as well.

  3. frankbuckley commented on Jul 22, 2025

    @frankbuckley
    ContributorAuthor

    Thank you!

    JsonDocument.Parse doesn't seem to be causing this issue as I see similar numbers without it as well.

    I would imagine this might depend on the size of the JSON payload.

    Out of curiosity, why does SqlJson validate the JSON?

    If it is invalid, the server will generate an error. This would be consistent with what happens if you attempt to (say) set a varchar(10) column to something longer or if a column constraint is not satisfied. And, when reading, presumably you trust the server not to be returning invalid JSON.

  4. frankbuckley commented on Jul 22, 2025

    @frankbuckley
    ContributorAuthor

    A modest payload gives me a notable difference (279x) on this simple benchmark ---

    BenchmarkDotNet v0.15.2, Windows 11 (10.0.26120.4741)
    11th Gen Intel Core i7-11370H 3.30GHz, 1 CPU, 8 logical and 4 physical cores
    .NET SDK 10.0.100-preview.6.25358.103
      [Host]     : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
      DefaultJob : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
    
    
    | Method                    | Mean         | Error      | StdDev     | Ratio  | RatioSD | Gen0    | Gen1   | Allocated | Alloc Ratio |
    |-------------------------- |-------------:|-----------:|-----------:|-------:|--------:|--------:|-------:|----------:|------------:|
    | CreateSqlJsonWithParse    | 1,206.448 us | 21.2805 us | 19.9058 us | 279.25 |    5.72 | 15.6250 |      - | 101.73 KB |        3.24 |
    | CreateSqlJsonWithoutParse |     4.321 us |  0.0650 us |  0.0577 us |   1.00 |    0.02 |  5.1270 | 0.5646 |  31.42 KB |        1.00 |
    
    [MemoryDiagnoser]
    public class SqlJsonParseBenchmark
    {
        private const int N = 1000;
    
        private readonly string _json = """
        {
            "Id": 1002,
            "Title": "Sample Entry",
            "Tags": [ "tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tag10", "tag11", "tag12", "tag13", "tag14", "tag15", "tag16", "tag17", "tag18", "tag19", "tag20" ],
            "Related": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
        }
        """;
    
        [Benchmark]
        public SqlJsonWithParse[] CreateSqlJsonWithParse()
        {
            return [.. Enumerable.Range(0, N).Select(i => new SqlJsonWithParse(_json))];
        }
    
        [Benchmark(Baseline = true)]
        public SqlJsonWithoutParse[] CreateSqlJsonWithoutParse()
        {
            return [.. Enumerable.Range(0, N).Select(i => new SqlJsonWithoutParse(_json))];
        }
    
        public class SqlJsonWithParse
        {
            public SqlJsonWithParse(string json)
            {
                JsonDocument.Parse(json).Dispose();
                Json = json;
            }
    
            public string Json { get; }
        }
    
        public class SqlJsonWithoutParse
        {
            public SqlJsonWithoutParse(string json)
            {
                Json = json;
            }
    
            public string Json { get; }
        }
    }
  5. frankbuckley commented on Jul 22, 2025

    @frankbuckley
    ContributorAuthor

    Seems worse with large payload of floats (8.7x) ---

    BenchmarkDotNet v0.15.2, Windows 11 (10.0.26120.4741)
    11th Gen Intel Core i7-11370H 3.30GHz, 1 CPU, 8 logical and 4 physical cores
    .NET SDK 10.0.100-preview.6.25358.103
      [Host]     : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
      DefaultJob : .NET 10.0.0 (10.0.25.35903), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
    
    
    | Method             | Mean      | Error     | StdDev    | Median    | Ratio | RatioSD | Gen0      | Gen1     | Allocated | Alloc Ratio |
    |------------------- |----------:|----------:|----------:|----------:|------:|--------:|----------:|---------:|----------:|------------:|
    | ReadEntriesJson    | 89.976 ms | 1.4025 ms | 1.2433 ms | 89.692 ms | 48.64 |    3.89 | 1000.0000 | 800.0000 |    6.1 MB |        3.81 |
    | ReadEntriesVarchar | 10.351 ms | 0.0511 ms | 0.0399 ms | 10.354 ms |  5.60 |    0.44 |  890.6250 | 390.6250 |   5.38 MB |        3.37 |
    | ReadEntriesVector  |  1.862 ms | 0.0553 ms | 0.1587 ms |  1.812 ms |  1.01 |    0.12 |  265.6250 | 138.6719 |    1.6 MB |        1.00 |
    

    This also suggests the 50x read improvement claimed in the release notes may be more to do with the (not so good) performance of SqlJson than the performance of SqlVector (which appears to be ~5.6x faster than deserializing an array of 1024 floats from a JSON string)?

    Benchmark:

    using System.Text.Json;
    using BenchmarkDotNet.Attributes;
    using Microsoft.Data.SqlClient;
    using Microsoft.Data.SqlTypes;
    
    namespace SqlJsonBenchmark;
    
    [MemoryDiagnoser]
    public class ReadSqlJsonVectorBenchmark
    {
        private readonly SqlConnection _sqlConnection;
    
        public ReadSqlJsonVectorBenchmark()
        {
            _sqlConnection = new SqlConnection("Server=(local);Database=SqlJsonBenchmark;Trusted_Connection=True;TrustServerCertificate=true");
    
            _sqlConnection.Open();
    
            string[] tables =
            [
                "ItemJson",
                "ItemVarchar",
                "ItemVector"
            ];
    
            foreach (string table in tables)
            {
                string embeddingColumn = table.EndsWith("Varchar")
                    ? "VARCHAR(MAX) COLLATE Latin1_General_100_BIN2_UTF8 NOT NULL"
                    : table.EndsWith("Vector")
                        ? "VECTOR(1024) NOT NULL"
                        : "JSON NOT NULL";
    
                var createTableCommand = _sqlConnection.CreateCommand();
    
                createTableCommand.CommandText = $"""
                    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.{table}') AND type in (N'U'))
                        DROP TABLE dbo.{table};
    
                    CREATE TABLE {table} (
                        Id INT IDENTITY(1,1) PRIMARY KEY,
                        Title VARCHAR(200) NOT NULL,
                        Embedding {embeddingColumn}
                    );
                    """;
    
                _ = createTableCommand.ExecuteNonQuery();
            }
    
            foreach (string table in tables)
            {
                for (int i = 0; i < 100; i++)
                {
                    var insertCommand = _sqlConnection.CreateCommand();
    
                    insertCommand.CommandText = $"""
                        INSERT INTO {table} (Title, Embedding)
                        VALUES (@Title, @Embedding);
                        """;
    
                    _ = insertCommand.Parameters.AddWithValue("@Title", $"Entry {i}");
    
                    ReadOnlyMemory<float> embedding = Enumerable.Range(0, 1024).Select(i => Random.Shared.NextSingle()).ToArray();
    
                    _ = table.EndsWith("Vector")
                        ? insertCommand.Parameters.AddWithValue("@Embedding", new SqlVector<float>(embedding))
                        : insertCommand.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(embedding));
    
                    _ = insertCommand.ExecuteNonQuery();
                }
            }
        }
    
        [Benchmark]
        public Entry[] ReadEntriesJson()
        {
            var selectCommand = _sqlConnection.CreateCommand();
    
            selectCommand.CommandText = $"SELECT Id, Title, Embedding FROM ItemJson";
    
            using var reader = selectCommand.ExecuteReader();
    
            var entries = new List<Entry>();
    
            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string title = reader.GetString(1);
                var embeddingJson = reader.GetSqlJson(2);
                var embedding = JsonSerializer.Deserialize<ReadOnlyMemory<float>>(embeddingJson.Value);
                entries.Add(new Entry { Id = id, Title = title, Embedding = embedding });
            }
    
            return [.. entries];
        }
    
        [Benchmark]
        public Entry[] ReadEntriesVarchar()
        {
            var selectCommand = _sqlConnection.CreateCommand();
    
            selectCommand.CommandText = $"SELECT Id, Title, Embedding FROM ItemVarchar";
    
            using var reader = selectCommand.ExecuteReader();
    
            var entries = new List<Entry>();
    
            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string title = reader.GetString(1);
                string embeddingJson = reader.GetString(2);
                var embedding = JsonSerializer.Deserialize<ReadOnlyMemory<float>>(embeddingJson);
                entries.Add(new Entry { Id = id, Title = title, Embedding = embedding });
            }
    
            return [.. entries];
        }
    
        [Benchmark(Baseline = true)]
        public Entry[] ReadEntriesVector()
        {
            var selectCommand = _sqlConnection.CreateCommand();
    
            selectCommand.CommandText = $"SELECT Id, Title, Embedding FROM ItemVector";
    
            using var reader = selectCommand.ExecuteReader();
    
            var entries = new List<Entry>();
    
            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string title = reader.GetString(1);
                var sqlVector = reader.GetSqlVector<float>(2);
                entries.Add(new Entry { Id = id, Title = title, Embedding = sqlVector.Memory });
            }
    
            return [.. entries];
        }
    
        public class Entry
        {
            public int Id { get; set; }
    
            public required string Title { get; set; }
    
            public required ReadOnlyMemory<float> Embedding { get; set; }
        }
    }
  6. changed the title [-]Reading JSON column appears to be 5x to 6x slower than reading VARCHAR(MAX) column[/-] [+]Reading JSON column appears to be 5x to 8x slower than reading VARCHAR(MAX) column[/+] on Jul 22, 2025
  7. moved this from To triage to Backlog in SqlClient Boardon Nov 21, 2025
  8. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  9. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Json
    Duplicates None found
    Regression Not indicated

    Analysis

    The issue reports that reading JSON columns via SqlDataReader.GetSqlJson is 5–8x (and in some payload shapes, much more) slower and allocates more than reading equivalent VARCHAR(MAX) data. Follow-up investigation by a maintainer (@apoorvdeshmukh) points to server-side storage/TDS conversion differences, while the reporter's own micro-benchmarks show the SqlJson(string) constructor's JsonDocument.Parse(...).Dispose() validation call is extremely expensive (up to ~279x slower than skipping validation) and is likely the dominant cost on the client side for every value materialized through GetSqlJson. This also potentially undermines the JSON/SqlVector read performance claims in the v6.1.0-preview2 release notes. Given the measured magnitude and that it affects a documented public API path (SqlDataReader.GetSqlJson), this is a P1/P2 performance defect worth deeper investigation.

    Next Steps

    • Assign this issue to Copilot coding agent to investigate SqlJson's constructor validation path in src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlTypes/SqlJson.cs — specifically whether JsonDocument.Parse(jsonString).Dispose() can be replaced with a cheaper validation strategy (e.g., Utf8JsonReader-based structural validation without building a full JsonDocument, or deferring/skipping validation for values known to originate from the server, which is already assumed well-formed).
    • Investigate whether TdsParser/reader code paths performing extra buffering or encoding conversions for the JSON TDS type contribute additional overhead versus VARCHAR(MAX), as suggested by the maintainer's server-side storage layout comment.
    • Consider benchmarking with Utf8JsonReader.TryParse/JsonReaderOptions validation-only mode against the existing JsonDocument.Parse approach to quantify potential savings before implementing a fix.
    • Since no duplicate was found, this can proceed as the primary tracking issue for the SqlJson performance regression.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #3499 · copilot · auto · 61.1 AIC · ⌖ 13.2 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Performance 📈

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions