Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distinct vs group by which is better

for the simplest case we all refer to:

select id from mytbl 
group by id

and

select distinct id from mytbl

as we know, they generate same query plan which had been repeatedly mentioned in some items like Which is better: Distinct or Group By

In hive, however, the former only has one reduce task while the latter has many.

From experiments, I founded that the GROUP BY is 10+ times faster than DISTINCT.

They are different.

So what I learned is:

GROUP-BY is anyway not worse than DISTINCT, and it is better sometimes.

I would like to know:

1. If this conclusion is true.

2. If true, I shall consider DISTINCT as a approach for logical convenience, but why DISTINCT doesn't take GROUP-BY's better implementation?

3. If false, I would be very eager to know its decent usage under big-data situation.

Thank you very much!!:)

like image 811
Chiron Avatar asked Aug 07 '15 11:08

Chiron


People also ask

Is distinct better than GROUP BY?

DISTINCT is used to filter unique records out of all records in the table. It removes the duplicate rows. SELECT DISTINCT will always be the same, or faster than a GROUP BY.

Why is GROUP BY better than distinct SQL?

GROUP BY lets you use aggregate functions, like AVG , MAX , MIN , SUM , and COUNT . On the other hand DISTINCT just removes duplicates. This will give you one row per department, containing the department name and the sum of all of the amount values in all rows for that department.

Which is better distinct or GROUP BY in Oracle?

DISTINCT implies you want a distinct set of columns. However, GROUP BY implies you want to compute some sort of aggregate value which you are not. It will take more time in your case.

Is GROUP BY faster than distinct hive?

In hive, however, the former only has one reduce task while the latter has many. From experiments, I founded that the GROUP BY is 10+ times faster than DISTINCT.


2 Answers

Your experience is interesting. I have not seen the single reducer effect for distinct versus group by. Perhaps there is some subtle difference in the optimizer between the two constructs.

A "famous" example in Hive is:

select count(distinct id) from mytbl; 

versus

select count(*) from (select distinct id       from mytbl      ) t; 

The former only uses one reducer and the latter operates in parallel. I have seen this both in my experience, and it is documented and discussed (for example, on slides 26 and 27 in this presentation). So, distinct can definitely take advantage of parallelism.

I imagine that as Hive matures, such problems will be fixed. However, it is ironic that Postgres has a similar performance issue with COUNT(DISTINCT), although I think the underlying reason is a little bit different.

like image 143
Gordon Linoff Avatar answered Sep 20 '22 02:09

Gordon Linoff


I take same task and analyze by postgres commands. For distinct query: explain (analyze) select distinct product_id, size from logistic.product_stock where status = 'STOCK' I get:

HashAggregate  (cost=2166.24..2232.35 rows=6611 width=23) (actual time=46.417..47.104 rows=3770 loops=1)
   Group Key: product_id, size
   Batches: 1  Memory Usage: 721kB
   ->  Seq Scan on product_stock  (cost=0.00..2050.57 rows=23133 width=23) (actual time=0.144..39.954 rows=22357 loops=1)
         Filter: ((status)::text = 'STOCK'::text)
         Rows Removed by Filter: 44930
 Planning Time: 0.126 ms
 Execution Time: 47.517 ms

For group by query explain (analyze) select product_id, size from logistic.product_stock where status = 'STOCK' group by product_id, size I get next:

HashAggregate  (cost=2166.24..2232.35 rows=6611 width=23) (actual time=40.519..41.273 rows=3775 loops=1)
   Group Key: product_id, size
   Batches: 1  Memory Usage: 721kB
   ->  Seq Scan on product_stock  (cost=0.00..2050.57 rows=23133 width=23) (actual time=0.053..34.159 rows=22362 loops=1)
         Filter: ((status)::text = 'STOCK'::text)
         Rows Removed by Filter: 44930
 Planning Time: 0.802 ms
 Execution Time: 41.768 ms

As we can see: steps are same in both situations. Seq scan -> Group key.

ANSWER: NO MATTER WHAT QUERY YOU CHOOSE

PS. time depends on cache.

like image 40
AlexRut Avatar answered Sep 20 '22 02:09

AlexRut