0
$userid = $_SESSION[userid]
$sqll = mysql_query("SELECT count(id) FROM entries WHERE userid='$userid'");

Is using count(id) efficient? Or is there a better way?

4 Answers 4

4

count(*) should do.

Make sure you escape $userid, though.

Edit: just half-a-word of explanation. count(column) counts all rows for which column is not null. Using * is supposed to be the most efficient way of counting rows regardless of content and more descriptive in case someone (including yourself) is ever going to (re)read the code.

3

Make sure you have an index on your userid column (if it's a primary key, you already do).

If you've got userid as a primary key, then this should be an efficient query and will have the same efficiency as using count(*).

1

Some say it's more efficient to use COUNT(*) it also depends how often are you going to count this value and how often is it going to change. May be it's better to cache it somewhere.

0

Since you don't need to count the id column specifically you can use COUNT(*), which may be faster with some engines.

1

Your Answer

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

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