From b0d8bfae9e040287fca36cbd3ed550c236792d36 Mon Sep 17 00:00:00 2001 From: Oleg Zhuk Date: Fri, 6 Sep 2024 08:55:33 +0200 Subject: [PATCH] VCST-1752: Performance Degradation in MS SQL Database fix: Addressed performance degradation in MS SQL Database when using OPENJSON and CONTAINS. The issue was resolved by setting the CompatibilityLevel to 120. For more details, refer to this link: https://www.virtocommerce.org/t/performance-degradation-in-ms-sql-database-queries-after-upgrading-to-ef-core-8/731. feat: Added connfiguration in appsettings.json SqlServer:CompatibilityLevel fto set up the compatibility level for SQL Server. The default value is set to 120. ```json "SqlServer": { "CompatibilityLevel": 120 } ``` --- .../DbContextOptionsBuilderExtensions.cs | 5 +++-- src/VirtoCommerce.OrdersModule.Web/Module.cs | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/VirtoCommerce.OrdersModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs b/src/VirtoCommerce.OrdersModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs index b8f7c62f..77be77e9 100644 --- a/src/VirtoCommerce.OrdersModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs +++ b/src/VirtoCommerce.OrdersModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs @@ -7,10 +7,11 @@ public static class DbContextOptionsBuilderExtensions /// /// Configures the context to use SqlServer. /// - public static DbContextOptionsBuilder UseSqlServerDatabase(this DbContextOptionsBuilder builder, string connectionString) + public static DbContextOptionsBuilder UseSqlServerDatabase(this DbContextOptionsBuilder builder, string connectionString, int compatibilityLevel) { return builder.UseSqlServer(connectionString, db => db - .MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name)); + .MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name) + .UseCompatibilityLevel(compatibilityLevel)); } } } diff --git a/src/VirtoCommerce.OrdersModule.Web/Module.cs b/src/VirtoCommerce.OrdersModule.Web/Module.cs index 4f71f969..267feb1a 100644 --- a/src/VirtoCommerce.OrdersModule.Web/Module.cs +++ b/src/VirtoCommerce.OrdersModule.Web/Module.cs @@ -70,7 +70,8 @@ public void Initialize(IServiceCollection serviceCollection) options.UsePostgreSqlDatabase(connectionString); break; default: - options.UseSqlServerDatabase(connectionString); + var compatibilityLevel = Configuration.GetValue("SqlServer:CompatibilityLevel", 120); + options.UseSqlServerDatabase(connectionString, compatibilityLevel); break; } });