MySql:使用 .NET/Connector 使用准备好的语句调用存储过程会更快吗?
我已经阅读了一些有关 MySql 的预处理语句的内容,并且 .NET/Connector 确实支持它们。
我想知道的是,如果我使用预准备语句调用相同的存储过程数千次,是否比不使用预准备语句执行更快或更好的性能(因为存储过程实际上应该已经编译)?
例如:
var mySqlCmd = new MySqlCommand(con, "call sp_someProcedure(@param1, @param2);");
mySqlCmd.Prepare();
mySqlCmd.Parameters.AddWithValue("@param1", "");
mySqlCmd.Parameters.AddWithValue("@param2", "");
for (int i = 0; i < 1000; i++)
{
mySqlCmd.Parameters["@param1"].Value = i.ToString();
mySqlCmd.Parameters["@param2"].Value = i.ToString();
mySqlCmd.ExecuteNonQuery();
}
I've been reading a bit about Prepared statements with MySql, and the .NET/Connector does support them.
What I'm wondering, is if I use a prepared statement to call the same stored procedure thousands of times, is that any faster or better performance than not using prepared statements to do so (since the stored procedure should really be compiled already)?
Eg:
var mySqlCmd = new MySqlCommand(con, "call sp_someProcedure(@param1, @param2);");
mySqlCmd.Prepare();
mySqlCmd.Parameters.AddWithValue("@param1", "");
mySqlCmd.Parameters.AddWithValue("@param2", "");
for (int i = 0; i < 1000; i++)
{
mySqlCmd.Parameters["@param1"].Value = i.ToString();
mySqlCmd.Parameters["@param2"].Value = i.ToString();
mySqlCmd.ExecuteNonQuery();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
准备好的语句至少需要 2 次数据库调用。第一个调用(准备)采用应用程序级别的 sql 语句,例如 select * from users where user_id = ?并在数据库中创建一个查询模板,然后对其进行解析和验证。后续调用只需将值从应用程序层传递到数据库,然后将其插入模板并执行。
数据库中已存在存储过程。它在创建过程中已被解析和验证。存储过程有点像上面提到的模板,但它是数据库的永久功能而不是临时功能。
因此,要执行存储过程,您只需要传递参数 - 不需要准备它。
A prepared statement requires a minimum of 2 db calls. The first call (prepare) takes your application level sql statement e.g. select * from users where user_id = ? and creates a query template in the database which is then parsed and validated. Subsequent calls simply involve passing values from your application layer to the db which are then inserted into the template and executed.
A stored procedure already exists in the database. It has been parsed and validated during creation. A stored procedure is a bit like the template mentioned above but it's a permanent feature of the database not a temporary one.
Therefore, to execute a stored procedure you only need to pass it params - you dont need to prepare it.