What is the difference between "x IS NULL" and "NOT (x IS NOT NULL)"?

Is there a significant difference in execution, performance or postgresql logic between

SELECT "users".* FROM "users" WHERE ("users"."deleted_at" IS NULL) 

and

 SELECT "users".* FROM "users" WHERE (NOT ("users"."deleted_at" IS NOT NULL)) 

Obviously, if hand-written, the first expression is the one I would write (who would intentionally write double negative ?!). But in this case, I use the ruby ​​arel library to dynamically create both versions, sort of like:

 def generate_query(search_terms, negated=false, users=User) where_clause = arel_for_one_of_many_possible_queries(search_terms) where_clause = where_clause.not if negated users.where(where_clause) end 

And for "deleted" search_term where_clause will be arel_table[:deleted_at].not_eq(nil) , but for other search_terms it can be a lot of sentences, including compound sentences and subqueries. By adding .not to the end, isl will always generate second form SQL. I could generate the first form with a special case of my NULL checks and manually generate .eq or .not_eq , depending on the situation, but I want to get a clear advantage in this before making my code more detailed.

+4
source share
1 answer

Use EXPLAIN to see the difference, if any.

I think a rewriting request will optimize this, but I have not verified the source code for this example.

Edit: I was wrong, this is not at all optimized. Where ("users". "Deleted_at" IS NULL) can use the index, the value (NOT ("users". "Deleted_at" NOT NOT)) scans the serial drive.

+7
source

Source: https://habr.com/ru/post/1387766/