Performance Comparation Between sql SELECT NULL and SELECT 1

会有一股神秘感。 提交于 2019-12-22 09:08:15

问题


Which one is better for performance

IF EXISTS(Select null from table)

or

IF EXISTS(Select 1 from table)

?


回答1:


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.



来源:https://stackoverflow.com/questions/4876166/performance-comparation-between-sql-select-null-and-select-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!