I am wondering if there is a performance difference between these two queries that check if a record exists?
select count(1) from table where id = 1;
or
select id from table where id = 1;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think there will be much of a difference between those two queries : the difference being selecting a field (which is part of an index) in the second case, and counting one line in the first… Not that much of a difference.
Still, out of curiosity, I did a very quick benchmark of those kind of queries on a database I have on my computer — note there are only like 7 lines in the post table, so it might not be that close to a real situation, but as there is a PK on
id, which means an index….Here’s what I got :
So, really not that much of a difference, it seems ^^