Inserting 10 Million Records in SQL Server with C# and ADO.NET (Efficient way)

  Рет қаралды 21,931

Programming with Felipe Gavilan

Programming with Felipe Gavilan

Күн бұрын

Пікірлер: 21
@gavilanch3803
@gavilanch3803 2 жыл бұрын
Get my Udemy courses with a discount: www.felipe-gavilan.com/cursos?idioma=eng Github: github.com/gavilanch/TenMillionRecords
@pavfrang
@pavfrang 2 жыл бұрын
Thank you for the great video! Memory consumption is truly great for this. Based on my experience, bulk copy operations with SqlBulkCopy are the fastest way to insert large chunks of data in the database. Unfortunately a DataTable is needed to be loaded, however, you can load table in batches via another thread - and the SqlBulkCopy is responsible for the fast sending of all the data batch with a single command to the database.
@gavilanch3803
@gavilanch3803 2 жыл бұрын
True, batches are a solution. Though, you need to implement transactions manually in that case, right?
@pavfrang
@pavfrang 2 жыл бұрын
@@gavilanch3803 no need for transactions. You just need an open SqlConnection (ok there is one internally, but you do not really need to set any transaction, because on failure the current operation is reverted automatically). In practice, depending on the record size, the batch size should be tuned/calibrated (after some tests) and then the optimum batch size really gives the best performance in terms of time.
@docdaven
@docdaven 2 жыл бұрын
Try this helper function: private static DataTable ToDataTable(List items) { DataTable dataTable = new(typeof(T).Name); //Get all the properties PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) { //Defining type of data column gives proper data table var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType); //Setting column names as Property names dataTable.Columns.Add(prop.Name, type); } foreach (T item in items) { var values = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { //inserting property values to datatable rows values[i] = Props[i].GetValue(item, null); } dataTable.Rows.Add(values); } return dataTable; }
@Tamer_Ali
@Tamer_Ali 2 жыл бұрын
Thanks Mr.Felipe for this video tutorial. Keep it up 👍
@parmarjitesh1979
@parmarjitesh1979 2 жыл бұрын
Superb video as always. Keep it up.
@marcrippee4365
@marcrippee4365 Жыл бұрын
Brilliant thank you
@DucaTech
@DucaTech Жыл бұрын
How does this compare to SQL Bulk Copy?
@hqcart1
@hqcart1 Жыл бұрын
bulk copy is the fastest.
@amolsawant9048
@amolsawant9048 7 күн бұрын
Can I get the DB Stored Procedure Script?
@lifeinloop
@lifeinloop 2 жыл бұрын
I just didn't understood how "yeld return " returns all the records one by one, being that, that method "GetData" was only called once..
@hqcart1
@hqcart1 Жыл бұрын
This video does not represent real life applications. 1. how big the records are. 2. how many indexes you have. 3. any outside relations. any real application will have at least one of the above. there is also another factor is how big is your batch?
@Thalapraga
@Thalapraga Жыл бұрын
Where can i get this code
@EngineerASO
@EngineerASO 2 жыл бұрын
dear sir Is there another efficient way to insert multi-rows from Access Database to Sql Server, I tried using pass-through, doen't work when I used a form and try to select all records from its table and inserted to sql Server table... thank for your reply
@rmcgraw7943
@rmcgraw7943 3 ай бұрын
If you are converting from Access to SQL Server, MS has a tool for that; however, you have to rewrite all your queries. The best way is to export the Access to text, then import it into SQL, but be aware that all your Access queries will alias the SQL tables as aliased tables inside of Access, and that will result in a table scan, so you’ll need to rewrite all those to be SQL Server Stored Procedures, which you CAN then call as pass-thru queries.
@DoctorMGL
@DoctorMGL Жыл бұрын
i have 800 million line in a text file and each line is about 100 character using the same method you have (streamReader) with async to read the file line by line and compare it to a text i have on a string, something like ( SearchText == TextFromFile ). and it takes 1 second to read every 50.000 line so 800 million line at this speed will take me ( 4 hours ) to finish . any other way to speed it more ? because 4 hours is to long ... im just trying to find if the "text" i have matches the once in the text file , and its a 35 Gb file with 800 million line
@hqcart1
@hqcart1 Жыл бұрын
maybe you are limited by I/O speed?
@longuinni
@longuinni 2 жыл бұрын
is bulk insert faster?
@davestorm6718
@davestorm6718 2 жыл бұрын
Much faster, though you have to load the dt in memory first (as far as I know).
@HajvazS
@HajvazS 2 жыл бұрын
Hi, thank you for you videos :) they are great, is it possible to contact you, would like to offer you something, but on your website i cant find any contact details, best regards
Tracking Data Changes in C# .NET
20:42
Hassan Habib
Рет қаралды 50 М.
C# Data Access: Complex Objects with Dapper
28:09
IAmTimCorey
Рет қаралды 31 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 21 МЛН
Extremely easy way to bulk insert data into SQL Server using SqlBulkCopy class
11:33
The weirdest way to loop in C# is also the fastest
12:55
Nick Chapsas
Рет қаралды 254 М.
Querying 100 Billion Rows using SQL, 7 TB in a single table
9:07
Arpit Agrawal (Elastiq.AI)
Рет қаралды 52 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 260 М.
Stored Procedures with Entity Framework Core in .NET 6 🚀
13:51
Getting Started with Dapper in .NET
11:29
Amichai Mantinband
Рет қаралды 14 М.
Update a Table with Millions of Rows in SQL (Fast)
7:01
Database Star
Рет қаралды 10 М.
How to connect to a SQL Server Database in C# (using ADO.NET data providers)
11:02
Players push long pins through a cardboard box attempting to pop the balloon!
00:31