本文介绍了批处理单独准备好的 npgsql 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您准备了三个 NpgsqlCommands.因为它们必须分开执行.但有时必须同时执行两个或三个(好吧,一个接一个).

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)

现在我使用第四个命令并用分号分隔原始三个命令的副本,并准备这个 -- 但我认为这将在服务器上使用更多资源,并在 npgsql 客户端使用更多 SQL 解析.

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.

推荐答案

Npgsql 通过在 CommandText 中包含多个语句来支持批处理,这些语句用分号分隔.这些在单个网络往返中执行,也可以准备:

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();

在内部,Npgsql 在分号上拆分这样的命令并单独准备每个语句(PostgreSQL 实际上不识别批处理,只识别单个语句).此外,Npgsql 在逐个语句级别上管理准备好的语句,并且知道重用已经存在的语句.这意味着如果您准备两个包含相同语句的命令,这些语句将共享相同的服务器端准备好的语句资源.

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.

这篇关于批处理单独准备好的 npgsql 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:11