简体   繁体   中英

What is the fastest and most efficient way of doing bulk updates with the C# MySQL Connector?

I need to update around five hundred rows using the C# MySQL Connector/NET after a user posts a form. I'm reusing the same connection, but I'm executing multiple ExecuteNonQuery, and this is giving me a performance problem.

What is the best way / best practices to solve this problem?

  • Should I create an extremely long SQL string? If so, how do I handle having specific parameter values and variables for each row?
  • Is there a streaming interface?
  • Is there an optimal bulk interface?

Note that for security reasons, I am preferring to use parameter variables instead of plain-text. Eg "INSERT INTO xxx (a, b, c) VALUES (@a, @b, @c)", but if this is an impossible constraint let me know.

If its only 500 or so rows, i'd be inclined to just build the long SQL string and execute the once

Insert into MyTable (a, b, c) values (1,2,3), (2, 3, 4)

Where you are building the "(1, 2, 3),(2 ,3, 4)" part.

You can batch the updates into batches of, say, 50. Build a SQL query for 50 updates with numbered parameter names: INSERT INTO xxx (a, b, c) VALUES (@a_1, @b_1, @c_1) . Then you create the parameters with the same names dynamically.

You are saying you need to update but your code shows inserts. If you are really doing inserts, you should probably insert multiple rows with one insert statement (I think MySQL supports that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM