Skip to content

Commit

Permalink
VCST-1752: Fix performance degradation on SQL Server (#2834)
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-dudarev authored Sep 12, 2024
1 parent c3adf25 commit e5df65b
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 68 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>NU5048</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildProjectName.Contains(Tests))' == false">
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;

namespace VirtoCommerce.Platform.Data.MySql.Extensions;

public static class DbContextOptionsBuilderExtensions
{
/// <summary>
/// Configures the context to use MySql.
/// </summary>
public static DbContextOptionsBuilder UseMySqlDatabase(
this DbContextOptionsBuilder optionsBuilder,
string? connectionString,
Type migrationsAssemblyMarkerType,
IConfiguration configuration,
Action<MySqlDbContextOptionsBuilder, IConfiguration>? mySqlOptionsAction = null)
{
return optionsBuilder.UseMySql(connectionString,
ServerVersion.AutoDetect(connectionString),
mySqlDbContextOptionsBuilder =>
{
mySqlDbContextOptionsBuilder.MigrationsAssembly(migrationsAssemblyMarkerType.Assembly.GetName().Name);
mySqlOptionsAction?.Invoke(mySqlDbContextOptionsBuilder, configuration);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace VirtoCommerce.Platform.Data.MySql;

public class MySqlDataAssemblyMarker
{
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using VirtoCommerce.Platform.Data.Repositories;
Expand All @@ -23,7 +18,7 @@ PlatformDbContext IDesignTimeDbContextFactory<PlatformDbContext>.CreateDbContext
connectionString,
ResolveServerVersion(serverVersion, connectionString),
db => db
.MigrationsAssembly(typeof(MySqlDbContextFactory).Assembly.GetName().Name));
.MigrationsAssembly(typeof(MySqlDataAssemblyMarker).Assembly.GetName().Name));

return new PlatformDbContext(builder.Options);
}
Expand All @@ -38,7 +33,7 @@ SecurityDbContext IDesignTimeDbContextFactory<SecurityDbContext>.CreateDbContext
connectionString,
ResolveServerVersion(serverVersion, connectionString),
db => db
.MigrationsAssembly(typeof(MySqlDbContextFactory).Assembly.GetName().Name));
.MigrationsAssembly(typeof(MySqlDataAssemblyMarker).Assembly.GetName().Name));

builder.UseOpenIddict();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;

namespace VirtoCommerce.Platform.Data.PostgreSql.Extensions;

public static class DbContextOptionsBuilderExtensions
{
/// <summary>
/// Configures the context to use PostgreSql.
/// </summary>
public static DbContextOptionsBuilder UsePostgreSqlDatabase(
this DbContextOptionsBuilder optionsBuilder,
string? connectionString,
Type migrationsAssemblyMarkerType,
IConfiguration configuration,
Action<NpgsqlDbContextOptionsBuilder, IConfiguration>? npgsqlOptionsAction = null)
{
return optionsBuilder.UseNpgsql(connectionString,
npgsqlDbContextOptionsBuilder =>
{
npgsqlDbContextOptionsBuilder.MigrationsAssembly(migrationsAssemblyMarkerType.Assembly.GetName().Name);
npgsqlOptionsAction?.Invoke(npgsqlDbContextOptionsBuilder, configuration);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace VirtoCommerce.Platform.Data.PostgreSql;

public class PostgreSqlDataAssemblyMarker
{
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using VirtoCommerce.Platform.Data.Repositories;
Expand All @@ -20,7 +15,7 @@ PlatformDbContext IDesignTimeDbContextFactory<PlatformDbContext>.CreateDbContext

builder.UseNpgsql(
connectionString,
db => db.MigrationsAssembly(typeof(PostgreSqlDbContextFactory).Assembly.GetName().Name));
db => db.MigrationsAssembly(typeof(PostgreSqlDataAssemblyMarker).Assembly.GetName().Name));

return new PlatformDbContext(builder.Options);
}
Expand All @@ -32,7 +27,7 @@ SecurityDbContext IDesignTimeDbContextFactory<SecurityDbContext>.CreateDbContext

builder.UseNpgsql(
connectionString,
db => db.MigrationsAssembly(typeof(PostgreSqlDbContextFactory).Assembly.GetName().Name));
db => db.MigrationsAssembly(typeof(PostgreSqlDataAssemblyMarker).Assembly.GetName().Name));

builder.UseOpenIddict();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;

namespace VirtoCommerce.Platform.Data.SqlServer.Extensions;

public static class DbContextOptionsBuilderExtensions
{
/// <summary>
/// Configures the context to use SqlServer.
/// </summary>
public static DbContextOptionsBuilder UseSqlServerDatabase(
this DbContextOptionsBuilder optionsBuilder,
string? connectionString,
Type migrationsAssemblyMarkerType,
IConfiguration configuration,
Action<SqlServerDbContextOptionsBuilder, IConfiguration>? sqlServerOptionsAction = null)
{
return optionsBuilder.UseSqlServer(connectionString,
sqlServerOptionsBuilder =>
{
var compatibilityLevel = configuration.GetValue<int?>("SqlServer:CompatibilityLevel", null);
if (compatibilityLevel != null)
{
sqlServerOptionsBuilder.UseCompatibilityLevel(compatibilityLevel.Value);
}

sqlServerOptionsBuilder.MigrationsAssembly(migrationsAssemblyMarkerType.Assembly.GetName().Name);
sqlServerOptionsAction?.Invoke(sqlServerOptionsBuilder, configuration);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace VirtoCommerce.Platform.Data.SqlServer;

public class SqlServerDataAssemblyMarker
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ PlatformDbContext IDesignTimeDbContextFactory<PlatformDbContext>.CreateDbContext

builder.UseSqlServer(
connectionString,
db => db.MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name));
db => db.MigrationsAssembly(typeof(SqlServerDataAssemblyMarker).Assembly.GetName().Name));

return new PlatformDbContext(builder.Options);
}
Expand All @@ -27,7 +27,7 @@ SecurityDbContext IDesignTimeDbContextFactory<SecurityDbContext>.CreateDbContext

builder.UseSqlServer(
connectionString,
db => db.MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name));
db => db.MigrationsAssembly(typeof(SqlServerDataAssemblyMarker).Assembly.GetName().Name));

builder.UseOpenIddict();

Expand Down
16 changes: 9 additions & 7 deletions src/VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.Platform.Data.Extensions;
using VirtoCommerce.Platform.Data.MySql;
using VirtoCommerce.Platform.Data.MySql.Extensions;
using VirtoCommerce.Platform.Data.MySql.HealthCheck;
using VirtoCommerce.Platform.Data.PostgreSql;
using VirtoCommerce.Platform.Data.PostgreSql.Extensions;
using VirtoCommerce.Platform.Data.PostgreSql.HealthCheck;
using VirtoCommerce.Platform.Data.Repositories;
using VirtoCommerce.Platform.Data.SqlServer;
using VirtoCommerce.Platform.Data.SqlServer.Extensions;
using VirtoCommerce.Platform.Data.SqlServer.HealthCheck;
using VirtoCommerce.Platform.DistributedLock;
using VirtoCommerce.Platform.Hangfire.Extensions;
Expand Down Expand Up @@ -133,19 +136,18 @@ public void ConfigureServices(IServiceCollection services)

services.AddDbContext<PlatformDbContext>((provider, options) =>
{
var databaseProvider = Configuration.GetValue("DatabaseProvider", "SqlServer");
var connectionString = Configuration.GetConnectionString("VirtoCommerce");

switch (databaseProvider)
{
case "MySql":
options.UseMySqlDatabase(connectionString);
options.UseMySqlDatabase(connectionString, typeof(MySqlDataAssemblyMarker), Configuration);
break;
case "PostgreSql":
options.UsePostgreSqlDatabase(connectionString);
options.UsePostgreSqlDatabase(connectionString, typeof(PostgreSqlDataAssemblyMarker), Configuration);
break;
default:
options.UseSqlServerDatabase(connectionString);
options.UseSqlServerDatabase(connectionString, typeof(SqlServerDataAssemblyMarker), Configuration);
break;
}
});
Expand Down Expand Up @@ -203,13 +205,13 @@ public void ConfigureServices(IServiceCollection services)
switch (databaseProvider)
{
case "MySql":
options.UseMySqlDatabase(connectionString);
options.UseMySqlDatabase(connectionString, typeof(MySqlDataAssemblyMarker), Configuration);
break;
case "PostgreSql":
options.UsePostgreSqlDatabase(connectionString);
options.UsePostgreSqlDatabase(connectionString, typeof(PostgreSqlDataAssemblyMarker), Configuration);
break;
default:
options.UseSqlServerDatabase(connectionString);
options.UseSqlServerDatabase(connectionString, typeof(SqlServerDataAssemblyMarker), Configuration);
break;
}

Expand Down
5 changes: 5 additions & 0 deletions src/VirtoCommerce.Platform.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"VirtoCommerce": "Data Source=(local);Initial Catalog=VirtoCommerce3.net8;Persist Security Info=True;User ID=virto;Password=virto;Connect Timeout=30;TrustServerCertificate=True;"
//"RedisConnectionString": "127.0.0.1:6379,ssl=False"
},
"SqlServer": {
// Set compatibility level to 120 (SQL Server 2014) to prevent the use of OPENJSON, which has poor performance.
// https://github.com/dotnet/efcore/issues/32394
// "CompatibilityLevel": 120
},
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
Expand Down

0 comments on commit e5df65b

Please sign in to comment.