日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

对PostgreSQL的prepared statement的深入理解

發布時間:2024/10/12 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对PostgreSQL的prepared statement的深入理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看官方文檔:

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的深入理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。