If you need the number of items that satisfy a criteria in a SQL database table, intead of
count($db->query("SELECT * FROM customer_comments WHERE status = 1")->rows)
use
$db->query("SELECT COUNT(*) FROM customer_comments WHERE status = 1")->value
Using COUNT(*) is faster because databases are optimized for such operations. When you fetch all rows into your application and then count them using count(), you're moving the data processing to the application level, which is generally less efficient.
No comments:
Post a Comment