Counts, we all love to show counts in our applications. Under a
high traffic website, it's visually appealing to show a big
number. But the cost of generating that big number realtime can
be very expensive on our mySQL backend.
For example:
$sql = SELECT COUNT(*) FROM VERY_BIG_TABLE WHERE key_part1=NUM AND no_key IN (1,2,3) AND key_part2=NUM
Would you think this is fast? In a dev environment yes, but this
query has to get the exact row count that meets the requirements
of the WHERE clause.
In many cases we use a count to solve a BOOLEAN term in our code,
for example
$count_result = db_fetch($sql)
if ($count_result) {
// do something
} else {
$SHOW_ERROR = 1;
}
Don't use a count for this purpose. Just do something like
[Read more]
$sql = …