Very often i need to prevent querys to get executed when a value like a email already exist.
Until now i searched for the value like that:
$checkemailexist = $X['db']->prepare("SELECT uid FROM userdata WHERE uid = :uid LIMIT 1");
$checkemailexist->execute(array(
':uid'=>$uid
));
if(empty($checkemailexist)){
INSERT QUERY ..
}
...
The problem on a big database with many rows, a string search even on a varchar can take a lot of performance and time.
So i made the uid column unique and tried something like that:
try{
$insertuser = $X['dbh']->prepare("
INSERT INTO user (uid) VALUES (:uid)
");
$insertuser->execute(array(
':uid'=> $mail
));
} catch (PDOException $e) {
header("Location: ...");
exit();
}
Its working fine, but could the performance even be worse ?