對于distinct,groupby的性能俯画。
- 數(shù)據(jù)量非常巨大時候溉卓,比如1000萬中有300W重復(fù)數(shù)據(jù)铜异,這時候的distinct的效率略好于group by;
- 對于相對重復(fù)量較小的數(shù)據(jù)量比如1000萬中1萬的重復(fù)量庶喜,用groupby的性能會遠優(yōu)于distnct小腊。
- 簡書上的一篇博客說的不錯,大家可以穿送過去看一看傳送門
例如久窟、找出所有員工當前(to_date='9999-01-01')具體的薪水salary情況秩冈,對于相同的薪水只顯示一次,并按照逆序顯示
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
1.select salary
from salaries
where to_date='9999-01-01'
group by salary
order by salary desc71. select salary from salaries where to_date='9999-01-01' group by salary order by salary desc; - SELECT DISTINCT salary FROM salaries WHERE to_date = '9999-01-01' ORDER BY salary DESC