Test for null in function with varying parameters

前端 未结 4 648
一生所求
一生所求 2020-12-01 08:27

I have a Postgres function:

create function myfunction(integer, text, text, text, text, text, text) RETURNS 
table(id int, match text, score int, nr int, nr         


        
4条回答
  •  天涯浪人
    2020-12-01 09:21

    Several things...

    First, as side note: the semantics of your query might need a revisit. Some of the stuff in your where clauses might actually belong in your join clauses, like:

    from ...
    left join ... on ... and ...
    left join ... on ... and ...
    

    When they don't, you most should probably be using an inner join, rather than a left join.

    Second, there is a is not distinct from operator, which can occasionally be handy in place of =. a is not distinct from b is basically equivalent to a = b or a is null and b is null.

    Note, however, that is not distinct from does NOT use an index, whereas = and is null actually do. You could use (field = $i or $i is null) instead in your particular case, and it will yield the optimal plan if you're using the latest version of Postgres:

    https://gist.github.com/ddebernardy/5884267

提交回复
热议问题