sql - PostgreSQL:是否值得准备一个简单地调用存储过程并传递其参数的语句

标签 sql postgresql

如果我们有一个像这样的准备好的语句:

SELECT my_func($1::text, $2::int)

如果我使用此调用准备一个语句并通过准备好的语句进行调用,是否会提高速度。

最佳答案

让我引用 the docs这里:

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.

强调是我的。我认为它清楚地说明了 PREPARE 在哪些条件下可以带来好处。

尽管如此,目前所有语言都提供了一种本地方式来准备语句 (like PHP),因此整个机制都是在幕后为您执行的。

简而言之:

  • 如果是客户端一次性的,直接执行;
  • 如果它来自应用程序并假定用户输入,请使用您的平台及其功能,为安全原因做好准备;
  • 如果语句在 session 中执行多次,请使用任何方式(PREPARE 或平台的功能)为性能原因做好准备。

关于sql - PostgreSQL:是否值得准备一个简单地调用存储过程并传递其参数的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767818/

相关文章:

ruby-on-rails - Postgres 列为列

c# - 如何正确使用 ExecutionStrategy?

SQL select from table where key in array group by 列

mysql - 如何对包含子句 "order by"、 "desc"和 "Limit"的多个列使用 distinct

postgresql - 创建一个带有参数作为子选择的函数

ruby-on-rails - 涉及不同类型键的Rails关联

java - PostgreSQL Java 身份验证错误

sql - 获取多行的查询需要逗号分隔的输出

sql - RDBMS数据库中的多对多关系

mysql - 'IN/ALL/ANY' 子查询中的未知列