SQL优化经典----利用分析函数优化自连接
2015-05-20 17:19
495 查看
SQL优化经典----利用分析函数优化自连接
----http://blog.sina.com.cn/s/blog_61cd89f60102efak.html这几天看到一个不错的SQL优化案例,读完感觉很受启发,在此记录一下优化思路了具体分析过程,对于SQL优化我们的目标是减少逻辑读、减少递归调用,我们来看一下
select distinct decode(length(a.category_id),
5,
decode(a.origin_type, 801, 888888, 999999),
a.category_id) category_id,
a.notice_code,
a.treat_status,
lr.real_name as receiver_name,
f.send_code,
f.policy_code,
g.real_name agent_name,
f.organ_id,
f.dept_id,
a.policy_id,
a.change_id,
a.case_id,
a.group_policy_id,
a.fee_id,
a.auth_id,
a.pay_id,
cancel_appoint.appoint_time cancel_appoint_time,
a.insert_time,
a.send_time,
a.end_time,
f.agency_code,
a.REPLY_TIME,
a.REPLY_EMP_ID,
a.FIRST_DUTY,
a.NEED_SEND_PRINT,
11 source
from t_policy_problem a,
t_policy f,
t_agent g,
t_letter_receiver lr,
t_problem_category pc,
t_policy_cancel_appoint cancel_appoint
where f.agent_id = g.agent_id(+)
and a.policy_id = f.policy_id(+)
and lr.main_receiver = 'Y'
and a.category_id = pc.category_id
and a.item_id = lr.item_id
and a.policy_id = cancel_appoint.policy_id(+)
And a.Item_Id = (Select Max(item_id)
From t_Policy_Problem
Where notice_code = a.notice_code)
and a.policy_id is not null
and a.notice_code is not null
and a.change_id is null
and a.case_id is null
and a.group_policy_id is null
and a.origin_type not in (801, 802)
and a.pay_id is null
and a.category_id not in
(130103, 130104, 130102, 140102, 140101)
and f.policy_type = 1
and (a.fee_id is null or
(a.fee_id is not null and a.origin_type = 701))
and f.organ_id in
(select distinct organ_id
from T_COMPANY_ORGAN
start with organ_id = '107'
connect by parent_id = prior organ_id)
and pc.NEED_PRITN = 'Y'
优化后:
WITH t_Policy_Problem_w AS
(SELECT tp.*, max(item_id) OVER(PARTITION BY notice_code) max_item_id
FROM t_Policy_Problem tp)
select distinct decode(length(a.category_id),
5,
decode(a.origin_type, 801, 888888, 999999),
a.category_id) category_id,
a.notice_code,
a.treat_status,
lr.real_name as receiver_name,
f.send_code,
f.policy_code,
g.real_name agent_name,
f.organ_id,
f.dept_id,
a.policy_id,
a.change_id,
a.case_id,
a.group_policy_id,
a.fee_id,
a.auth_id,
a.pay_id,
cancel_appoint.appoint_time cancel_appoint_time,
a.insert_time,
a.send_time,
a.end_time,
f.agency_code,
a.REPLY_TIME,
a.REPLY_EMP_ID,
a.FIRST_DUTY,
a.NEED_SEND_PRINT,
11 source
from t_Policy_Problem_w a,
t_policy f,
t_agent g,
t_letter_receiver lr,
t_problem_category pc,
t_policy_cancel_appoint cancel_appoint
where a.item_id = a.max_item_id
and f.agent_id = g.agent_id(+)
and a.policy_id = f.policy_id(+)
and lr.main_receiver = 'Y'
and a.category_id = pc.category_id
and a.item_id = lr.item_id
and a.policy_id = cancel_appoint.policy_id(+)
and a.policy_id is not null
and a.notice_code is not null
and a.change_id is null
and a.case_id is null
and a.group_policy_id is null
and a.origin_type not in (801, 802)
and a.pay_id is null
and a.category_id not in (130103, 130104, 130102, 140102, 140101)
and f.policy_type = 1
and (a.fee_id is null or (a.fee_id is not null and a.origin_type = 701))
and f.organ_id in (select distinct organ_id
from T_COMPANY_ORGAN
start with organ_id = '107'
connect by parent_id = prior organ_id)
and pc.NEED_PRITN = 'Y'
利用分析函数max() over(partition by )优化自连接,具体分析详见:
/article/1666854.html
相关文章推荐
- sql语句的优化分析之一查询语句中左连接和函数效率分析比较
- 利用分析函数优化自连接
- 利用分析函数优化自连接
- 利用分析函数优化自连接
- 28 分析函数优化自连接,减少表访问次数 --优化主题系列
- 怎样使用复合索引优化一个分析函数SQL
- SQL查询和优化(十四)——用分析函数优化标量子查询
- SQL优化经典案例----外连接where条件位置优化
- SQL优化技术分析-3:SQL语句索引的利用
- SQL优化经典案例----外连接where条件位置优化
- SQL Server 第四堂课,创建存储过程。存储过程是一组编译在单个执行计划中的transact-SQL语句。存储过程相当于C#函数,可以允许模块化程序设计,允许更快执行如果某操作需要大量transct-SQL代码或需要重复执行,将在创建存储过程中对其进行分析和优化。
- SQL优化--使用分析函数
- 【MySql性能优化二】利用explain进行查询和分析sql语句
- SQL优化--使用分析函数(转自:http://www.cnblogs.com/zping/archive/2008/08/06/1261973.html)
- SQL优化--使用分析函数
- mysql 利用explain优化sql
- SQL语句优化技术分析
- DB Optimizer:数据库SQL代码分析调试优化,数据库存储过程优化,数据库负载测试
- 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
- SQL语句优化技术分析