补充说明
对分页进行优化只能是第二种,第三种优化不能翻页。只能最小数是1
业务背景
公司报表查询,当时在测试库里面运行的时候,速度是很快的,在生产上运行速度很慢,当初以为生产上数据量太大的原因。后来发现是分页导致的查询速度慢。
优化介绍
百度上的oracle分页都需要嵌套两层,一层查询中带出rownum,最外层去掉rownum。
而百度上针对的优化也是在两层的基础上优化,相比以前是提高了效率。但是只用一整效率会更高。下面针对代码优化演示。
代码演示
正常的oracle分页查询
select id, name, sex, age, address, phone
from (
select id, name, sex, age, address, phone, rownum num
from student
) b
where b.num >= 1
and b.num <= 10
百度上的优化
select id, name, sex, age, address, phone
from (
select id, name, sex, age, address, phone, rownum num
from student
where rownum <= 10
) b
where b.num >= 1
第三种
这种优化不能分页,只能从1开始,
select id, name, sex, age, address, phone, rownum num
from student
where
rownum >= 1
and rownum <= 10
总结,根据上面的优化只是将范围缩小了一下而已,相当于两层循环,而第三种方法,相当于一层循环。
共同探讨学习技术创建技术氛围Day9884125