Npgsql: How do Prepared Statements
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.) 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.) 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
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.
Thanks!
I'd advise you to not use prepared statements. Npgsql doesn't have a very good performance yet with it. :( Sorry for that.
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.
精彩评论