看官方文档:

http://www.postgresql.org/docs/current/static/sql-prepare.html

PREPARE creates a prepared statement. A prepared statement is a server-side object that can be used to optimize performance. When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. When an EXECUTE command is subsequently issued, the prepared statement is planned and executed. This division of labor avoids repetitive parse analysis work, while allowing the execution plan to depend on the specific parameter values supplied.

各自负责的阶段

Prepare: parse--> analyze-->rewritten    Prepare负责 构造语法树,执行一次。

Execute: plan-->Execute  负责生成执行计划和执行,这个步骤每次都要执行。

Prepared statements can take parameters: values that are substituted into the statement when it is executed. When creating the prepared statement, refer to parameters by position, using $1, $2, etc. A corresponding list of parameter data types can optionally be specified. When a parameter's data type is not specified or is declared as unknown, the type is inferred from the context in which the parameter is used (if possible). When executing the statement, specify the actual values for these parameters in the EXECUTE statement. Refer to EXECUTE for more information about that.

Prepared statement 可以带参数,也可以不带参数。

Prepared statements only last for the duration of the current database session. When the session ends, the prepared statement is forgotten, so it must be recreated before being used again. This also means that a single prepared statement cannot be used by multiple simultaneous database clients; however, each client can create their own prepared statement to use. Prepared statements can be manually cleaned up using the DEALLOCATE command.

Prepared statement 在PostgreSQL中是session 单位的。

a single prepared statement cannot be used by multiple simultaneous database clients

何时使用 prepared statement 比较有利:

Prepared statements have the largest performance advantage when a single session is being used to execute a large number of similar statements. The performance difference will be particularly significant if the statements are complex to plan or rewrite, for example, if the query involves a join of many tables or requires the application of several rules. If the statement is relatively simple to plan and rewrite but relatively expensive to execute, the performance advantage of prepared statements will be less noticeable.

最有利的情况是:
When a single session is being used to execute a large number of similar statements.

最不利的情况是:

如果语句的 parse/analyze/rewritten 时间耗费很少,而plan和 execute 的时间耗费很大,则prepares statment 不一定有什么明显效果。

对PostgreSQL的prepared statement的深入理解的更多相关文章

  1. Postgresql:prepared statement "S_1" already exists

    近期由于业务需要和一些json的存储查询需要,把新的应用切到pgsql上来,刚刚切好,是可以正常使用的,但是偶尔会来一下 java连接pgsql 偶尔出现 这个错.   org.postgresql. ...

  2. Java向PostgreSQL发送prepared statement 与 libpq 向PostgreSQL发送prepared statement之比较:

    Java 代码,在数据库端,并没有当成 prepared statetment 被处理. C代码通过libpq 访问数据库端,被当成了 prepared statement 处理.也许是因Postgr ...

  3. 对Prepared Statement 是否可以防止 SQL Injection 的实验

    代码: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; im ...

  4. cannot insert multiple commands into a prepared statement问题原因及解决办法

    问题是这样,我在对数据库进行写操作(添加.删除.修改)时,我想同时删除两个表中的两条关联数据,像这样 let sql = ` DELETE FROM bridge_parts WHERE id = $ ...

  5. Postgre cannot insert multiple commands into a prepared statement

    悲剧... FireDAC连接Postgre数据库, 使用默认的属性, 一次执行多条SQL的时候, 会报"cannot insert multiple commands into a pre ...

  6. PDO 使用prepared statement 预处理LIMIT等非表字段参数

    由于一些驱动不支持原生的预处理语句,因此PDO可以完全模拟预处理.PDO的模拟预处理是默认打开的,即便MYSQL驱动本身支持预处理,在默认打开的状态下,PDO是不会用到MYSQL本身提供的预处理功能. ...

  7. java-mysql(2) Prepared statement

    上一篇学习了java如何链接配置mysql,这篇学习下java如何处理sql预处理语句(PreparedStatement),首先是一个sql预处理的例子: package core; import ...

  8. mysql 执行存储过程报错Prepared statement needs to be re-prepared

    今日思语:不喜欢再见 说再见,因为有时明知道下一次再见已是遥遥无期或是不再见 错误如下: ERROR 1615 (HY000) at line 406 in file: 'update-mysql.s ...

  9. libpq中调用prepared statement:

    代码如下: [root@lex tst]# cat testlibpq.c /* * testlibpq.c * Test the C version of LIBPQ, the POSTGRES f ...

随机推荐

  1. php.ini – 配置文件详解

    详见: https://my.oschina.net/miaowang/blog/299546 这个文件必须命名为''php.ini''并放置在httpd.conf中的PHPIniDir指令指定的目录 ...

  2. Android开发之一些资源索引

    1.android标题栏(titlebar)显示进度条 protected void onCreate(Bundle savedInstanceState) { super.onCreate(save ...

  3. poj 1459 Power Network(增广路)

    题目:http://poj.org/problem?id=1459 题意:有一些发电站,消耗用户和中间线路,求最大流.. 加一个源点,再加一个汇点.. 其实,过程还是不大理解.. #include & ...

  4. freemarker跳出循环

    break语句跳出当前循环,如下: <#list table.columns as c>             <#if c.isPK>                 &l ...

  5. hdu4177:Super Mario

    主席树+离散化.给一段区间.多次询问[l,r]中有多少个数小于k.啊主席树用指针版写出来优美多了QAQ... #include<cstdio> #include<cstring> ...

  6. Vim常用配置(~/.vimrc)(转载)

    原文地址:http://www.2cto.com/os/201309/246271.html " This must be first, beacuse it changes other o ...

  7. 我的WCF之旅(3):在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...

  8. Proguard使用教程

    一.概念 ProGuard是一款免费的Java类文件压缩器.优化器和混淆器.它能发现并删除无用类.字段(field).方法和属性值(attribute).它也能优化字节码并删除无用的指令.最后,它使用 ...

  9. ajax post方法

    定义和用法 post() 方法通过 HTTP POST 请求从服务器载入数据. 语法 jQuery.post(url,data,success(data, textStatus, jqXHR),dat ...

  10. MSP430的比较器

    这两天研究了一下430的比较器,开始的时候,没有看懂是怎么一回事,在网站看这方面的博客,好像懂了,但是一到编程,就变得无从下手,但是,皇天不负有心人,笔者还是把他弄懂了 其实这里就是看懂一幅图,两个寄 ...