I have a simple question but I couldn’t found the answer on the internet.
I have a SQL table named result with only 2 fields : name and contents.
As every single name is unique I put my primary key on the field name.
What I want to do in Php is to specified a name and to get its contents :
$req = $bdd->prepare('SELECT contents FROM result WHERE name = :name');
$req->execute(array('name' => $inputName));
$donnees = $req->fetch();
$result=$donnees['contents'];
echo $result;
Of course this code works, but what I want to know is do I have to specify on my PHP code that name is the primary key or it already knows it and thus the execution speed of my query is at its maximum.
Thank you.
Don’t do premature optimization.
First you need a real world use case, then you need to profile/benchmark that in order to find the bottleneck and then think about what and how to optimize.
profiling sql queries can be done using
EXPLAIN SELECT ...– but that won’t be necessary for your simple query if name is already indexed.In case you want to use this result table as a cache – that’s probably not the best solution. Think about using a in-memory key value store like memcache.