我有一个Postgresql函数,该函数从树中不同的postgresql函数收集数据(数据相同)。但是,应该仅可以基于过滤器运行这三个功能中的某些功能。例子
FROM (SELECT events.id,
events.slug,
events.picture_url,
events.lat,
events.lng,
events.user_id,
events.inserted_at,
events.updated_at,
events.geom,
events.address,
events.place
FROM (
-- DO FILTERING CASE WHEN (filter = 'global' OR filter = 'self') THEN
SELECT * from my_events(param_user_id)
END
UNION
-- DO FILTERING CASE WHEN (filter = 'global' OR filter = 'friends') THEN
--by starting from follows table
SELECT * FROM follows(param_user_id)
END
UNION
-- DO FILTERING CASE WHEN (filter = 'global' OR filter = 'friends') THEN
SELECT * FROM friends_events(param_user_id)
END
) AS events ORDER BY events.inserted_at DESC LIMIT limit_count OFFSET limit_count * page
) AS e; END; $function$
过滤器是文本字符串,例如filter =“ function 1”。 如果filter =“ function 1”,则仅执行SELECT并忽略其他两个。
If filter = "all" then run all of them. Lambda example
SELECT (<fields>) FROM(
IF filter = "function1" OR filter = "all" then
SELECT <function 1>
IF filter = "function 2" OR filter = "all" THEN
SELECT <function2>
以此类推。
答案 0 :(得分:0)
您可以按以下方式使用独立IF:
IF filter = 'func1' THEN
RETURN QUERY SELECT ...;
ELSEIF filter = 'func2' THEN
RETURN QUERY SELECT ...;
ELSEIF
...
END IF;
或者更好的是,动态查询不重复大的SELECT:
DECLARE
_str text;
BEGIN
...
--Build dynamic query:
_str = 'SELECT ... FROM '||filter||'('||param_user_id||') ...';
--Print to console (Use to debug only)
RAISE NOTICE '_str = %', _str;
--Execute and return results
RETURN QUERY EXECUTE _str;
...
END
来自filter
的文本与查询串联在一起,并用作函数名称。如果您不能在filter
中输入函数名称,请使用
CASE filter WHEN 'func1' THEN 'name_of_func1' WHEN 'func2' THEN 'name_of_func2' ... ELSE 'name_of_default_func' END
串联,而不是filter
本身。
使用这种方式,使您的案例更具可读性。
答案 1 :(得分:0)
或者您可以修改查询和一些变量以选择要运行的功能。
CREATE FUNCTION a1() RETURNS TABLE (t text, k text) AS $f$
SELECT f1, f2 FROM t1;
$f$ LANGUAGE sql;
EXPLAIN ANALYZE
SELECT * FROM a1() WHERE true
UNION ALL
SELECT * FROM a2() WHERE false
UNION ALL
SELECT * FROM a3() WHERE false;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
Append (cost=0.25..20.25 rows=1000 width=64) (actual time=0.677..0.865 rows=291 loops=1)
-> Function Scan on a1 (cost=0.25..10.25 rows=1000 width=64) (actual time=0.676..0.758 rows=291 loops=1)
Planning time: 0.222 ms
Execution time: 0.981 ms
您可以一起调用所有三个功能:
EXPLAIN ANALYZE
SELECT * FROM a1() WHERE true
UNION ALL
SELECT * FROM a2() WHERE true
UNION ALL
SELECT * FROM a3() WHERE true ;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
Append (cost=0.25..60.75 rows=3000 width=64) (actual time=0.682..2.324 rows=873 loops=1)
-> Function Scan on a1 (cost=0.25..10.25 rows=1000 width=64) (actual time=0.681..0.770 rows=291 loops=1)
-> Function Scan on a2 (cost=0.25..10.25 rows=1000 width=64) (actual time=0.512..0.599 rows=291 loops=1)
-> Function Scan on a3 (cost=0.25..10.25 rows=1000 width=64) (actual time=0.557..0.644 rows=291 loops=1)
Planning time: 0.227 ms
Execution time: 2.615 ms
或者全部都不是
EXPLAIN ANALYZE
SELECT * FROM a1() WHERE false
UNION ALL
SELECT * FROM a2() WHERE false
UNION ALL
SELECT * FROM a4() WHERE false ;
QUERY PLAN
-------------------------------------------------------------------------------------
Result (cost=0.00..0.00 rows=0 width=64) (actual time=0.002..0.002 rows=0 loops=1)
One-Time Filter: false
Planning time: 0.191 ms
Execution time: 0.036 ms