3
I have this doubt of what is preferred to do when I need data from the two tables to get the result.
I always did the comparison in PHP, but I learned the SQL relationship commands that could help. What is the most correct way to do, regarding runtime, standard and performance?
I used to do this:
<?php
$consultaRecomendados=$con->prepare("SELECT * FROM hf_recomendados");
$consultaRecomendados->execute();
$resultadoRecomendados=$consultaRecomendados->fetchAll();
foreach($resultadoRecomendados as $produtoRecomendado){
$contultaProdutoRecomendado=$con->prepare("SELECT * FROM hf_produtos WHERE id=:id");
$contultaProdutoRecomendado->execute(array(":id"=>$produtoRecomendado['idProduto']));
$resultadoProdutoRecomendado=$contultaProdutoRecomendado->fetch();
echo $resultadoProdutoRecomendado['nome'];
}
?>
And I went like this:
<?php
$contultaRecomendados=$con->prepare("SELECT * FROM hf_recomendados r JOIN hf_produtos p
ON r.idProduto=p.id");
$contultaRecomendados->execute();
$resultadoRecomendados=$contultaRecomendados->fetchAll();
foreach($resultadoRecomendados as $produtoRecomendado){
echo $produtoRecomendado['nome'];
}
?>
Is there a better or more correct way? What technical considerations should I have?
Line of code is irrelevant. An efficient 100-line code is usually worth more than a 2-line pig code, unless in rare cases. Qq way, the 1st example of yours is a programming failure even working. The 2nd case is delegating the task to the right place.
– Bacco
So that’s why I came up with this question...
– LocalHost
Note that in the first case you are requesting a series of queries from the server, without the minimum optimization. In the second case, it is a query only, and the relation is done on the server. If you do a test with a lot of data you will notice an absurd difference even in speed. I am not going to tell you that it should NEVER be done the first way, but in normal situations there is no reason and there is no reason. And if you ever need it, it pays to do an internal query, and use bind. (and if it is Mysql, forget this PDO business, which is an unnecessary and ineffective layer especially in these cases)
– Bacco
Because it says that "your first example is a programming fault"?
– LocalHost
Pq is extremely stressful for script execution to do a number of queries within a loop - the difference in performance is brutal, and loses atomicity. You can have data variation between a select and another, generating a number of inconsistencies. There are other problems as well. If no one posts anything cool explaining to you, and there’s time, I try to work something out.
– Bacco
I understood. I don’t really like doing various things within a loop. It feels heavier. It’s the same feeling of using loop input from another loop
– LocalHost
Loop inside Loop if used normally has no problem. As long as it is suitable for the specific case, of course.
– Bacco
And depending on the size of each loop as well. Thank you Bacco, either way already helped enough
– LocalHost
Only one comment, the first example should only be used if and only if there is no other resource
– MarceloBoni
@Marcelobonifazio, you say that about performance? something that complements this comment?
– LocalHost
It’s more in line than @Acco said, never say you’ll never need to use any feature like this, but if you can avoid it, it’s the best performance-oriented decision
– MarceloBoni
JOIN SQL serves precisely to prevent the software from being responsible for this data junction.
– mau humor
If you want efficiency and delegate responsibility to manipulate the data to your code, work with "txt" files, as this killing the purpose of a DBMS
– mau humor
You need to analyze, make a
join
may be unnecessary. Everything @Bacco said I agree with, however thejoin
should be done only if at that time the information is relevant, for example, a list of records where there is an "edit" button. For the list I won’t do thejoin
if the information is irrelevant, then I’ll just do thejoin
when the client clicks to edit. I’m doing more than one database query, it’s true, but in that case there’s performance gain.– Filipe Moraes