📄 prepare.7
字号:
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "PREPARE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEPREPARE \- prepare a statement for execution.SH SYNOPSIS.sp.nfPREPARE \fIname\fR [ (\fIdatatype\fR [, ...] ) ] AS \fIstatement\fR.sp.fi.SH "DESCRIPTION".PP\fBPREPARE\fR creates a prepared statement. A preparedstatement is a server-side object that can be used to optimizeperformance. When the \fBPREPARE\fR statement isexecuted, the specified statement is parsed, rewritten, andplanned. When an \fBEXECUTE\fR command is subsequentlyissued, the prepared statement need only be executed. Thus, theparsing, rewriting, and planning stages are only performed once,instead of every time the statement is executed..PPPrepared statements can take parameters: values that aresubstituted into the statement when it is executed. When creatingthe prepared statement, refer to parameters by position, using$1, $2, etc. A corresponding list ofparameter data types can optionally be specified. When aparameter's data type is not specified or is declared asunknown, the type is inferred from the contextin which the parameter is used (if possible). When executing thestatement, specify the actual values for these parameters in the\fBEXECUTE\fR statement. Refer to EXECUTE [\fBexecute\fR(7)] for moreinformation about that..PPPrepared statements only last for the duration of the currentdatabase session. When the session ends, the prepared statement isforgotten, so it must be recreated before being used again. This also means that a single prepared statement cannot be used bymultiple simultaneous database clients; however, each client can createtheir own prepared statement to use. The prepared statement can bemanually cleaned up using the DEALLOCATE [\fBdeallocate\fR(7)] command..PPPrepared statements have the largest performance advantage when asingle session is being used to execute a large number of similarstatements. The performance difference will be particularlysignificant if the statements are complex to plan or rewrite, forexample, if the query involves a join of many tables or requiresthe application of several rules. If the statement is relatively simpleto plan and rewrite but relatively expensive to execute, theperformance advantage of prepared statements will be less noticeable..SH "PARAMETERS".TP\fB\fIname\fB\fRAn arbitrary name given to this particular preparedstatement. It must be unique within a single session and issubsequently used to execute or deallocate a previously preparedstatement..TP\fB\fIdatatype\fB\fRThe data type of a parameter to the prepared statement. If thedata type of a particular parameter is unspecified or isspecified as unknown, it will be inferredfrom the context in which the parameter is used. To refer to theparameters in the prepared statement itself, use$1, $2, etc..TP\fB\fIstatement\fB\fRAny \fBSELECT\fR, \fBINSERT\fR, \fBUPDATE\fR,\fBDELETE\fR, or \fBVALUES\fR statement..SH "NOTES".PPIn some situations, the query plan produced for a preparedstatement will be inferior to the query plan that would have beenchosen if the statement had been submitted and executednormally. This is because when the statement is planned and theplanner attempts to determine the optimal query plan, the actualvalues of any parameters specified in the statement areunavailable. PostgreSQL collectsstatistics on the distribution of data in the table, and can useconstant values in a statement to make guesses about the likelyresult of executing the statement. Since this data is unavailablewhen planning prepared statements with parameters, the chosen planmay be suboptimal. To examine the query planPostgreSQL has chosen for a preparedstatement, use EXPLAIN [\fBexplain\fR(7)]..PPFor more information on query planning and the statistics collectedby PostgreSQL for that purpose, seethe ANALYZE [\fBanalyze\fR(7)]documentation..PPYou can see all available prepared statements of a session by querying the\fBpg_prepared_statements\fRsystem view..SH "EXAMPLES".PPCreate a prepared statement for an \fBINSERT\fRstatement, and then execute it:.sp.nfPREPARE fooplan (int, text, bool, numeric) AS INSERT INTO foo VALUES($1, $2, $3, $4);EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);.sp.fi.PPCreate a prepared statement for a \fBSELECT\fRstatement, and then execute it:.sp.nfPREPARE usrrptplan (int) AS SELECT * FROM users u, logs l WHERE u.usrid=$1 AND u.usrid=l.usrid AND l.date = $2;EXECUTE usrrptplan(1, current_date);.sp.fiNote that the data type of the second parameter is not specified,so it is inferred from the context in which $2 is used..SH "COMPATIBILITY".PPThe SQL standard includes a \fBPREPARE\fR statement,but it is only for use in embedded SQL. This version of the\fBPREPARE\fR statement also uses a somewhat differentsyntax..SH "SEE ALSO"DEALLOCATE [\fBdeallocate\fR(7)], EXECUTE [\fBexecute\fR(l)]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -