Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query - cost of using DISTINCT

Does the use of DISTINCT have a lot of influence on the query cost of a SQL query's execution plan?

On a table with 100,000 rows, what difference will there be between:

select * from TABLE

and 

select distinct(*) from TABLE 

in term of milliseconds? 

like image 929
Gaetano Piazzolla Avatar asked Jan 13 '15 19:01

Gaetano Piazzolla


1 Answers

Generally, the answer is many milliseconds. The select distinct is equivalent to doing a group by on all the columns. It is unlikely that you have an index on all the columns, so an index will not generally be very useful.

Some database engines might be smart enough to detect if one of the columns is declared distinct or a primary key. In that case, there might be an engine that optimizes the query.

But, in general, you should avoid using select distinct unless you need to.

like image 116
Gordon Linoff Avatar answered Nov 17 '22 06:11

Gordon Linoff