做项目,写oracle查询语句的时候,总是喜欢滥用distinct关键字。但后来发现,某些场合下,distinct的性能开销非常大。
with
diag_list as
(select distinct diagnosis_desc from medrec.diagnosis where diagnosis_desc is not null),
diag_dict as
(select distinct diagnosis_name,diagnosis_code from comm.diagnosis_dict where diagnosis_name is not null and diagnosis_code is not null)
select <span style="color:#ff0000;">distinct</span> t1.diagnosis_desc,t2.diagnosis_code,t2.diagnosis_name from diag_list t1,diag_dict t2
where INSTR(t1.diagnosis_desc,t2.diagnosis_name)>0;
红色的distinct是完全没有必要的。而且,这个distinct的性能开销极大。
有时间的时候,可以使用profiler之类的工具对不同场景下的distinct对性能的影响进行量化分析。这里先做个备忘。