Batching separate prepared npgsql commands

ⅰ亾dé卋堺 提交于 2019-12-08 04:48:31

问题


Say you have three prepared NpgsqlCommands. Because they must be executed separatedly. But sometimes two or all three must be executed at once (well, one after the other).

Is there a way to batch and reuse these previously prepared commands using a single roundtrip to the server? (goal is to minimize latency)

Now I use a fourth command and semicolon separate a copy of the original three commands, and prepare this -- but I assume this will use more resources at the server, and more SQL parsing at the npgsql client.


回答1:


Npgsql supports batching through including several statements in your CommandText, separated by semicolons. These are executed in a single network roundtrip, and can also be prepared:

cmd.CommandText = "SELECT ...; UPDATE ...";
cmd.Prepare();

Internally, Npgsql splits such commands on semicolons and prepares each statement separately (PostgreSQL does not actually recognize batches, only individual statements). In addition, Npgsql manages prepared statements on a statement-by-statement level, and knows to reuse already-existing statements. This means that if you prepare two commands which contain the same statements, those statements will share the same server-side prepared statement resource.



来源:https://stackoverflow.com/questions/53262323/batching-separate-prepared-npgsql-commands

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!