What Is the Fastest Way To Do a Bulk Insert? Let’s Find Out

  Рет қаралды 12,291

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 68
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@ksmemon1
@ksmemon1 4 ай бұрын
Subscribed 👍
@alexanderst.7993
@alexanderst.7993 4 ай бұрын
Milan just wanted to say, thanks to you and your C# videos, i managed to land a job. Appreciate ya pal ;)
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Nah, that's all on you buddy. Great work 💪
@たろ羊
@たろ羊 4 ай бұрын
Thank you for the video. I'd like to see a video on bulk upserts/merging data
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Great suggestion!
@oneshot2579
@oneshot2579 4 ай бұрын
Especially for postresql
@anonymoos
@anonymoos 4 ай бұрын
I like using the BulkCopy function, it is amazingly fast for importing large datasets. One thing to note is that the column names specified for the database side are case sensitive. If there's a mismatch in case on column names, the import will fail. You can also eek out even more performance by tweaking the batch size in BulkCopy using `bulk.BatchSize = 10_000;`. Actual performance will vary based on how many columns you're inserting.
@lolyasuo1235
@lolyasuo1235 4 ай бұрын
What about primary keys? can you import data on table with incremental PK or you have to specify the PK?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
That's an excellent suggestion. Will try it with different batch sizes.
@pilotboba
@pilotboba 4 ай бұрын
@@lolyasuo1235 SQL Server will generate identity values just like it does on a normal insert. There is an option though to "keep identity" so if you did have PK values you wanted retained you could use that option. Of course, if those values exist in the table then you will get duplicate insert errors. I believe it will update the next identity value on the table after it is done if you use that option.
@FolkoFess
@FolkoFess 4 ай бұрын
There is another way to speed up bulk insert for some specific cases (for example during periodic ETL process when the entire table or partition should be fully cleaned and recreated from scratch using bulk insert). In this case bulk insert can be slowed down by indexes, constraints, concurrent access , etc . The solution for this would be to do bulk insert into temporary table that does not have any indexes or constraints using ReadUncommited transaction -> after build all needed indexes -> after do partition swap operation with the main table/partition. Another advantage of this approach is that up until the last step - data from original table stays fully available . And partition swap is almost instant and atomic operation
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Ah, great addition to this topic 👌
@vasiliylu8054
@vasiliylu8054 4 ай бұрын
Thank you, Milan! This video must be in top.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Glad you enjoyed it 😁
@10Totti
@10Totti 4 ай бұрын
Another best tutorial! Thanks!
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
You're very welcome! :)
@-INC0GNIT0-
@-INC0GNIT0- 4 ай бұрын
Thanks for doing the research ! Very insightful investigation
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Sure thing! :)
@pilotboba
@pilotboba 4 ай бұрын
It also looks like there is a library called dapper plus that has bulk insert feature as well. Also a commercial paid library.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Will check it out also!
@pilotboba
@pilotboba 4 ай бұрын
A few things. You never adjusted the batch size for EF Core. It is possible to speed up inserts by increasing the batch since. I think by default it is 100. Also bulk-copy has a way to set the batch size. By default I believe it is set to 0 which means all rows. But, its recommended to use it. Bulk-copy by default does a non-trasacted insert. So, if there is an issue there is no way to roll it back. There is an option to have it use a transaction, but I assume that will slow it down a bit. I'm curious if you match the bulkcopy and efcore batch size settings and enable Internal transactions in bulk-copy if the speeds would be closer? I'm not sure, but did your code create the collection each time? Perhaps to remove the overhead of that you could create the user collection in the constructor?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
That is great constructive criticism. I think I'll do a Part 2 of this video in a few weeks, with these remarks + some others I got. I wanted to include data creation for some reason, but I can also do a benchmark without it.
@giammin
@giammin 4 ай бұрын
Really interesting! thanks I think you can send directly the array without the need to convert to anonymous objects in dapper. Anyway it will not change much the benchmark results
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Had some trouble with the Id field so this was a workaround
@rsrodas
@rsrodas 4 ай бұрын
Another alternative, if you already have files ready to import, is to use inside of SQL Server the OPENROWSET command: INSERT INTO Table ( Col1, Col2, ... ) SELECT Col1, Col2, .. FROM OPENROWSET( BULK 'c:\myfile.txt', FORMATFILE='c:\format.xml' ) In the XML file, you define the rules in how the file you want to import is formatted (fixed size, comma split, etc...)
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Does it have to be an XML file? Can it work with CSV? What about JSON?
@rsrodas
@rsrodas 4 ай бұрын
@@MilanJovanovicTech Other option for format file is to use a non-XML...
4 ай бұрын
can you do this in an azure sql database?
@islandparadise
@islandparadise 4 ай бұрын
Love this. One quick qn: For the EFCore approaches, would the performance be consistent on Postgres as well as SQL server?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Hmm, I'm pretty sure the performance wouldn't change dramatically. However, I didn't test that.
@islandparadise
@islandparadise 4 ай бұрын
@@MilanJovanovicTech got it. Thanks mate you're a champ!
@antonmartyniuk
@antonmartyniuk 4 ай бұрын
Surprisingly Dapper doesn't perform well. Still I would like to see results when using Dapper with SQL Bulk Insert command. I personally have used a EFCore.Extensions library, which is a paid one, to do the bulk inserts. My company bought a license for this library and it saved many development days for such things as bulk merge and bulk synchronize operations. Interesting to compare its performance to sql bulk copy class
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
It's nor surprising if you understand how that specific SQL statement works with Dapper.
@harshakumar6890
@harshakumar6890 4 ай бұрын
It's possible to check for dapper with executing SP that accept UDT table as parameter?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
I'll see if I can update the article
@xtazyxxx3487
@xtazyxxx3487 4 ай бұрын
Can you try to concatenate the insert query then try sql raw query with ef and see the results
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
It's what EF does with AddRange
@dy0mber847
@dy0mber847 4 ай бұрын
Will results be different in case of using postgres?🤔
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Not in relative terms between the different options
@way_no6810
@way_no6810 4 ай бұрын
Can u test Dapper Plus
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Can do, in part 2 of this video
@musaalp4677
@musaalp4677 4 ай бұрын
Have you try OpenJson or other json structure with raw sql query?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
No, I did not, but you're welcome to give it a try
@belediye_baskani
@belediye_baskani 4 ай бұрын
What do you think about Bulk Update? Can you run Benchmark for us?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
It all comes down to an UPDATE operation
@lolyasuo1235
@lolyasuo1235 4 ай бұрын
How dapper can be 5 times slower at 1m records than efcore addall? This doesn't make sense at all.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Because Dapper has to unwrap the (collection) loop and run the SQL commands one by one. :)
@EzequielRegaldo
@EzequielRegaldo 4 ай бұрын
So DataTable is EF core without paid lib?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
DataTable is a .NET construct
@EzequielRegaldo
@EzequielRegaldo 4 ай бұрын
@@MilanJovanovicTech amazing ! Thank you for your response :D
@ExtremeTeddy
@ExtremeTeddy 4 ай бұрын
All shown methods are slow compared to „load data from file“. Whenever possible for large data imports use load data from file. It will load gigabyte of data within seconds. One of the best approaches in my experience is to create a temporary table for the datasource and do the load data file command. Then perform the inserts to the entity tables on the database server. Only issue / drawback can be the network connection when loading large datasets.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
What if we don't have a file? Would it be worthwhile storing the file locally before calling that command?
@ExtremeTeddy
@ExtremeTeddy 4 ай бұрын
@@MilanJovanovicTech Care to elaborate on it? Large data imports without a file or source material won't make any sense to me. LOAD DATA FROM FILE requires a file. When a databas is the source I recommend using raw SQL rather than writing application logic.
@たろ羊
@たろ羊 4 ай бұрын
😊
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
🚀
@Ivang017
@Ivang017 4 ай бұрын
Hey Milan, any discount incoming for the The Ultimate Modular Monolith Blueprint course? I bought your Clean Architecture course and I loved it. Just wondering if there is a sale soon or discount for the Modular Monolith Course. Thanks
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Send me an email :)
EF Core Migrations Deep Dive, Applying Migration, SQL Scripts
16:41
Milan Jovanović
Рет қаралды 15 М.
Completely Get Rid of Exceptions Using This Technique
19:24
Milan Jovanović
Рет қаралды 25 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН
진짜✅ 아님 가짜❌???
0:21
승비니 Seungbini
Рет қаралды 10 МЛН
98% Cloud Cost Saved By Writing Our Own Database
21:45
ThePrimeTime
Рет қаралды 391 М.
The Only Database Abstraction You Need | Prime Reacts
21:42
ThePrimeTime
Рет қаралды 215 М.
What is OpenTelemetry?
12:55
Highlight
Рет қаралды 9 М.
How to Bulk Insert Data With Laravel
23:42
Laracasts
Рет қаралды 10 М.
The Alternative to Deleting Data in .NET
11:11
Nick Chapsas
Рет қаралды 37 М.
A Step-by-Step Guide for the Cache-Aside Pattern + Stampede Protection
19:29
Microservices with Databases can be challenging...
20:52
Software Developer Diaries
Рет қаралды 68 М.
This is why understanding database concurrency control is important
9:05
Master Claims Transformation for Flexible JWT Auth in ASP.NET Core
14:10
Milan Jovanović
Рет қаралды 13 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН