【发布时间】:2011-05-23 06:01:39
【问题描述】:
我们正在为多人游戏开发数据库后端。服务器是用 C# 编写的,并通过 Npgsql 与 Postgres 数据库通信。
现在,手册展示了如何使用准备好的语句:
NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = :column1", conn);
// Now add the parameter to the parameter collection of the command specifying its type.
command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer);
// Now, prepare the statement.
command.Prepare();
// Now, add a value to it and later execute the command as usual.
command.Parameters[0].Value = 4;
并且它指出准备好的语句仅在数据库会话中有效。 我现在有两个问题:
1.) 如果我创建一个具有相同命令文本和参数类型的新 NpgsqlCommand 对象,或者我必须保留该对象并在再次执行之前简单地更改变量,服务器是否会识别该命令已准备好?在示例中,命令对象被放置在查询之后。
2.) 尽管我们只有这种风格的简单语句,但准备好的语句可能会提高性能:
SELECT f1,f2 FROM t1
UPDATE t1 SET f1=1, f2=2
可能连续执行数百个具有相同样式的查询 - 当前为每个查询创建一个新的 NpgsqlCommand(以及 NpgSql 池中的 NpgsqlConnection)对象。
谢谢!
【问题讨论】:
标签: c# performance postgresql prepared-statement npgsql