如果我们有一个像这样的准备好的语句:
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/