开发者

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

I've recently 开发者_如何学JAVAchanged 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 SELECTing 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜