Skip to content

MySqlBulkImport - #1647

Open
SoftStoneDevelop wants to merge 13 commits into
mysql-net:masterfrom
SoftStoneDevelop:feat/1644v2
Open

SoftStoneDevelop wants to merge 13 commits into
mysql-net:masterfrom
SoftStoneDevelop:feat/1644v2

Conversation

@SoftStoneDevelop

Copy link
Copy Markdown
Contributor

A new way to insert data into tables is MySqlBulkImport. MySqlBulkImport is similar to MySqlBulkCopy in its purpose, but:

  • Has reduced memory consumption (over 30% savings, depending on the number of columns in a row and their type)
  • Adheres to the "push" model instead of "pull"

#1644

Comment thread tests/Benchmarks/Cases/BenchmarkBase.cs Resolved
Comment thread src/MySqlConnector/MySqlBulkImport.cs Resolved
@SoftStoneDevelop

SoftStoneDevelop commented May 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Based on the MySqlBulkImport performance test results:

  • Consistently demonstrates lower memory consumption
  • Either the same execution speed or slightly faster

Benchmark results when building NET 10.0:

Method RowsToInsert Mean Error StdDev Ratio RatioSD Completed Work Items Lock Contentions Gen0 Gen1 Allocated Alloc Ratio
MySqlBulkCopy.DataRows 500 37.83 ms 3.660 ms 10.79 ms 1.09 0.46 4.0000 - - - 333.81 KB 1.00
MySqlBulkCopy.IDataReader 500 35.21 ms 3.846 ms 11.34 ms 1.01 0.46 4.0000 - - - 153.33 KB 0.46
MySqlBulkImport 500 35.69 ms 3.533 ms 10.36 ms 1.03 0.44 3.0000 - - - 87.33 KB 0.26
MySqlBulkCopy.DataRows 5000 201.88 ms 10.446 ms 30.64 ms 1.02 0.21 4.0000 - - - 3606.34 KB 1.00
MySqlBulkCopy.IDataReader 5000 218.33 ms 15.252 ms 44.73 ms 1.10 0.27 4.0000 - - - 1383.8 KB 0.38
MySqlBulkImport 5000 214.19 ms 12.330 ms 36.36 ms 1.08 0.24 7.0000 - - - 791.09 KB 0.22
MySqlBulkCopy.DataRows 50000 559.60 ms 15.841 ms 45.45 ms 1.01 0.11 11.0000 - 3000.0000 1000.0000 33557.29 KB 1.00
MySqlBulkCopy.IDataReader 50000 547.71 ms 17.569 ms 50.97 ms 0.99 0.12 10.0000 - 1000.0000 - 13692.68 KB 0.41
MySqlBulkImport 50000 526.46 ms 13.970 ms 40.31 ms 0.95 0.10 72.0000 - - - 7828.87 KB 0.23
MySqlBulkCopy.DataRows 100000 859.89 ms 19.606 ms 57.50 ms 1.00 0.09 21.0000 - 6000.0000 2000.0000 67092.98 KB 1.00
MySqlBulkCopy.IDataReader 100000 795.86 ms 17.498 ms 51.04 ms 0.93 0.08 19.0000 - 3000.0000 - 27369.34 KB 0.41
MySqlBulkImport 100000 779.99 ms 18.467 ms 53.87 ms 0.91 0.09 108.0000 - 1000.0000 - 15660.21 KB 0.23

@SoftStoneDevelop
SoftStoneDevelop marked this pull request as ready for review May 18, 2026 09:07
@SoftStoneDevelop

Copy link
Copy Markdown
Contributor Author

Benchmark results when building NativeAOT 10.0:

Method RowsToInsert Mean Error StdDev Median Ratio RatioSD Gen0 Completed Work Items Lock Contentions Gen1 Gen2 Allocated Alloc Ratio
MySqlBulkCopy.DataRows 500 36.62 ms 3.345 ms 9.810 ms 36.78 ms 1.08 0.44 - 4.0000 - - - 335.05 KB 1.00
MySqlBulkCopy.IDataReader 500 36.74 ms 3.602 ms 10.622 ms 37.68 ms 1.09 0.46 - 4.0000 - - - 154.31 KB 0.46
MySqlBulkImport 500 35.84 ms 3.295 ms 9.715 ms 34.95 ms 1.06 0.43 - 3.0000 - - - 168.56 KB 0.50
MySqlBulkCopy.DataRows 5000 221.93 ms 15.050 ms 44.138 ms 216.76 ms 1.04 0.32 - 4.0000 - - - 3607.59 KB 1.00
MySqlBulkCopy.IDataReader 5000 184.71 ms 7.003 ms 20.205 ms 183.62 ms 0.87 0.22 - 5.0000 - - - 1385.07 KB 0.38
MySqlBulkImport 5000 175.69 ms 9.207 ms 27.147 ms 177.03 ms 0.83 0.23 - 9.0000 - - - 792.8 KB 0.22
MySqlBulkCopy.DataRows 50000 585.80 ms 17.280 ms 50.408 ms 585.05 ms 1.01 0.12 4000.0000 14.0000 - 1000.0000 1000.0000 33559.43 KB 1.00
MySqlBulkCopy.IDataReader 50000 550.45 ms 15.708 ms 45.573 ms 544.38 ms 0.95 0.12 1000.0000 10.0000 - - - 13693.95 KB 0.41
MySqlBulkImport 50000 531.13 ms 14.457 ms 42.172 ms 518.59 ms 0.91 0.11 - 68.0000 - - - 7830.02 KB 0.23
MySqlBulkCopy.DataRows 100000 873.68 ms 18.822 ms 54.005 ms 858.40 ms 1.00 0.09 7000.0000 23.0000 - 3000.0000 1000.0000 67096.28 KB 1.00
MySqlBulkCopy.IDataReader 100000 785.56 ms 16.453 ms 47.208 ms 775.91 ms 0.90 0.08 3000.0000 20.0000 - - - 27371.06 KB 0.41
MySqlBulkImport 100000 756.34 ms 15.164 ms 43.016 ms 746.45 ms 0.87 0.07 1000.0000 110.0000 - - - 15661.64 KB 0.23

@bgrainger

Copy link
Copy Markdown
Member

Thanks for a practical demonstration of what you're trying to accomplish.

I don't know if I'll merge this specific approach or just treat it as a POC. (Feel free to keep working on it if you want, but for now I don't think it needs to be taken beyond prototype stage.)

@SoftStoneDevelop

Copy link
Copy Markdown
Contributor Author

Okay, let me know if there is anything I need to improve or do on my end, I will be happy to help.

@SoftStoneDevelop

Copy link
Copy Markdown
Contributor Author

FYI: I also noticed that the buffer size in 'MySqlBulkCopy' is taken as const int maxLength = 1048575;, but I don't understand why. It seems like such a large array just went straight to LOH? Is this done on purpose? Why not something around <80 kilobytes?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants