(func()).*
避免重複函數呼叫在 9.3 之前的 PostgreSQL 版本中,使用 (func()).*
語法(其中 func
傳回表)可能會導致函數意外多次執行。這會顯著影響查詢效能。
有幾種方法可以有效規避這個問題:
1。子查詢方法:
<code class="language-sql">SELECT (mf).* FROM ( SELECT my_func(x) AS mf FROM some_table ) sub;</code>
2。 OFFSET 0 技術:
<code class="language-sql">SELECT (mf).* FROM ( SELECT my_func(x) AS mf FROM some_table OFFSET 0 ) sub;</code>
3。 CTE(通用表表達式)方法:
<code class="language-sql">WITH tmp(mf) AS ( SELECT my_func(x) FROM some_table ) SELECT (mf).* FROM tmp;</code>
4。橫向連結(PostgreSQL 9.3 及更高版本):
<code class="language-sql"> SELECT mf.* FROM some_table LEFT JOIN LATERAL my_func(some_table.x) AS mf ON true; ``` This is the preferred method for PostgreSQL 9.3 and above. ### Explanation The root cause lies in how PostgreSQL's parser handles `(func()).*` in older versions. The wildcard expands into individual column selections, mistakenly causing the function to be called repeatedly for each column. ### Why Repeated Calls Occur Pre-9.3 PostgreSQL parsers interpret `(func()).*` by replicating nodes within the parse tree. This replication results in a separate function call for every selected column, even if a single call would suffice.</code>
以上是如何在舊 PostgreSQL 版本中使用 `(func()).*` 防止多個函式呼叫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!