Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

("" OR null) VS NOT(!"" AND NOT NULL) in where clause

Does anyone know if there is a performace differance between the following we parts of a where SQL string in mysql?

   WHERE ...  AND (field = "" OR field IS NULL);

and

  WHERE ...  AND (NOT (field != "" AND field IS NOT NULL));
like image 506
pvgoddijn Avatar asked Aug 25 '10 12:08

pvgoddijn


People also ask

WHERE clause is NULL or is not null?

IS NULL & IS NOT NULL in SQL is used with a WHERE clause in SELECT, UPDATE and DELETE statements/queries to validate whether column has some value or data does not exist for that column. Please note that NULL and 0 are not same. A column with NULL value has no value, it's empty.

Can we use NULL in WHERE clause?

Null values can be used as a condition in the WHERE and HAVING clauses. For example, a WHERE clause can specify a column that, for some rows, contains a null value. A basic comparison predicate using a column that contains null values does not select a row that has a null value for the column.

IS NOT NULL and <> NULL difference in SQL?

The IS NULL condition is satisfied if the column contains a null value or if the expression cannot be evaluated because it contains one or more null values. If you use the IS NOT NULL operator, the condition is satisfied when the operand is column value that is not null, or an expression that does not evaluate to null.

What is the difference between != NULL AND IS NOT NULL?

The main difference between e != null and e is not null is the way the the compiler executes the comparison. Microsoft: "The compiler guarantees that no user-overloaded equality operator == is invoked when expression x is null is evaluated."


2 Answers

There is but it is negligibly small, so small I can't tell which would be faster and which slower. It's more of a matter of which is more readable — in this case I would pick the former.

like image 127
BoltClock Avatar answered Oct 06 '22 17:10

BoltClock


there is no performance difference as such,. its much more about code-readability here,. and of course the first statement is more readable,.

one more thing i would like to add is when thinking about optimizations,. one should not delve into micro-optimizations because that doesnt really add up to increasing performance because the difference is very negligible and u end up wasting time and resources and making your code less readable, less maintainable and less manageable.,

so whenever thinking about optimizations,. profile your code and start out with things like improving ur app design, db design, utilizing caching and so on,. rather then indulging into discussions about whether echo is faster or print (an example)

like image 42
ovais.tariq Avatar answered Oct 06 '22 18:10

ovais.tariq