Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance Comparation Between sql SELECT NULL and SELECT 1

Which one is better for performance

IF EXISTS(Select null from table)

or

IF EXISTS(Select 1 from table)

?

like image 551
kamaci Avatar asked Dec 05 '22 23:12

kamaci


1 Answers

Both perform the same, because the SELECT clause in the EXISTS is never evaluated. You can test using:

... EXISTS(SELECT 1/0 FROM TABLE) 

That should trigger a divide by zero error, but won't.

I personally prefer using NULL because it's obvious that nothing is referenced in the table, so it's more visible to others. Selecting a value, like the INT number 1 in the second example, can lead to assumptions about what is happening if not familiar with the EXISTS clause.

like image 196
OMG Ponies Avatar answered Dec 10 '22 10:12

OMG Ponies