Search code examples
postgresqldatabase-performance

Is using ANY (or ALL) faster than checking manually against each entry in the ANY? - postgres


Say I want to check

"A does not equal any of the following: A, B, C"

I could write my condition as

A != ALL('{A, B, C})`

I could also write it as

A != A and A != B and A != C

I know I'm not really using types correctly here (mostly for brevity) but the idea should be clear. Is one of these faster than the other? I'm not sure how to test these kind of things on databases yet :(


Solution

  • Both versions should produce the same execution plan, but if you want to get rid of the subjunctive in that sentence, you should check yourself using EXPLAIN.

    By the way, I recommend that you use the SQL standard operator <> rather than !=.