OK, my problem is that I have a script that loads on every page, it checks if a signed in user is banned or not by checking a column named "banned" that can output either 0 (not banned) or 1 (banned). I've tried searching for this, but didn't find an absolute answer to which performs better:
Fetching a COUNT(*) query:
$query = $PDO->prepare("SELECT COUNT(*) FROM users WHERE id = :ID AND banned = 1");
$query->execute(array(":ID" => $USER_ID));
if ($query->fetchColumn() > 0) {
// USER IS BANNED! SHOW ERROR MESSAGE!
}
Using rowCount() instead of fetching:
$query = $PDO->prepare("SELECT banned FROM users WHERE id = :ID AND banned = 1");
$query->execute(array(":ID" => $USER_ID));
if ($query->rowCount() > 0) {
// USER IS BANNED! SHOW ERROR MESSAGE!
}
limit 1
to restrict the second one from looking for more rows.SELECT 1
in the second case ... but I strongly suspect we're talking about such tiny differences in performance it's not really worth bothering with unless you're sort of Google/Facebook kinda size.SELECT COUNT(*) ...
would make the most sense and would perform the best if indexes are being used