Search code examples
sqltunneling

sql performance detail decision with * or specify field


When i write:

if not exists (select * from sys_maxkey)

is better,or

if not exists (select id from sys_maxkey)

is better when the table sys_maxkey variable.

or it has no difference?I have found internet and analyse exists and in but not pay attention to this special point.

Thank you!


Solution

  • I would suggest to use only 1 like this. This would be the best. As exists only checks whether there is any entry in your table.

    if not exists (select 1 from sys_maxkey)
    

    Although if you want to compare between the above two mentioned in your question then

    if not exists (select id from sys_maxkey)
    

    would be better provided that id is the primary key. You dont have to check all the columns by using * just for checking whether there exists a value in your table.