当前位置:网站首页>使用 Order by 与 rownum SQL 优化案例一则
使用 Order by 与 rownum SQL 优化案例一则
2022-06-22 18:24:00 【老虎刘】
这是某客户AWR报告中的一个TOP SQL,执行时间8.8小时(也有执行12.7小时报错退出的情况)
SELECT *
FROM
(
SELECT rownum num ,t.*
FROM TI_BI_CAMPN_USERORDER_TEMP t
WHERE oper_type = '0' OR oper_type = '2' OR
(oper_type = '1' AND end_date <= add_months(trunc(sysdate, 'mm'), 1))
ORDER BY oper_time ASC
) c
WHERE c.num <= : 1;
表记录数280M,返回其中符合条件并按oper_time排序的500条记录。
看到上面sql monitor显示的执行计划,可能有人会生出疑问:
返回了表的大部分记录,为什么不做全表扫描,而是使用了比较差的索引扫描?
答案是:
因为修改了索引相关的两个系统参数,把索引的COST大大的降低了,让优化器认为走索引总是比全表扫描要好:
optimizer_index_caching = 95(默认值0)
optimizer_index_cost_adj = 3(默认值100)
这个SQL如何优化呢?
根据SQL monitor显示的信息及表的实际记录数,我们可以通过创建oper_time字段上的索引来优化:
create index idx_name on TI_BI_CAMPN_USERORDER_TEMP(oper_time);
预计创建索引后,该SQL的执行时间应该在1~2秒左右。
小结:
order by 与 rownum 搭配使用时,可以创建谓词字段与order by字段上的联合索引(本例不可,因为有or和不对称的谓词条件);或者在大部分数据都符合条件的情况下,创建order by字段上的索引(本例),避免大结果集的排序。
系统默认参数一般不建议修改(内存参数、bug除外),因为很多性能测试是在默认参数条件下完成。
其他情况:
如果这个SQL执行频率低,也可以选择不创建索引,使用并行加全表扫描来提高响应速度,使用下面的hint:
SELECT *
FROM
(
SELECT /*+ full(t) parallel(4) */rownum num ,t.*
FROM TI_BI_CAMPN_USERORDER_TEMP t
WHERE oper_type = '0' OR oper_type = '2' OR
(oper_type = '1' AND end_date <= add_months(trunc(sysdate, 'mm'), 1))
ORDER BY oper_time ASC
) c
WHERE c.num <= : 1;
如果索引那两个系统参数是默认值,其中的full hint是可以去掉的。
当前使用的并行度是4,具体的并行度可以根据实际需要适当增减。
注意:11g的并行写法已经不要求加表名或别名。10g中加表名或别名的写法繁琐而且容易遗漏,抛弃了吧!
各位网友如果有什么意见、建议、问题都可以与老虎刘沟通。
老虎刘的文章都是原创,欢迎大家转发。
边栏推荐
- 华为云招募工业智能领域合作伙伴,强力扶持+商业变现
- Follow up course supplement of little turtle teacher "take you to learn C and take you to fly"
- Service实战:使用Service完成一个下载任务
- 推荐一个解剖学网站
- Activereports report practical application tutorial (19) -- multi data source binding
- 将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT
- AttributeError: ‘KeyedVectors‘ object has no attribute ‘wv‘
- 创建者模式大汇总
- c# winform 嵌入flash
- shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)
猜你喜欢

常用技术注解

Detailed explanation of session mechanism and related applications of session

创建者模式大汇总

如何用银灿IS903主控DIY自己的U盘?(练习BGA焊接的好项目)

Digital supply chain centralized purchase platform solution for mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

Solution de pin hors grille dans altium designer

详解openGauss多线程架构启动过程

Adapter mode of structural mode

Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)

AB打包有的Shader没有触发IPreprocessShaders的回调
随机推荐
从11小时到25秒--还有优化空间吗?
In the first half of the year, there were 7 new unicorns in this field, and the capital scrambled to enter the market
calendar控件编程
修改隐含参数造成SQL性能下降案例之一
mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
Input two strings and output the longest substring with the same length
在线生成占位图片
Altium Designer中off grid pin解决方法
推荐一个解剖学网站
Paopao Mart: empty souls need stories
Online generation of placeholder pictures
Solution de pin hors grille dans altium designer
Shell script explanation (VII) -- regular expression, sort, uniq, tr
NAND闪存(NAND Flash)颗粒SLC,MLC,TLC,QLC的对比
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
Pat a 1093 count Pat's (25 points)
Pull down refresh and pull up to load more listviews
Service实战:使用Service完成一个下载任务
SSH password free login
Screw数据库文档生成器