子查询有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,执行 ...
随机推荐
- win7下自写驱动导致开机蓝屏调试过程
之前没有接触过驱动调试.这里上手就要解决一个因为某个自定义驱动导致的系统登陆后蓝屏问题,记录下来. 问题: 从客户那边弄来的一个虚拟机,已知是加了我们的驱动之后才会导致蓝屏. 解决过程: 使用 ...
- 算法导论(第三版)Exercises2.3(归并排序、二分查找、计算集合中是否有和为X的2个元素)
2.3-1: 3 9 26 38 41 49 52 59 3 26 41 52 9 38 49 57 3 41 52 26 38 57 9 49 3 41 52 26 38 ...
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- 【C#基础】HTTP发送POST二进制数据
//postdata为数组的请求方式 public byte[] POST(string Url, byte[] byteRequest) { byte[] responsebody; HttpWeb ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
- MC, MCMC, Gibbs採样 原理&实现(in R)
本文用讲一下指定分布的随机抽样方法:MC(Monte Carlo), MC(Markov Chain), MCMC(Markov Chain Monte Carlo)的基本原理,并用R语言实现了几个样 ...
- pd的django个人博客教程----1:效果展示等
开发环境同to do list 1:首页:localhost/pd/ 2:导航栏测试或者生活点入: 测试:localhost/category/?cid=1 3:点击文章后进入文章显示页面 e.g:进 ...
- 添加序号列(SQL Server)
SELECT ROW_NUMBER() OVER (ORDER BY 实际缴费金额 ) AS A, --序号 RANK() OVER (ORDER BY 实际缴费金额 ) AS B, --相同跳过从新 ...
- C# List<T>转为 DataTable
// remove "this" if not on C# 3.0 / .NET 3.5 public static System.Data.DataTable ConvertTo ...
- [c#]asp.net开发微信公众平台(8)微信9大高级接口,自定义菜单
前7篇把最基础的消息接收和回复全做完了, 也把高级接口的入口和分拆处理写好了空方法, 此篇接着介绍微信的9大高级接口, 并着重讲解其中的自定义菜单. 微信9大接口为: 1.语音识别接口 2.客服接 ...