当前位置:网站首页>A SQL optimization case using order by and rownum
A SQL optimization case using order by and rownum
2022-06-22 22:38:00 【Tiger Liu】
This is a customer AWR One of the reports TOP SQL, execution time 8.8 Hours ( There is also implementation 12.7 An error is reported for hours to exit )
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;
Number of table records 280M, Return to where the conditions are met and press oper_time Sort of 500 Bar record .
See above sql monitor Displayed execution plan , Some people may ask questions :
Most records of the table are returned , Why not do a full table scan , Instead, a poor index scan is used ?
The answer is :
Because two system parameters related to the index have been modified , Indexed COST Greatly reduced , Let the optimizer think that index walking is always better than full table scanning :
optimizer_index_caching = 95( The default value is 0)
optimizer_index_cost_adj = 3( The default value is 100)
This SQL How do you optimize it ?
according to SQL monitor The information displayed and the actual number of records in the table , We can create oper_time Field to optimize :
create index idx_name on TI_BI_CAMPN_USERORDER_TEMP(oper_time);
It is expected that after the index is created , The SQL Of The execution time should be 1~2 About seconds .
Summary :
order by And rownum When used together , You can create predicate fields and order by Union index on field ( This example cannot be , Because there is or And asymmetric predicate conditions ); Or when most of the data meets the conditions , establish order by Index on field ( This case ), Avoid sorting large result sets .
Generally, it is not recommended to modify the system default parameters ( Memory parameters 、bug With the exception of ), Because many performance tests are done under the default parameters .
Other situations :
If this SQL Low execution frequency , You can also choose not to create an index , Use parallel and full table scanning to improve response speed , Use the following 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;
If the two system parameters of the index are the default values , Among them full hint It can be removed .
The currently used parallelism is 4, The specific parallelism can be increased or decreased according to the actual needs .
Be careful :11g The parallel writing method of does not require adding table names or aliases .10g Adding table names or aliases in is tedious and easy to omit , Throw it away !
If you have any comments 、 Suggest 、 You can communicate with tiger Liu .
Tiger Liu's articles are original , Welcome to forward .
边栏推荐
- MySQL functions
- How to use the data dictionary function in the low code platform of the Internet of things?
- 新捷途X70S上市8.79万起,空间安全越级,不愧是网红国民大7座SUV
- Reasons for the failure of digital transformation and the way to success
- 立體渲染
- Uniapp applet mall develops thinkphp6 points mall, group purchase and seckill packaged app
- Why do you perform performance tests before the software goes online? How to find a software performance testing organization
- R language data Table data import practice: data Rename the name of the table data column (rename)
- MySQL constraints
- 【ROS 入门学习 】CmakeList.txt 和Packages.xml释义
猜你喜欢

The method of making videos of knowledge payment system support m3u8 format playback
![[Li mu] how to read papers [intensive reading of papers]](/img/86/4894bdef31d47d3f9bf3206b997eed.jpg)
[Li mu] how to read papers [intensive reading of papers]

Eureka service registration and discovery
![[GWCTF 2019]mypassword XSS](/img/26/3611fd5aae21ea004dcfcc2c623328.png)
[GWCTF 2019]mypassword XSS

大不列颠泰迪熊加入PUBG 手游
![[icml2022] using virtual nodes to promote graph structure learning](/img/cc/8d009e3c073b4eeef090ac393b8199.png)
[icml2022] using virtual nodes to promote graph structure learning

自助图书馆系统-Tkinter界面和openpyxl表格综合设计案例

Summary of just meal with 900W increase in playback and acclaim from station B users

Reasons for the failure of digital transformation and the way to success

Seriously, the hang up of the kotlin collaboration process is not so mysterious (principle)
随机推荐
自助图书馆系统-Tkinter界面和openpyxl表格综合设计案例
How to use the data dictionary function in the low code platform of the Internet of things?
RapidEye快鸟、SPOT卫星遥感影像数据
Crud+ form verification for spa project development
Self service library system Tkinter interface and openpyxl form comprehensive design case
[icml2022] using virtual nodes to promote graph structure learning
Pycharm configuring remote connection server development environment
June25,2022 PMP Exam clearance manual-6
Eureka service registration and discovery
新捷途X70S上市8.79万起,空间安全越级,不愧是网红国民大7座SUV
322.零钱兑换
Mysql8 installation and environment configuration
Implementation of breadth traversal adjacency matrix of 6-6 graph
Which is the higher priority of V-IF or V-for?
≥ server2012r2 system, disable some planned tasks of the system
【论文解读】关于基于视觉无人机自主降落平台的论文梳理
Dragon City in Europe | National Geographic the most romantic and safe destination in the world
下一个排列[发挥主观能动性发现规律]
Generate detailed API and parameters of QR code using qrcodejs2
Task management of embedded development foundation (thread management)