【转】sql语句优化原则
1.多where,少having
where用来过滤行,having用来过滤组
2.多union all,少union
union删除了重复的行,因此花费了一些时间
3.多Exists,少in
Exists只检查存在性,性能比in强很多,有些朋友不会用Exists,就举个例子
例,想要得到有电话号码的人的基本信息,table2有冗余信息
select * from table1;--(id,name,age)select * from table2;--(id,phone)in:select * from table1 t1 where t1.id in (select t2.id from table2 t2 where t1.id=t2.id);Exists:select * from table1 t1 where Exists (select 1 from table2 t2 where t1.id=t2.id);
insert into XXX(pk_id,column1) values(?,?);update XXX set column1=? where pk_id=?;delete from XXX where pk_id=?;select pk_id,column1 from XXX where pk_id=?;
sql1:select * from (select t.*,rownum rn from XXX t)where rn>0 and rn <10;sql2:select * from (select t.*,rownum rn from XXX t where rownum <10)where rn>0;乍看一下没什么区别,实际上区别很大...125万条数据测试,