这是另一个网友发的sql,这个有点儿坑
SYS@netdb1(netdb61)>select l_target.*, rownum as rn 2 FROM (select l0.l0_id as l0Id, 3 l0.date1 as createDate, 4 l0.id1 as applyId, 5 bai.stitle as applyTitle, 6 bai.amt1 as applyAmt, 7 bai.rate1 as applyRatio, 8 l0.value1 as oriPri, 9 l0.value2 as leftAmt, 10 sum(case 11 when lpi.status != '1' then 12 1 13 else 14 0 15 end) as remPeriods, 16 l0.flag1 AS flag2, 17 curi.s_cnname AS name2, 18 curi.i_user_id AS sellUserId 19 from l0 20 left join lpp 21 on lpp.l0_id = l0.l0_id 22 left join lpi0 lpi 23 on lpi.id3 = lpp.id3 24 left join b_apply_info bai 25 on bai.i_id1 = l0.id1 26 left join p_borrower_product pbp0 27 on pbp0.id1 = l0.id1 28 left join p_product pp0 29 on pbp0.id2 = pp0.id2 30 INNER JOIN c_user_regist_info curi 31 ON l0.creditor_user_id = curi.i_user_id 32 where 1 = 1 33 and l0.creditor_user_id = 5263 34 and l0.status != '2' 35 AND l0.flag1 = '1' 36 and pp0.product_type = 1 37 group by l0.l0_id, 38 l0.value1, 39 l0.value2, 40 l0.id1, 41 l0.date1, 42 bai.stitle, 43 bai.amt1, 44 bai.rate1, 45 l0.flag1, 46 curi.s_cnname, 47 curi.i_user_id) l_target 48 inner join (select l2.l0_id, 49 l2.creditor_user_id, 50 lpi0.PERIOD_NUM, 51 lpi0.period_end_time 52 from l0 l2 53 left join lpp0 54 on l2.l0_id = lpp0.l0_id 55 left join lpi0 lpi0 56 on lpp0.id3 = lpi0.id3 57 left join arda0 58 on arda0.id3 = lpi0.id3 59 where l2.creditor_user_id =5263 60 and l2.status != '2' 61 and lpi0.period_start_time <= sysdate 62 and lpi0.period_end_time > sysdate 63 group by l2.l0_id, 64 lpi0.PERIOD_NUM, 65 l2.creditor_user_id, 66 lpi0.period_end_time 67 having sum(arda0.REPAYMENT_AMT) = 0 or sum(arda0.REPAYMENT_AMT) is null) l_cur 68 on l_cur.l0_id = l_target.l0Id 69 left join (select lpp0.l0_id, lpp0.status, lpp0.PERIOD 70 from lpp0 71 inner join (select l2.l0_id, 72 l2.creditor_user_id, 73 lpi0.PERIOD_NUM, 74 lpi0.period_end_time 75 from l0 l2 76 left join lpp0 77 on l2.l0_id = lpp0.l0_id 78 left join lpi0 lpi0 79 on lpp0.id3 = 80 lpi0.id3 81 left join arda0 82 on arda0.id3 = 83 lpi0.id3 84 where l2.creditor_user_id =5263 85 and l2.status != '2' 86 and lpi0.period_start_time <= sysdate 87 and lpi0.period_end_time > sysdate 88 group by l2.l0_id, 89 lpi0.PERIOD_NUM, 90 l2.creditor_user_id, 91 lpi0.period_end_time 92 having sum(arda0.REPAYMENT_AMT) = 0 or sum(arda0.REPAYMENT_AMT) is null) lc 93 on lc.l0_id = lpp0.l0_id 94 where lc.PERIOD_NUM = lpp0.PERIOD + 1) l_pre 95 on l_pre.l0_id = l_target.l0Id 96 left join (select ttla.SELLER_l0_ID, 97 count(*) t_cnt, 98 max(transferable) transferable 99 from tla_transfer_l0_apply ttla 100 where 1 = 1 101 AND ttla.SELLER_ID =5263 102 group by ttla.SELLER_l0_ID) l_tran 103 on l_tran.SELLER_l0_ID = l_target.l0Id 104 left join (select * 105 from tla_transfer_l0_apply 106 where transferable = '1') ttla1 107 on ttla1.seller_l0_id = l_target.l0Id 108 where 1 = 1 109 and (l_tran.transferable is null or l_tran.transferable != '1') 110 and applyRatio=12 111 ORDER by leftAmt desc; 步惊云 14:48:54 ----------------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 594 | 15752 (3)| 00:03:10 | | 1 | SORT ORDER BY | | 2 | 594 | 15752 (3)| 00:03:10 | | 2 | COUNT | | | | | | |* 3 | FILTER | | | | | | |* 4 | HASH JOIN OUTER | | 2 | 594 | 15751 (3)| 00:03:10 | |* 5 | HASH JOIN OUTER | | 2 | 562 | 15734 (3)| 00:03:09 | |* 6 | HASH JOIN OUTER | | 2 | 536 | 7909 (3)| 00:01:35 | | 7 | NESTED LOOPS | | 2 | 504 | 7893 (3)| 00:01:35 | | 8 | VIEW | | 1 | 13 | 4152 (3)| 00:00:50 | |* 9 | FILTER | | | | | | | 10 | HASH GROUP BY | | 1 | 125 | 4152 (3)| 00:00:50 | |* 11 | HASH JOIN OUTER | | 5494 | 670K| 4151 (3)| 00:00:50 | | 12 | NESTED LOOPS | | | | | | | 13 | NESTED LOOPS | | 3010 | 291K| 3896 (3)| 00:00:47 | |* 14 | HASH JOIN | | 110K| 5949K| 3875 (3)| 00:00:47 | |* 15 | TABLE ACCESS FULL | l0 | 278 | 8062 | 168 (2)| 00:00:03 | | 16 | TABLE ACCESS FULL | lpp | 1530K| 37M| 3691 (2)| 00:00:45 | |* 17 | INDEX UNIQUE SCAN | PK_lpi0 | 1 | | 0 (0)| 00:00:01 | |* 18 | TABLE ACCESS BY INDEX ROWID | lpi0 | 1 | 44 | 0 (0)| 00:00:01 | | 19 | TABLE ACCESS FULL | arda0 | 131K| 3331K| 253 (2)| 00:00:04 | | 20 | VIEW PUSHED PREDICATE | | 1 | 239 | 3741 (2)| 00:00:45 | | 21 | SORT GROUP BY | | 1 | 225 | 3741 (2)| 00:00:45 | | 22 | NESTED LOOPS OUTER | | 1 | 225 | 3741 (2)| 00:00:45 | |* 23 | HASH JOIN OUTER | | 1 | 209 | 3741 (2)| 00:00:45 | | 24 | NESTED LOOPS | | | | | | | 25 | NESTED LOOPS | | 1 | 183 | 79 (3)| 00:00:01 | | 26 | NESTED LOOPS | | 4 | 628 | 79 (3)| 00:00:01 | | 27 | NESTED LOOPS | | 1 | 131 | 3 (0)| 00:00:01 | | 28 | NESTED LOOPS | | 1 | 95 | 2 (0)| 00:00:01 | | 29 | TABLE ACCESS BY INDEX ROWID| C_USER_REGIST_INFO | 1 | 15 | 2 (0)| 00:00:01 | |* 30 | INDEX UNIQUE SCAN | PK_C_USER_REGIST_INFO | 1 | | 1 (0)| 00:00:01 | |* 31 | TABLE ACCESS BY INDEX ROWID| l0 | 1 | 80 | 0 (0)| 00:00:01 | |* 32 | INDEX UNIQUE SCAN | PK_l0 | 1 | | 0 (0)| 00:00:01 | |* 33 | TABLE ACCESS BY INDEX ROWID | B_APPLY_INFO | 203 | 7308 | 1 (0)| 00:00:01 | |* 34 | INDEX UNIQUE SCAN | PK_B_APPLY_INFO | 1 | | 0 (0)| 00:00:01 | |* 35 | TABLE ACCESS FULL | P_BORROWER_PRODUCT | 4 | 104 | 76 (3)| 00:00:01 | |* 36 | INDEX UNIQUE SCAN | PK_P_PRODUCT | 1 | | 0 (0)| 00:00:01 | |* 37 | TABLE ACCESS BY INDEX ROWID | P_PRODUCT | 1 | 26 | 0 (0)| 00:00:01 | |* 38 | TABLE ACCESS FULL | lpp | 399 | 10374 | 3662 (2)| 00:00:44 | | 39 | TABLE ACCESS BY INDEX ROWID | lpi0 | 1 | 16 | 0 (0)| 00:00:01 | |* 40 | INDEX UNIQUE SCAN | PK_lpi0 | 1 | | 0 (0)| 00:00:01 | |* 41 | TABLE ACCESS FULL | TLA_TRANSFER_l0_APPLY | 2 | 32 | 15 (0)| 00:00:01 | | 42 | VIEW | | 4 | 52 | 7825 (3)| 00:01:34 | |* 43 | HASH JOIN | | 4 | 208 | 7825 (3)| 00:01:34 | | 44 | VIEW | | 1 | 26 | 4152 (3)| 00:00:50 | |* 45 | FILTER | | | | | | | 46 | HASH GROUP BY | | 1 | 125 | 4152 (3)| 00:00:50 | |* 47 | HASH JOIN OUTER | | 5494 | 670K| 4151 (3)| 00:00:50 | | 48 | NESTED LOOPS | | | | | | | 49 | NESTED LOOPS | | 3010 | 291K| 3896 (3)| 00:00:47 | |* 50 | HASH JOIN | | 110K| 5949K| 3875 (3)| 00:00:47 | |* 51 | TABLE ACCESS FULL | l0 | 278 | 8062 | 168 (2)| 00:00:03 | | 52 | TABLE ACCESS FULL | lpp | 1530K| 37M| 3691 (2)| 00:00:45 | |* 53 | INDEX UNIQUE SCAN | PK_lpi0 | 1 | | 0 (0)| 00:00:01 | |* 54 | TABLE ACCESS BY INDEX ROWID | lpi0 | 1 | 44 | 0 (0)| 00:00:01 | | 55 | TABLE ACCESS FULL | arda0 | 131K| 3331K| 253 (2)| 00:00:04 | | 56 | TABLE ACCESS FULL | lpp | 1530K| 37M| 3657 (2)| 00:00:44 | | 57 | VIEW | | 86 | 1376 | 16 (7)| 00:00:01 | | 58 | HASH GROUP BY | | 86 | 2494 | 16 (7)| 00:00:01 | |* 59 | TABLE ACCESS FULL | TLA_TRANSFER_l0_APPLY | 86 | 2494 | 15 (0)| 00:00:01 | ----------------------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter("L_TRAN"."TRANSFERABLE" IS NULL OR "L_TRAN"."TRANSFERABLE"<>'1') 4 - access("L_TRAN"."SELLER_l0_ID"(+)="L_TARGET"."l0ID") 5 - access("L_PRE"."l0_ID"(+)="L_TARGET"."l0ID") 6 - access("TLA_TRANSFER_l0_APPLY"."SELLER_l0_ID"(+)="L_TARGET"."l0ID") 9 - filter(SUM("ARDA0"."REPAYMENT_AMT")=0 OR SUM("ARDA0"."REPAYMENT_AMT") IS NULL) 11 - access("ARDA0"."id3"(+)="LPI0"."id3") 14 - access("L2"."l0_ID"="LPP0"."l0_ID") 15 - filter("L2"."CREDITOR_USER_ID"=5263 AND "L2"."STATUS"<>'2') 17 - access("LPP0"."id3"="LPI0"."id3") 18 - filter("LPI0"."PERIOD_START_TIME"<=SYSDATE@! AND "LPI0"."PERIOD_END_TIME">SYSDATE@!) 23 - access("LPP"."l0_ID"(+)="L0"."l0_ID") 30 - access("CURI"."I_USER_ID"=5263) 31 - filter("L0"."CREDITOR_USER_ID"=5263 AND "L0"."flag1"='1' AND "L0"."STATUS"<>'2') 32 - access("L0"."l0_ID"="L_CUR"."l0_ID") 33 - filter("BAI"."rate1"=12) 34 - access("BAI"."I_id1"="L0"."id1") 35 - filter("PBP0"."id1" IS NOT NULL AND "PBP0"."id1"="L0"."id1") 36 - access("PBP0"."id2"="PP0"."id2") 37 - filter("PP0"."PRODUCT_TYPE"=1) 38 - filter("LPP"."l0_ID"(+)="L_CUR"."l0_ID") 40 - access("LPI"."id3"(+)="LPP"."id3") 41 - filter("TRANSFERABLE"(+)='1') 43 - access("LC"."PERIOD_NUM"="LPP0"."PERIOD"+1 AND "LC"."l0_ID"="LPP0"."l0_ID") 45 - filter(SUM("ARDA0"."REPAYMENT_AMT")=0 OR SUM("ARDA0"."REPAYMENT_AMT") IS NULL) 47 - access("ARDA0"."id3"(+)="LPI0"."id3") 50 - access("L2"."l0_ID"="LPP0"."l0_ID") 51 - filter("L2"."CREDITOR_USER_ID"=5263 AND "L2"."STATUS"<>'2') 53 - access("LPP0"."id3"="LPI0"."id3") 54 - filter("LPI0"."PERIOD_START_TIME"<=SYSDATE@! AND "LPI0"."PERIOD_END_TIME">SYSDATE@!) 59 - filter("TTLA"."SELLER_ID"=5263) Note ----- - dynamic sampling used for this statement (level=2) Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 541 recursive calls 0 db block gets 2088099 consistent gets 234 physical reads 2264 redo size 11549 bytes sent via SQL*Net to client 619 bytes received via SQL*Net from client 11 SQL*Net roundtrips to/from client 359 sorts (memory) 0 sorts (disk) 147 rows processed 14:51:34 嗯 在lpp表的l0_ID 搞个索引。执行时间大约4秒多
WITH l_cur AS (SELECT l2.l0_id, l2.creditor_user_id, lpi0.period_num, lpi0.period_end_time FROM l0 l2 LEFT JOIN lpp0 ON l2.l0_id = lpp0.l0_id LEFT JOIN lpi0 lpi0 ON lpp0.id3 = lpi0.id3 LEFT JOIN arda0 ON arda0.id3 = lpi0.id3 WHERE l2.creditor_user_id = 5263 AND l2.status != '2' AND lpi0.period_start_time <= SYSDATE AND lpi0.period_end_time > SYSDATE GROUP BY l2.l0_id, lpi0.period_num, l2.creditor_user_id, lpi0.period_end_time HAVING SUM(arda0.repayment_amt) = 0 OR SUM(arda0.repayment_amt) IS NULL) SELECT l_target.*, rownum AS rn FROM (SELECT l0.l0_id AS l0id, l0.date1 AS createdate, l0.id1 AS applyid, bai.stitle AS applytitle, bai.amt1 AS applyamt, bai.rate1 AS applyratio, l0.value1 AS oripri, l0.value2 AS leftamt, SUM(CASE WHEN lpi.status != '1' THEN 1 ELSE 0 END) AS remperiods, l0.flag1 AS flag2, curi.s_cnname AS name2, curi.i_user_id AS selluserid FROM l0 LEFT JOIN lpp ON lpp.l0_id = l0.l0_id LEFT JOIN lpi0 lpi ON lpi.id3 = lpp.id3 LEFT JOIN b_apply_info bai ON bai.i_id1 = l0.id1 LEFT JOIN p_borrower_product pbp0 ON pbp0.id1 = l0.id1 LEFT JOIN p_product pp0 ON pbp0.id2 = pp0.id2 INNER JOIN c_user_regist_info curi ON l0.creditor_user_id = curi.i_user_id WHERE 1 = 1 AND l0.creditor_user_id = 5263 AND l0.status != '2' AND l0.flag1 = '1' AND pp0.product_type = 1 GROUP BY l0.l0_id, l0.value1, l0.value2, l0.id1, l0.date1, bai.stitle, bai.amt1, bai.rate1, l0.flag1, curi.s_cnname, curi.i_user_id) l_target INNER JOIN l_cur ON l_cur.l0_id = l_target.l0id LEFT JOIN (SELECT lpp0.l0_id, lpp0.status, lpp0.period FROM lpp0 INNER JOIN l_cur lc ON lc.l0_id = lpp0.l0_id WHERE lc.period_num = lpp0.period + 1) l_pre ON l_pre.l0_id = l_target.l0id LEFT JOIN (SELECT ttla.seller_l0_id, COUNT(*) t_cnt, MAX(transferable) transferable FROM tla_transfer_l0_apply ttla WHERE 1 = 1 AND ttla.seller_id = 5263 GROUP BY ttla.seller_l0_id) l_tran ON l_tran.seller_l0_id = l_target.l0id LEFT JOIN (SELECT * FROM tla_transfer_l0_apply WHERE transferable = '1') ttla1 ON ttla1.seller_l0_id = l_target.l0id WHERE 1 = 1 AND (l_tran.transferable IS NULL OR l_tran.transferable != '1') AND applyratio = 12 ORDER BY leftamt DESC;这个语句优化的效果不算大,只是从4秒到了2秒以内。勉强也算一次优化吧