简体   繁体   English

使用C#MySQL连接器进行批量更新的最快,最有效的方法是什么?

[英]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. 用户发布表单后,我需要使用C#MySQL Connector / NET更新约500行。 I'm reusing the same connection, but I'm executing multiple ExecuteNonQuery, and this is giving me a performance problem. 我正在重用同一连接,但是我正在执行多个ExecuteNonQuery,这给我带来了性能问题。

What is the best way / best practices to solve this problem? 解决此问题的最佳方法/最佳做法是什么?

  • Should I create an extremely long SQL string? 我应该创建一个非常长的SQL字符串吗? 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. 例如“ INSERT INTO xxx(a,b,c)VALUES(@a,@b,@c)”,但是如果这是不可能的约束,请告诉我。

If its only 500 or so rows, i'd be inclined to just build the long SQL string and execute the once 如果只有500行左右,我倾向于只构建长的SQL字符串并执行一次

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. 您在其中构建“(1、2、3),(2、3、4)”部分。

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) . 您可以将更新分批(例如50个)进行批处理。为带有编号的参数名称的50个更新构建SQL查询: 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). 如果您确实在执行插入操作,则可能应该使用一条插入语句插入多行(我认为MySQL支持该行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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