,我有以下数据:遍历表递归查询
cte1
=================
gp_id | m_ids
------|----------
1 | {123}
2 | {432,222}
3 | {123,222}
和一个功能foobar(m_ids integer[])
。该功能包含以下CTE:
with RECURSIVE foo as (
select id, p_id, name from bar where id = any(m_ids)
union all
select b.id, b.p_id, b.name from bar b join foo f on f.p_id = b.id
)
功能正在使用一种像这样:
select foobar(m_ids) from cte1;
现在,作为提高性能的过程的一部分,我被告知要摆脱功能。我的计划是在我的cte链中使用cte foo
,但我坚持试图调整any(m_ids)
的使用。
编辑:要清楚,问题是,那些在where id = any(m_ids)
语句中使用m_ids
是函数的参数,所以我就以使其功能外工作变换CTE。
我想到了以下几点:
with RECURSIVE foo as (
select (select id, p_id, name from bar where id = any(cte1.m_ids)
union all
select b.id, b.p_id, b.name from bar b join foo f on f.p_id = b.id)
from cte1
)
但是,这是行不通的,因为
1)recursive query "foo" does not have the form non-recursive-term UNION [ALL] recursive-term
2)subquery must return only one column
最后,我希望得到我的数据,形式如下:
m_ids |foobar_result
---------|-------------
{123} | 125
{432,222}| 215
为什么你不能在没有任何改变的情况下复制CTE?你不会在你的问题中的任何地方解释它。我们也没有看到功能的全部,所以不能说如何得到想要的结果。这个子选择背后的推理是什么?你想达到什么目的? –
@ŁukaszKamiński我不能复制CTE,因为在'where id = any(m_ids)'语句m_ids是在函数中传递的参数。 –