看官方文档:

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 不一定有什么明显效果。

转载于:https://www.cnblogs.com/gaojian/p/3142096.html

对PostgreSQL的prepared statement的深入理解相关推荐

  1. PostgreSQL — Prepared Statement 预编译

    目录 文章目录 目录 PREPARE 指令 EXECUTE 指令 DEALLOCATE 指令 EXPLAIN 指令 ANALYZE 指令 ERROR,0A000,"cached plan m ...

  2. postgresql报错prepared statement ““S_84““ does not exist

    开发人员说升级JDBC驱动后,很多SQL无法执行,查看数据库报错信息如下 2021-06-09 11:44:07.619 CST,"melotroom","melotro ...

  3. MySQL视图查询报错:Prepared statement needs to be re-prepared

    From: https://www.ywnds.com/?p=12609 今天公司的项目视图查询报错,报错如:ERROR 1615 (HY000): Prepared statement needs ...

  4. Prepared statement needs to be re-prepare

    sql抛出  Prepared statement needs to be re-prepared 异常 按照网上说的设置table_open_cache和table_definition_cache ...

  5. mysql的prepared statement

    写过java,rails,php等的同学都知道,mysql在执行中有一个占位符的sql,比如rails里: ModelTest.where("id = ?", 100) 但是在这个 ...

  6. SQLSTATE[HY000]: General error: xxxx Prepared statement needs to be re-prepared

    tp5 SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared TP操作数据库操作报错 SQLS ...

  7. prepared statement mysql_MySQL数据库之MySQL Prepared Statement

    本文主要向大家介绍了MySQL数据库之MySQL Prepared Statement ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. Summary: in this tuto ...

  8. Prepared statement needs to be re-prepared问题

    执行存储过程时Java代码报错:java.sql.SQLException: Prepared statement needs to be re-prepared:单独调用存储过程也是报错:Prepa ...

  9. Prepared statement needs to be re-prepared

    web页面报错:Error 1615: Prepared statement needs to be re-prepared mysql引起的错误,搜索了一下,都是设置 set global tabl ...

最新文章

  1. Spring自定义注解+redis实现接口限流
  2. C和C++线性表的链式存储
  3. Visual Studio 常见问题及解决方案(持续更新)
  4. 数据库设计Step by Step (10)——范式化
  5. Codeforces Round #709 (Div. 1) B. Playlist 链表维护 + bfs
  6. 手机游戏赚钱到底有多难?接入运营商或需一年
  7. 爬虫第四章 单线程+多任务异步协程
  8. 未来教育考试系统无法答题,点击选项后没有反应,不会记录答题
  9. 计算机注销和重新启动的区别,注销和重启的区别(转载)
  10. win7系统dhcp服务器设置方法,win7设定固定ip和同时支持dhcp的方法
  11. 从IP138上获取数据,查询多个IP的归宿地
  12. Seaborn使用violinplot函数可视化分组小提琴图(violin plot)、使用inner函数设置在小提琴图中使用虚线显示分位数位置(inner = ‘quartile‘)
  13. 大疆无人机飞控软件介绍
  14. 技术、商业和创新的彼岸:“被折叠”的三星
  15. Mysql 查询数据库数据量
  16. markdown中划线
  17. 【云原生 | Kubernetes 系列】K8s 实战 一文学会如何从 PodSecurityPolicy 迁移到内置的 PodSecurity 准入控制器
  18. jquery 线上 引用地址
  19. 【JZOJ1758】过河
  20. HTML中padding作用

热门文章

  1. Spring MVC入门示例教程--静态页面跳转
  2. JDBC连接mysql、创建表、操作数据、PreparedStatement防注入、sql语句返回值类型知识汇总
  3. 使用jspsmartupload完成简单的文件上传系统
  4. php 获取ip mac,php获取IP、MAC地址函数
  5. python数据清洗实例_python 数据的清理行为实例详解
  6. 台式电脑耳机插孔在哪_不到一千元的迷你电脑究竟如何?Intel NUC7CJYH测评来了...
  7. Tcp连接arp协议详解
  8. C/Cpp / STL / map 的 key 为自定义的 struct 或者 class 时,有什么注意事项?
  9. springboot去掉git版本控制_关于 Git 提交这些规范,你都遵守了吗?
  10. python关键字匹配_python通过BF算法实现关键词匹配的方法