I'm trying to make the fastest COALESCE() that accepts two or more arguments, and returns the first non-null AND non-empty ("") value.
我正在尝试使用最快的COALESCE()接受两个或多个参数,并返回第一个非空和非空(“”)值。
I'm using this:
我正在使用这个:
CREATE OR REPLACE FUNCTION coalescenonempty(VARIADIC in_ordered_actual varchar[])
RETURNS varchar AS $$
SELECT i
FROM (SELECT unnest($1) AS i) t
WHERE i IS NOT NULL AND i <> ''
LIMIT 1;
$$ LANGUAGE sql;
It's pretty fast, but still nowhere as fast as COALESCE or CASE WHEN statements.
它非常快,但仍然没有COALESCE或CASE WHEN语句那么快。
What do you think?
你怎么看?
1 个解决方案
#1
62
Do not create a user function is you want speed. Instead of this:
不要创建用户功能是你想要的速度。而不是这个:
coalescenonempty(col1,col2||'blah',col3,'none');
do this:
做这个:
COALESCE(NULLIF(col1,''),NULLIF(col2||'blah',''),NULLIF(col3,''),'none');
That is, for each non-constant parameter, surround the actual parameter with NULLIF( x ,'')
.
也就是说,对于每个非常量参数,用NULLIF(x,'')包围实际参数。
#1
62
Do not create a user function is you want speed. Instead of this:
不要创建用户功能是你想要的速度。而不是这个:
coalescenonempty(col1,col2||'blah',col3,'none');
do this:
做这个:
COALESCE(NULLIF(col1,''),NULLIF(col2||'blah',''),NULLIF(col3,''),'none');
That is, for each non-constant parameter, surround the actual parameter with NULLIF( x ,'')
.
也就是说,对于每个非常量参数,用NULLIF(x,'')包围实际参数。