Skip to content

Improve multiple updates SQL #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;

namespace PlatformBenchmarks
{
internal class BatchUpdateString
{
private const int MaxBatch = 500;

public static DatabaseServer DatabaseServer;

internal static readonly string[] ParamNames = Enumerable.Range(0, MaxBatch * 2).Select(i => $"@p{i}").ToArray();

private static string[] _queries = new string[MaxBatch + 1];

public static string Query(int batchSize)
Expand All @@ -31,23 +26,15 @@ private static string CreateBatch(int batchSize)
Func<int, string> paramNameGenerator = i => "@p" + i;
#endif

sb.AppendLine("UPDATE world SET randomNumber = CASE id");
for (var i = 0; i < batchSize * 2;)
{
sb.AppendLine($"when {paramNameGenerator(++i)} then {paramNameGenerator(++i)}");
}
sb.AppendLine("else randomnumber");
sb.AppendLine("end");
sb.Append("where id in (");
for (var i = 1; i < batchSize * 2; i += 2)
sb.Append("UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES ");
var c = 1;
for (var i = 0; i < batchSize; i++)
{
sb.Append(paramNameGenerator(i));
if (i < batchSize * 2 - 1)
{
sb.AppendLine(", ");
}
if (i > 0)
sb.Append(", ");
sb.Append($"({paramNameGenerator(c++)}, {paramNameGenerator(c++)})");
}
sb.Append(")");
sb.Append(" ORDER BY 1) AS temp(id, randomNumber) WHERE temp.id = world.id");

return _queries[batchSize] = StringBuilderCache.GetStringAndRelease(sb);
}
Expand Down
2 changes: 0 additions & 2 deletions src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public static async Task Main(string[] args)
DateHeader.SyncDateTimer();

var host = BuildWebHost(args);
var config = (IConfiguration)host.Services.GetService(typeof(IConfiguration));
BatchUpdateString.DatabaseServer = config.Get<AppSettings>().Database;
#if DATABASE
await BenchmarkApplication.RawDb.PopulateCache();
#endif
Expand Down