简体   繁体   中英

Is there a performance difference between `=` and `<=>`?

I've recently changed all my where conditions to use <=> instead of = because I need to check against nulls. Are there any performance concerns?

There is no real performance impact here is a test to verify for yourself

mysql> SELECT BENCHMARK(1000000, (SELECT SQL_NO_CACHE userId FROM Activity WHERE userId<=>42459204 LIMIT 1));

Make sure that you need to use <=>

NULL-safe equal. This operator performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is NULL.

If you just need to check the rvalue do

col=CONST AND CONST IS NOT NULL

or t1.col=t2.col

<=> is basically a shortcut to include OR (Val1 IS NULL AND Val2 IS NULL) or IS NOT DISTINCT FROM

It is an additional operation but the difference should be negligible unless you are SELECT ing the data to be compared because otherwise the first SELECT returning NULL doesn't need to execute the second SELECT because the standard equality operator = will always yield false.

As @Dathan noted, make sure this is actually when you intend to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM