postgresql - 当函数不返回任何内容时,postgres 选择不返回任何记录
问题描述
我很难说出这个问题——
我有这个返回记录的简单选择语句
SELECT q.qid,q.question,depth,active
FROM q
JOIN qp ON q.qid = qp.descendant where qid=723
order by depth
这正确返回了它之后的 1 条记录
但是,当我在 select 语句中包含一个函数时,它不再返回记录,我不确定为什么会这样。
SELECT q.qid,q.question,depth,active,(getprevquestiontemp(qid)).qid parent
FROM q
JOIN qp ON q.qid = qp.descendant where qid=723
order by depth
这是功能
CREATE OR REPLACE FUNCTION getprevquestiontemp(cur_qid integer)
RETURNS SETOF q AS
$BODY$
SELECT q.* FROM q
JOIN qp ON q.qid = qp.ancestor WHERE qp.descendant = cur_qid
AND depth = 1;
$BODY$
LANGUAGE sql VOLATILE
COST 100
ROWS 1000;
解决方案
不要在 SELECT 列表中使用该函数,像使用外连接的表一样使用它:
SELECT q.qid, q.question, depth, active, g.qid parent
FROM q
JOIN qp ON q.qid = qp.descendant
LEFT JOIN getprevquestiontemp(qid) as g(qid) ON true
where qid=723
order by depth
不相关,但是:如果您将函数声明为stable
优化器,则可以 [内联] https://wiki.postgresql.org/wiki/Inlining_of_SQL_functions()该函数可能会带来更好的性能
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射