Using SUM() in the SELECT is better or in the SUB-

2019-09-11 20:09发布

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?

3条回答
叼着烟拽天下
2楼-- · 2019-09-11 20:30

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-09-11 20:38

Presumably, you intend for the first query to be:

select t1.*, sum(t2.value) as total_votes
from table1 t1 left join
     table2 t2
     on t1.id = t2.post_id
group by t1.id;

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 the join 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:

select t1.*, 
       (select sum(t2.value)
        from table2 t2
        where t1.id = t2.post_id
       ) as total_votes
from table1 t1;

This is particularly effective when you have where and/or order by clauses that only contain references to table1.

查看更多
我只想做你的唯一
4楼-- · 2019-09-11 20:41

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

查看更多
登录 后发表回答