0

I work in a website that sell products. I have a table (sell) with has more than 2.000.000 rows.

I need to improve its performace, so I want to know what is the best way I get the number of "sells" one product has.

a) count(id)

$stmt = $mysqli->prepare("SELECT count(id) as num FROM sell WHERE prod_id=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

$num = $result->fetch_assoc();
$total = $num['num'];

b) num_rows

$stmt = $mysqli->prepare("SELECT id FROM sell WHERE prod_id=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

$total = $result->num_rows;

a or b? why?

2
  • b doesn't work as it will always return "1"
    – John Conde
    Commented Apr 13, 2015 at 13:26
  • sorry, i copy and past it wrong Commented Apr 13, 2015 at 13:31

0

Browse other questions tagged or ask your own question.