Logo

dev-resources.site

for different kinds of informations.

Efficient Bulk Operations with UkrGuru.Sql

Published at
1/3/2025
Categories
dotnet
dotnetcore
sqlserver
csharp
Author
ukrguru
Author
7 person written this
ukrguru
open
Efficient Bulk Operations with UkrGuru.Sql

Efficient Bulk Operations with UkrGuru.Sql

In this article, we explore how to perform efficient bulk operations—insert, update, and delete—using the UkrGuru.Sql library. This approach is particularly useful when dealing with large datasets, ensuring optimal performance and minimal execution time.

Initial Setup

First, we initialize the database connection:

Utils.InitDb();
Enter fullscreen mode Exit fullscreen mode

We then define the number of records (N) and create a list to hold our student data:

int N = 100 * 1024; 
List<Student>? students = new();
DateTime started = DateTime.Now;
Enter fullscreen mode Exit fullscreen mode

Mass Insert

To insert a large number of records efficiently, we prepare the data and use a bulk insert operation:

for (int i = 0; i < N; i++)
{
    students.Add(new Student() { ID = i, Name = $"Name_{i}" });
}

var sql_insert = """
    INSERT Students (ID, Name) 
    SELECT D.ID, D.Name FROM OPENJSON(@Data) WITH (ID int, Name varchar(50)) D
    """;

started = DateTime.Now;

await DbHelper.ExecAsync(sql_insert, students.ToJson());

Console.WriteLine($"Inserted {N / 1024}K - {DateTime.Now.Subtract(started)}");
Enter fullscreen mode Exit fullscreen mode

This method leverages OPENJSON to parse the JSON data and insert it into the Students table. The result of this operation was:

Database created successfully.
Inserted 100K - 00:00:00.4206026
Enter fullscreen mode Exit fullscreen mode

Mass Update

Next, we update the records with new class and grade information:

for (int i = 0; i < N; i++)
{
    students[i].Class = (char)((byte)'A' + i % 25);
    students[i].Grade = (byte)(i % 5);
}

var sql_update = """
    UPDATE S
    SET S.Class = D.Class, S.Grade = D.Grade
    FROM Students S
    JOIN OPENJSON(@Data) WITH (ID int, Class char(1), Grade tinyint) D ON S.ID = D.ID;
    """;

started = DateTime.Now;

await DbHelper.ExecAsync(sql_update,
    students.Select(c => new { c.ID, c.Class, c.Grade }).ToJson());

Console.WriteLine($"Updated {N / 1024}K - {DateTime.Now.Subtract(started)}");
Enter fullscreen mode Exit fullscreen mode

This update operation uses a similar approach, parsing the JSON data and updating the corresponding records in the Students table. The result of this operation was:

Updated 100K - 00:00:00.3418457
Enter fullscreen mode Exit fullscreen mode

Mass Delete

Finally, we delete records based on a specific condition (e.g., Grade < 1):

var sql_delete = """
    DELETE S 
    FROM Students S 
    JOIN OPENJSON(@Data) AS D ON S.ID = D.value;
    """;

started = DateTime.Now;

await DbHelper.ExecAsync(sql_delete,
    students.Where(x => x.Grade < 1).Select(c => c.ID ).ToJson());

Console.WriteLine($"Deleted {(N / 5) / 1024}K - {DateTime.Now.Subtract(started)}");
Enter fullscreen mode Exit fullscreen mode

This delete operation ensures that only the records meeting the specified condition are removed from the Students table. The result of this operation was:

Deleted 20K - 00:00:00.0400413
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using UkrGuru.Sql for bulk operations significantly improves performance when handling large datasets. By leveraging JSON parsing and efficient SQL commands, we can perform mass inserts, updates, and deletes with minimal execution time. The results demonstrate the efficiency of this approach:

  • Database created successfully.
  • Inserted 100K - 00:00:00.4206026
  • Updated 100K - 00:00:00.3418457
  • Deleted 20K - 00:00:00.0400413

This method ensures that large-scale data operations are handled swiftly and effectively, making it an excellent choice for high-performance applications.

For more details, you can check the source code here.
https://github.com/UkrGuru/Sql/blob/main/demos/Mass_Ins_Upd_Del/Program.cs

UkrGuru.Sql package here
https://www.nuget.org/packages/UkrGuru.Sql/

dotnetcore Article's
30 articles in total
Favicon
.Net tarixi
Favicon
Oh bless me, Father, I have done something unholy: Installing .NET Core on Apple Silicon
Favicon
How to use Scoped service from Singleton Service in .Net Core
Favicon
How to add a Custom fields to Header in .NET Core Web API ?
Favicon
c#(.Net) - Basic Authentication WEB API
Favicon
CRUD operations on Arrays
Favicon
Working with interfaces
Favicon
Iterations
Favicon
Protfolio Website
Favicon
Dependency injection validation error in ASP.NET Core projects
Favicon
.Net Core and Kafka
Favicon
C# Null-Conditional (?.) & Null-Coalescing (??) Operators Explained
Favicon
Change a .Net Console application into an web application
Favicon
Efficient Bulk Operations with UkrGuru.Sql
Favicon
Improve Application Performance using “Fire and Forget” in .NET Core
Favicon
API Versioning in .Net Core.
Favicon
Move objects from one folder to other in the same S3 Bucket using C# in AWS
Favicon
🎉 We Made It: Trending in .NET on Dev.to! 🚀
Favicon
.NET 9 Improvements for ASP.NET Core: Open API, Performance, and Tooling
Favicon
.Net Core Microservice Communication Using Kafka.
Favicon
Getting Started with .NET and Docker Tutorial
Favicon
Experimental attribute in C# is a powerful tool : Let's explore
Favicon
Implementing Chain of Responsibility Pattern in C# : Middleware's Design Pattern
Favicon
How to create a background email sender with outbox pattern integration
Favicon
The End of Microsoft's Monopoly on ASP.NET
Favicon
.NET Core MVC Project Structure : Implementing a Generic Service and Repository Pattern
Favicon
Did you know? How .NET Achieving Language Interoperability (C# + VB.NET = Same Application)
Favicon
These 10+ comparisons cover entire SQL concepts, Is it?
Favicon
NET 9 BinaryFormatter migration paths
Favicon
How to create a background email sender

Featured ones: