对PostgreSQL的prepared statement 的理解

http://www.postgresql.org/docs/9.2/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.

在psql窗口A进行试验:

prepare 动作:

postgres=# prepare test(int) AS
postgres-# select * from customers c where c.cust_id = $1;
PREPARE
postgres=# 

execute 动作:

postgres=# execute test(3);
 cust_id | cust_name 
---------+-----------
       3 | Taylor
(1 row)

postgres=# 

此时,看 pg_prepared_statements 的状况:

postgres=# select * from pg_prepared_statements;
 name |                    statement                    |         prepare_time          | parameter_types | from_sql 
------+-------------------------------------------------+-------------------------------+-----------------+----------
 test | prepare test(int) AS                           +| 2013-06-14 15:58:22.796369+08 | {integer}       | t
      | select * from customers c where c.cust_id = $1; |                               |                 | 
(1 row)

postgres=# 

在另外的一个窗口B,再接着进行试验,我就无法看到 pg_prepared_statements中有值了!

postgres=# select * from pg_prepared_statements;
 name | statement | prepare_time | parameter_types | from_sql 
------+-----------+--------------+-----------------+----------
(0 rows)

postgres=# 

甚至我在此窗口B中,再次执行 execute test(3) ,也会失败。

postgres=# execute test(3);
ERROR:  prepared statement "test" does not exist
postgres=# 

由此,可以得出结论:

在PostgreSQL中,prepared statement 也是session 单位的。在为各个客户端服务的各个子进程之间,无法共享已经分析好的执行计划!

如果我的应用程序里,拥有多个数据库连接,是无法共享prepared statement的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值