Last active
May 20, 2024 07:56
-
-
Save PhilsKay/9fe43e9d948cb24dc4b47a31c526f101 to your computer and use it in GitHub Desktop.
EF core 8: Bulk Delete comparison, ExecuteDelete() Vs Remove+SaveChanges()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using BenchmarkDotNet.Attributes; | |
| using BenchmarkDotNet.Configs; | |
| using BenchmarkDotNet.Jobs; | |
| using BenchmarkDotNet.Running; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.Extensions.Options; | |
| namespace Reisty.TestConsole | |
| { | |
| public class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Create a manual configuration | |
| var config = ManualConfig.Create(DefaultConfig.Instance) | |
| .AddJob(Job.Default | |
| .WithWarmupCount(1) | |
| .WithIterationCount(40)); | |
| var result = BenchmarkRunner.Run<DeleteService>(config); | |
| //DeleteService deleteService = new DeleteService(); | |
| //deleteService.BulkDeleteBySaveChanges(); | |
| } | |
| } | |
| [MemoryDiagnoser] | |
| public class DeleteService | |
| { | |
| [Benchmark] | |
| public void BulkDeleteBySaveChanges() | |
| { | |
| DbContextOptionsBuilder<ReistyDbContext> optionsB = new DbContextOptionsBuilder<ReistyDbContext>().UseMySql(connectionString: "server=localhost;database=reisty;user=root;password=", serverVersion: ServerVersion.AutoDetect("server=localhost;database=reisty;user=root;password=")); | |
| ReistyDbContext context = new ReistyDbContext(optionsB.Options); | |
| IErrorLogs errorlogs = new ErrorlogService(context); | |
| errorlogs.BulkDeleteBySaveChanges(); | |
| } | |
| [Benchmark] | |
| public void BulkDeleteByExecuteDelete() | |
| { | |
| DbContextOptionsBuilder<ReistyDbContext> optionsB = new DbContextOptionsBuilder<ReistyDbContext>().UseMySql(connectionString: "server=localhost;database=reisty;user=root;password=", serverVersion: ServerVersion.AutoDetect("server=localhost;database=reisty;user=root;password=")); | |
| ReistyDbContext context = new ReistyDbContext(optionsB.Options); | |
| IErrorLogs errorlogs = new ErrorlogService(context); | |
| errorlogs.BulkDeleteByExecuteDelete(); | |
| } | |
| } | |
| public interface IErrorLogs | |
| { | |
| void BulkDeleteBySaveChanges(); | |
| void BulkDeleteByExecuteDelete(); | |
| ErrorLog Get(); | |
| } | |
| public class ErrorlogService(ReistyDbContext reistyDbContext) : IErrorLogs | |
| { | |
| public void BulkDeleteByExecuteDelete() | |
| { | |
| var delete = reistyDbContext.ErrorLogs.Take(8).ExecuteDelete(); | |
| } | |
| public void BulkDeleteBySaveChanges() | |
| { | |
| var errorlogs = reistyDbContext.ErrorLogs.Take(8); | |
| foreach (var errorlog in errorlogs) | |
| { | |
| reistyDbContext.ErrorLogs.Remove(errorlog); | |
| } | |
| reistyDbContext.SaveChanges(); | |
| } | |
| public ErrorLog Get() => reistyDbContext.ErrorLogs.FirstOrDefault(); | |
| } | |
| public class ReistyDbContext : DbContext | |
| { | |
| public ReistyDbContext(DbContextOptions<ReistyDbContext> options) : base(options) | |
| { | |
| } | |
| public virtual DbSet<ErrorLog> ErrorLogs { get; set; } | |
| protected override void OnModelCreating(ModelBuilder modelBuilder) | |
| { | |
| base.OnModelCreating(modelBuilder); | |
| } | |
| } | |
| public class ErrorLog | |
| { | |
| public Guid Id { get; set; } | |
| public string Message { get; set; } | |
| public string RequestUri { get; set; } | |
| public DateOnly DateLogged { get; set; } | |
| public TimeOnly TimeLogged { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment