I have two query with the same result, now I want to know which one is more optimized?
In the SELECT:
select t1.*, sum(t2.value) as total_votes
from table1 t1
left join table2 t2
on t1.id = t2.post_id
In the SUB-SELECT:
select t1.*, t3.total_votes
from table1 t1
left join (select post_id, sum(value) as total_votes from table2 t2 group by post_id) t3
on t1.id = t3.post_id
It should be noted, I have two table: table1(posts), table2(votes). And the two above query are for calculating total votes for each post.
So, which one is faster and more optimized?
Normally first is better, but it will be depend on table size and your conditions those are filtering data.
If you are able to filter data in sub-query then it can be fast but in your case it seems first query will be better as you are not doing much data filteration in your sub-query.
Note: If you want to get just common results then should use normal join instead of left join as you can get performance.
Presumably, you intend for the first query to be:
There is no right answer to your question, other than checking the performance on both queries and the data. Why? You have two competing things going on: optimizing the join and optimizing the aggregation.
The first query can use indexes for the join -- which makes that much more efficient. However, there may be lots of data for the aggregation, which is a problem.
The second can do the aggregation -- potentially on very little data (imagine that
t2
has one row) -- but then thejoin
may not be able to make full use of an index. Other databases have more powerful algorithms for joins and aggregations. But in MySQL, you really need to test.There is a third version that can often work better than either of these:
This is particularly effective when you have
where
and/ororder by
clauses that only contain references totable1
.If you want know whitch query is more optimized, you can use explain command
like 'explain select t1.*, sum(t2.value) as total_votes from table1 t1 left join table2 t2 on t1.id = t2.post_id'
explain command is show how MySQL would execute a query.
if you know about this query command show this page
http://dev.mysql.com/doc/refman/5.6/en/explain.html