I'm having an inner debate at my company about looping queries in this matter:
$sql = "
SELECT foreign_key
FROM t1";
foreach(fetchAll($sql) as $row)
{
$sub_sql = "
SELECT *
FROM t2
WHERE t2.id = " . $row['foreign_key'];
foreach(fetchAll($sub_sql) as $sub_row)
{
// ...
}
}
Instead of using an sql join like this:
$sql = "
SELECT t2.*
FROM t2
JOIN t1
ON t1.foreign_key = t2.id";
foreach(fetchAll($sql) as $row)
{
// ...
}
Additional information about this, the database is huge, millions of rows.
I have of course searched an answer to this question, but nobody can answer this in a a good way and with a lot of up votes that makes me certain that one way is better then the other.
Can somebody explain to me why one of thees methods is better then the other one?
The advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.
I won't leave you in suspense, between Joins and Subqueries, joins tend to execute faster. In fact, query retrieval time using joins will almost always outperform one that employs a subquery. The reason is that joins mitigate the processing burden on the database by replacing multiple queries with one join query.
A general rule is that joins are faster in most cases (99%). The more data tables have, the subqueries are slower. The less data tables have, the subqueries have equivalent speed as joins.
Basically, join order DOES matter because if we can join two tables that will reduce the number of rows needed to be processed by subsequent steps, then our performance will improve.
The join
method is generally considered better, if only because it reduces the overhead of sending queries back and forth to the database.
If you have appropriate indexes on the tables, then the underlying performance of the two methods will be similar. That is, both methods will use appropriate indexes to fetch the results.
From a database perspective, the join
method is far superior. It consolidates the data logic in one place, making the code more transparent. It also allows the database to make optimizations that might not be apparent in application code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With