12

I want to know just for curiosity that, does Select Count(*) from SomeTableName also traverses all the rows of the database as that of Select * from SomeTableName?

Or is there any other count field available in database's metadata that updates itself each time a row is added or deleted? And that field is accessed by the former query.

Also I want to know that which of the both queries is faster and how much?

6
  • how can db keep track of count? what if you request count with filter where? Aug 13, 2013 at 10:47
  • 2
    count(*) is fine, select * is not
    – i-one
    Aug 13, 2013 at 10:47
  • @ArsenMkrt then it will only count data the satisfy the condition
    – zxc
    Aug 13, 2013 at 10:48
  • 3
    not a dup @Bridge, this question asks if count * tarverse the table or not Aug 13, 2013 at 10:49
  • @bew, but should it traverse the table for that or not? Aug 13, 2013 at 10:50

1 Answer 1

17
SELECT Count(*)
FROM   SomeTableName 

will always count all rows. Though (unlike SELECT *) it does not have to read all columns and can use the narrowest (non filtered) index available to do so.

Unlike MySQL (MyISAM engine) it does not retrieve the value from metadata.

A rowcount value is available in the metadata and can be retrieved from sys.partitions but this is never used for COUNT queries and isn't always accurate.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.