For the number of SQL rows, is it more efficient to use PDO rowCount or count (*) AS?

advertisements

On my script, I have about 15 SQL queries just for counting the number of rows and displaying them to the user.

What is the most efficient way?

Should I use:

$stmt=$cxn->prepare("SELECT id FROM items WHERE seller=?");
$stmt->execute(array($username));
echo $stmt->rowCount();

Or this:

$stmt=$cxn->prepare("SELECT count(*) as count FROM items WHERE seller=?");
$stmt->execute(array($username));
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    echo $row['count'];

Thanks in advance.


The short answer is Count(*) will be faster.

However, that assumes your not using the data, if you are going to select the data, and you want a count, then use your first method.

("SELECT id FROM items WHERE seller=?");

If you have an index on the table, then that will return almost instantly.