对PostgreSQL的prepared statement的深入理解

摘要:
执行:计划--˃执行负责生成执行计划和执行。每次都应执行此步骤。Preparedstatements cantake parameters:执行语句时替换到语句中的值。创建表示语句时,使用$1、$2等按位置引用参数。可以选择指定相应的参数数据类型。当参数的数据类型未指定或声明为未知时,将从使用该参数的上下文中推断出该类型。执行语句时,为EXECUTE语句中的参数指定实际值。有关详细信息,请参阅执行表格。Preparedstatement可以采用参数,也可以不采用参数。

看官方文档:

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的深入理解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇解决Ruby中文字符串【乱码】的方法使用xentools快速创建虚拟机下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Oracle- 存储过程和异常捕捉

我学习了ORACLE存储过程的基础知识,并做了一些备注以供将来参考。创建一个无参数存储过程createprocedure_myPro1isbegininsertintdepthvalues;终止修改无参数存储过程createorreplaceprocedure_myPro1开始插入深度值;终止创建和修改参数存储过程createorreplaceprocedu...

Delphi国内优秀网站及开源项目

它被设计为与Delphi2010或更高版本一起使用,它使用了旧版Delphi中没有的语言/RTL功能。https://github.com/VSoftTechnologies/DUnitXDelphiIDEColorizerDelphiIDEColorizer是一个插件,它可以使RADStudioIDE的工作空间https://github.com/RRUZ...

QMap与QHash

Qt提供两个主要的关联容器类:QMap和QHash。QMap的K和T有一对方便的函数keys()和values(),它们在处理小数据集时显的特别有用。QMap重载了value,返回一个给定键多有值的QList列表。在内部,它们都依赖于QHash,且都像QHash一样对K的类型有相同的要求。...

Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)

在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器;一种是使用QTimer类。当定时器触发时,应用程序会发送一个QTimerEvent。与定时器相关的成员函数有:startTimer()、timeEvent()、killTimer()。virtualvoidQObject::timerEvent;虚函数timerEvent()被重载来实现用户的...

「雕爷学编程」Arduino动手做(26)——4X4矩阵键盘模块

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。108种传感器模块系列实验实验二十六:4X4矩阵键盘模块矩阵键盘是单片机外部设备中所使用的排布类似于矩阵的键盘组。工作原理矩阵键盘又称为行列式键盘,它是用4条I/O线作为行线,4条I/O线作为列线组成的键盘。矩阵键盘所需库文件在ArduinoIDE1.8.0...

winform窗体(六)——DataGridView控件及通过此控件中实现增删改查

“,”Delete Data“,btn)==DialogResult.Yes){}V.多条件查询。如果用户没有输入任何内容或文本框为空,则查询所有内容。//设置两个常量条件stringtj1=”1=1“;stringtj2=”1=1”;//根据用户的输入更改条件。//如果用户输入名称If(name!=”“){tj1=“Namelike@name“;}//如果...