子查询有OR无法展开,改写成union
SELECT A.*
FROM (SELECT CD.*,
nvl(CV.SUM_CI_BALANCE, 0) as SUM_CI_BALANCE,
nvl(CV.SUM_LN_BALANCE, 0) as SUM_LN_BALANCE
FROM OCRM_F_CI_CUST_DESC CD
left join (select cust_id,
FR_ID,
sum(CI_BALANCE) as sum_CI_BALANCE,
sum(LN_BALANCE) as sum_LN_BALANCE
from OCRM_F_CI_CUST_VIEW
where 1 = 1
and FR_ID = '15601'
and (MGR_ID = '01092' OR
MGR_ID IN
(SELECT USER_ID
FROM ADMIN_AUTH_MANAGE_ACCOUNT
WHERE MANAGE_ID = '01092'))
group by cust_id, FR_ID) CV
on CD.Cust_Id = CV.cust_id
and CD.Fr_Id = CV.fr_id
WHERE 1 = 1
and CD.FR_ID = '15601') A
where 1 = 1
AND ((EXISTS
(SELECT 1
FROM OCRM_F_CI_BELONG_CUSTMGR MGR
WHERE MGR.CUST_ID = A.CUST_ID
AND (MGR.MGR_ID = '01092' OR
MGR.MGR_ID IN (SELECT USER_ID
FROM ADMIN_AUTH_MANAGE_ACCOUNT
WHERE MANAGE_ID = '01092')))))
ORDER BY to_number(SUM_CI_BALANCE) DESC, to_number(SUM_LN_BALANCE) DESC; Plan hash value: 4232450453
---------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 48729 | 11M| | 10916 (1)| 00:02:11 |
| 1 | SORT ORDER BY | | 48729 | 11M| 12M| 10916 (1)| 00:02:11 |
|* 2 | HASH JOIN RIGHT OUTER | | 48729 | 11M| | 8242 (1)| 00:01:39 |
| 3 | VIEW | | 4483 | 227K| | 2148 (1)| 00:00:26 |
| 4 | HASH GROUP BY | | 4483 | 170K| 4944K| 2148 (1)| 00:00:26 |
|* 5 | FILTER | | | | | | |
| 6 | TABLE ACCESS BY INDEX ROWID| OCRM_F_CI_CUST_VIEW | 89357 | 3403K| | 1428 (1)| 00:00:18 |
|* 7 | INDEX RANGE SCAN | IDX_OCRM_F_CI_CUST_VIEW2 | 89357 | | | 215 (1)| 00:00:03 |
|* 8 | INDEX RANGE SCAN | ADMIN_AUTH_MANAGE_ACCOUNT_IDX2 | 1 | 36 | | 1 (0)| 00:00:01 |
|* 9 | HASH JOIN RIGHT SEMI | | 48729 | 9612K| | 6094 (1)| 00:01:14 |
| 10 | VIEW | VW_SQ_1 | 48655 | 950K| | 1380 (1)| 00:00:17 |
|* 11 | FILTER | | | | | | |
| 12 | INDEX FAST FULL SCAN | IDX_CI_BELONG_CUSTMGR | 969K| 24M| | 1380 (1)| 00:00:17 |
|* 13 | INDEX RANGE SCAN | ADMIN_AUTH_MANAGE_ACCOUNT_IDX2 | 1 | 36 | | 1 (0)| 00:00:01 |
|* 14 | TABLE ACCESS FULL | OCRM_F_CI_CUST_DESC | 60233 | 10M| | 4714 (1)| 00:00:57 |
---------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("CD"."FR_ID"="CV"."FR_ID"(+) AND "CD"."CUST_ID"="CV"."CUST_ID"(+))
5 - filter("MGR_ID"='01092' OR EXISTS (SELECT 0 FROM "ADMIN_AUTH_MANAGE_ACCOUNT" "ADMIN_AUTH_MANAGE_ACCOUNT"
WHERE "MANAGE_ID"='01092' AND "USER_ID"=:B1))
7 - access("FR_ID"='15601')
8 - access("USER_ID"=:B1 AND "MANAGE_ID"='01092')
9 - access("ITEM_1"="CD"."CUST_ID")
11 - filter("MGR"."MGR_ID"='01092' OR EXISTS (SELECT 0 FROM "ADMIN_AUTH_MANAGE_ACCOUNT"
"ADMIN_AUTH_MANAGE_ACCOUNT" WHERE "MANAGE_ID"='01092' AND "USER_ID"=:B1))
13 - access("USER_ID"=:B1 AND "MANAGE_ID"='01092')
14 - filter("CD"."FR_ID"='15601') 由于子查询里有OR,导致子查询无法展开,改写后:
SELECT A.*
FROM (SELECT CD.*,
nvl(CV.SUM_CI_BALANCE, 0) as SUM_CI_BALANCE,
nvl(CV.SUM_LN_BALANCE, 0) as SUM_LN_BALANCE
FROM OCRM_F_CI_CUST_DESC CD
left join (
select cust_id,
FR_ID,
sum(CI_BALANCE) as sum_CI_BALANCE,
sum(LN_BALANCE) as sum_LN_BALANCE
from OCRM_F_CI_CUST_VIEW
where 1 = 1
and FR_ID = '15601'
and MGR_ID = '01092'
group by cust_id, FR_ID
union
select cust_id,
FR_ID,
sum(CI_BALANCE) as sum_CI_BALANCE,
sum(LN_BALANCE) as sum_LN_BALANCE
from OCRM_F_CI_CUST_VIEW
where 1 = 1
and FR_ID = '15601'
and MGR_ID IN
(SELECT USER_ID
FROM ADMIN_AUTH_MANAGE_ACCOUNT
WHERE MANAGE_ID = '01092')
group by cust_id, FR_ID) CV
on CD.Cust_Id = CV.cust_id
and CD.Fr_Id = CV.fr_id
WHERE 1 = 1
and CD.FR_ID = '15601') A
where 1 = 1
AND ((EXISTS
(SELECT 1
FROM OCRM_F_CI_BELONG_CUSTMGR MGR
WHERE MGR.CUST_ID = A.CUST_ID
AND MGR.MGR_ID = '01092'
union
(SELECT 1
FROM OCRM_F_CI_BELONG_CUSTMGR MGR
WHERE MGR.CUST_ID = A.CUST_ID
and MGR.MGR_ID IN (SELECT USER_ID
FROM ADMIN_AUTH_MANAGE_ACCOUNT
WHERE MANAGE_ID = '01092')))
))
ORDER BY to_number(SUM_CI_BALANCE) DESC, to_number(SUM_LN_BALANCE) DESC;
子查询有OR无法展开,改写成union的更多相关文章
- SQL Server温故系列(3):SQL 子查询 & 公用表表达式 CTE
1.子查询 Subqueries 1.1.单行子查询 1.2.多行子查询 1.3.相关子查询 1.4.嵌套子查询 1.5.子查询小结及性能问题 2.公用表表达式 CTE 2.1.普通公用表表达式 2. ...
- 子查询解嵌套in改写为exists
SELECT * FROM (SELECT pubformdat0_.id id332_, pubformdat0_.domain_id domain2_332_, pubformdat0_.proc ...
- SQL查询优化思维即SQL子查询
一. 什么叫子查询 定义及分类 子查询又称内部查询,而包含子查询的语句称之外部查询(又称主查询).所有的子查询可以分为两类,即相关子查询和非相关子查询. 非相关子查询是独立于外部查询的子查询,子查询总 ...
- 分页语句where条件中的子查询有or关键字优化
背景 开发说: 有段SQL语句,去掉order by很快,有order by之后,查询1小时都无法返回结果. 我叫他把SQL扔给我看下. SQL代码及执行计划 select * from (selec ...
- 子查询解嵌套not in 无法展开改写
SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...
- MySQL改写子查询成Join
有时用别的方式而不是子查询可以获得更高的性能 : For example: SELECT * FROM t1 WHERE id IN (SELECT id FROM t2); 改写: SELECT D ...
- 标量子查询SQL改写
一网友说下面sql跑的好慢,让我看看 sql代码: select er, cid, pid, tbl, zs, sy, (select count(sr.mobile_tele_no) from tb ...
- SQL中带有NOT IN 子查询改写
报表程序中的一段SQL运行很慢,代码如下: 优化前: 耗时:1337s INSERT INTO PER_LTE_ZIB_PB_COMMISSION_07 SELECT P.TOPACTUALID, Q ...
- 优化系列 | DELETE子查询改写优化
0.导读 有个采用子查询的DELETE执行得非常慢,改写成SELECT后执行却很快,最后把这个子查询DELETE改写成JOIN优化过程 1.问题描述 朋友遇到一个怪事,一个用子查询的DELETE,执行 ...
随机推荐
- mirantis fuel
使用mirantis公司的openstack部署工具--fuel,迅速部署体验openStack云管理软件; 1,使用virtualbox虚拟化工具(此工具在此使用主要是为了简化新建虚机的网络规划,f ...
- [置顶] 如何运行用记事本写的java程序
今天用记事本写了一个java程序,测试能运行,现在把它分解成几个步骤,利于大家理解: 1. 新建一个记事本,后缀名是 .java :然后在里面写一段jav ...
- JavaScript 对象属性的遍历
ES6一共有5种方法可以遍历对象的属性. (1)for...in for...in循环遍历对象自身的和继承的可枚举属性(不含Symbol属性). (2)Object.keys(obj) Object. ...
- AngularJs学习笔记6——四大特性之依赖注入
压缩工具:YUI-compressor 为了优化网页功能,对一些脚本文件进行压缩,比如:删除所有的注释和空格等,简化形参.但是AngularJs模块中可以声明多种组件,如控制器.指令.过滤器.服务等. ...
- centos虚拟机NAT静态IP设置
宿主机为Centos6.3 64位,三台虚拟机为为Centos6.3 64位.虚拟机的网络连接方式为默认的NAT方式.虚拟机默认为DHCP方式动态获取IP.为了在三台虚拟机上搭建hadoop,需要将这 ...
- 使用带Arduino IDE & WIZ820io的ATmega1284P
使用带Arduino IDE & WIZ820io的ATmega1284P 2013/07/04 | Filed under: IO模块 and tagged with: arduin ...
- openwrt上网配置的一些理解(二)
上一篇里面遇到了只能静态上网的问题,动态不行.所以再接再励,问题总是要解决的,偷懒的下场就是一直停留在菜鸟的水平. 首先分析下问题,要动态上网,首先我要明确不是动态获取不了IP,是获取了,上不了外网. ...
- Python-xml解析常用方法简介
[XML几种解析方法] 常见的XML编程接口有DOM和SAX,这两种接口处理XML文件的方式不同,使用场合自然也就不同. Python有三种方法解析XML: SAX,DOM,以及ElementTree ...
- [A Top-Down Approach][第二章 应用层]
[A Top-Down Approach][第二章 应用层] 标签(空格分隔): 未分类 网络应用是计算机网络存在的理由 首先从定义几个关键的应用层概念开始 应用程序所需要的网络服务,客户和服务器,进 ...
- Solr的安装
1. JDK要求 Solr 4.10 要求JDK版本必须是1.7或更高. 下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/ 下载得到z ...