Npgsql:如何准备语句 [英] Npgsql: How do Prepared Statements

查看:127
本文介绍了Npgsql:如何准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为多人游戏开发数据库后端。服务器使用C#编写,并通过Npgsql与Postgres数据库进行对话。

we are developing a database backend for a multiplayer game. The server is written in C# and talks to a Postgres database via Npgsql.

现在,手册显示了如何使用准备好的语句:

Now, the manual shows how to use prepared statements:

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;

它表明准备好的语句仅在数据库会话中有效
我现在有两个问题:

And it states that prepared statements are valid only inside a database session. I have two questions now:

1。 )如果我使用相同的命令文本和参数类型创建一个新的NpgsqlCommand对象,或者我必须保留该对象并在再次执行之前简单地更改变量,服务器是否会识别出该命令已准备就绪?在示例中,命令对象放置在查询之后。

1.) Will the server recognize that this command was prepared if I create a new NpgsqlCommand object with the same command text and parameter types or do I have to keep the object and simply change the variables before executing it again? In the example the command object is disposed after the query.

2。)虽然我们只有这种形式的简单语句,但准备好的语句可能会提高性能:

2.) Will prepared statements probably increase the performance although we ONLY have simple statements in this style:

SELECT f1,f2 FROM t1
UPDATE t1 SET f1=1, f2=2

有可能连续执行数百个具有相同样式的查询-当前正在创建一个新的NpgsqlCommand(以及NpgSql池中的NpgsqlConnection)

It is possible that hundreds of queries with the same style are executed in a row - currently creating a new NpgsqlCommand (and also NpgsqlConnection from NpgSql's pool) object for each query.

谢谢!

推荐答案

建议您不要使用准备好的语句。 Npgsql的性能还不是很好。 :(很抱歉。

I'd advise you to not use prepared statements. Npgsql doesn't have a very good performance yet with it. :( Sorry for that.

此外,为了发送大量插入命令,我认为您应该看看NpgsqlCopy支持。它将为您提供更好的性能
希望对您有所帮助。

also, in order to send a lot of insert commands, I think you should look at NpgsqlCopy support. It will give you a much better performance. I hope it helps.

这篇关于Npgsql:如何准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆