查询缓冲区缓存忙等待

set pages 9999;
column buffer_busy_wait format 999,999,999;
column mydate heading ‘yr. mo dy Hr.’

select
to_char(snap_time,’yyyy-mm-dd HH24′) mydate,
new.name,
new.buffer_busy_wait-old.buffer_busy_wait buffer_busy_wait
from
perfstat.stats$buffer_pool_statistics old,
perfstat.stats$buffer_pool_statistics new,
perfstat.stats$snapshot sn
where
new.name=old.name
and new.snap_id=sn.snap_id
and old.snap_id=sn.snap_id-1
and new.buffer_busy_wait-old.buffer_busy_wait>1
group by to_char(snap_time,’yyyy-mm-dd HH24′),
new.name,
new.buffer_busy_wait-old.buffer_busy_wait;

dblink ora-00600 ora-02072 ora-02063

某汽车集团数据库环境为linux5.8,oracle 11g rac 11.2.0.4。dblink所连接的数据库环境未知,业务系统在通过dblink进行数据更新时在出现如下错误:

ORA-00600: internal error code, arguments: [ORA-00600: internal error code, arguments: [kghstack_underflow_internal_1], [0x1103965E0], [], [], [], [], [], []ORA-02072: distributed database network protocol mismatch], [], [], [], [], [], [], [], [], [], [========= Dump for incident 264681 (ORA 600 [ORA-00600: internal error code, arguments: [kghstack_underflow_internal_1], [0x1103965E0], [], [], [], [], [], []ORA-]) ========

检查相关的错误信息跟踪文件tsp1_ora_23612_i264681.trc有如下信息:

Dump continued from file: /u01/app/oracle/diag/rdbms/tsp/tsp1/trace/tsp1_ora_23612.trcORA-00600: internal error code, arguments: [ORA-00600: internal error code, arguments: [kghstack_underflow_internal_1], [0x1103965E0], [], [], [], [], [], []ORA-02072: distributed database network protocol mismatch], [], [], [], [], [], [], [], [], [], [========= Dump for incident 264681 (ORA 600 [ORA-00600: internal error code, arguments: [kghstack_underflow_internal_1], [0x1103965E0], [], [], [], [], [], []ORA-]) ========

*** 2015-01-18 17:55:15.413dbkedDefDump(): Starting incident default dumps (flags=0x2, level=3, mask=0x0)----- Current SQL Statement for this session (sql_id=43ws7jmatt2yf) -----
MERGE INTO xxx@DBLINK1 R USING (SELECT DISTINCT SUBSTR(A.VIN,LENGTH(VIN)-7,LENGTH(A.VIN)) AS VIN,A.OFFSET_LON,A.OFFSET_LAT,A.UTC,A.ADDRESS,A.MILEAGE, B.ORDERNO,C.STATUSNAME,C.STATUSFLAG FROM aaa@dblink2 A,b B,c C,d@dblink3 D WHERE LENGTH(VIN)>7 AND SUBSTR(A.VIN,LENGTH(VIN)-7,8)= B.CHASSISNUMBER AND B.CARSTATUS = C.STATUSFLAG AND A.VIN =D.LICENSEPLATE AND D.ISLOGOUT = 0 AND SUBSTR(B.ORDERNO,0,2) IN ('31','37','40') AND C.STATUSFLAG < 6 AND A.ADDRESS IS NOT NULL) D ON (R.ROW_ID='ssi'||D.VIN) WHEN MATCHED THEN UPDATE SET LONGITUDE=D.OFFSET_LON,DIMENSION=D.OFFSET_LAT,GPS_TIME = D.UTC,GPS_LOCATION=D.ADDRESS,GPS_LOCATION_SPLIT=D.ADDRESS,STATUS=D.STATUSNAME,GPS_MILEAGE=D.MILEAGE,GAUGE_MILEAGE='',CREATED_BY = 'ssi',LAST_UPD_BY='ssi',ORDER_NO=D.ORDERNO WHEN NOT MATCHED THEN INSERT (ROW_ID,CHASSIS_NO,LONGITUDE,DIMENSION,GPS_TIME,GPS_LOCATION,GPS_LOCATION_SPLIT,STATUS,GPS_MILEAGE,GAUGE_MILEAGE,CREATED_BY,LAST_UPD_BY,ORDER_NO) VALUES('ssi'||D.VIN,D.VIN,D.OFFSET_LON,D.OFFSET_LAT,D.UTC,D.ADDRESS,D.ADDRESS,D.STATUSNAME,D.MILEAGE,'','ssi','ssi',D.ORDERNO)----- PL/SQL Stack -----
----- PL/SQL Call Stack -----
  object      line  object
  handle    number  name0x2d3112bc0        33  package body DFCP.DFVL_DBLINK10x2d92769c8         1  anonymous block

----- Call Stack Trace -----
calling              call     entry                argument values in hexlocation             type     point                (? means dubious value)-------------------- -------- -------------------- ----------------------------
skdstdst()+41        call     kgdsdst()            000000000 ? 000000000 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
ksedst1()+103        call     skdstdst()           000000000 ? 000000000 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
ksedst()+39          call     ksedst1()            000000000 ? 000000001 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
dbkedDefDump()+2746  call     ksedst()             000000000 ? 000000001 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
ksedmp()+41          call     dbkedDefDump()       000000003 ? 000000002 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
ksfdmp()+69          call     ksedmp()             000000003 ? 000000002 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
dbgexPhaseII()+1764  call     ksfdmp()             000000003 ? 000000002 ?
                                                   7FFFD889F8E0 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
dbgexProcessError()  call     dbgexPhaseII()       2B585AE35730 ? 2B585AE3E7F0 ?
+2680                                              7FFFD88A8F38 ? 7FFFD889F9B8 ?
                                                   7FFFD88A4460 ? 000000002 ?
dbgeExecuteForError  call     dbgexProcessError()  2B585AE35730 ? 2B585AE3E7F0 ?
()+88                                              000000001 ? 000000000 ?
                                                   7FFFD88A4460 ? 000000002 ?
dbgePostErrorKGE()+  call     dbgeExecuteForError  2B585AE35730 ? 2B585AE3E7F0 ?
2136                          ()                   000000001 ? 000000001 ?
                                                   000000000 ? 000000002 ?
dbkePostKGE_kgsf()+  call     dbgePostErrorKGE()   00C0CC9E0 ? 2B585B260040 ?
71                                                 000000258 ? 000000001 ?
                                                   000000000 ? 000000002 ?
kgesev()+280         call     dbkePostKGE_kgsf()   00C0CC9E0 ? 2B585B260040 ?
                                                   000000258 ? 000000001 ?
                                                   000000000 ? 000000002 ?
ksesec1()+170        call     kgesev()             00C0CC9E0 ? 2B585B260040 ?
                                                   000000258 ? 000000001 ?
                                                   7FFFD88A9AE0 ? 000000002 ?
npierr()+878         call     ksesec1()            00C0CC9E0 ? 000000001 ?
                                                   0000000AC ? 2B585B49DDE0 ?
                                                   7FFFD88A9040 ? 00000000A ?
kpnerr()+240         call     npierr()             2B585B49C570 ? 000000258 ?
                                                   000000000 ? 00000005E ?
                                                   7FFFD88A9040 ? 2B585B49EE90 ?
kpnpst()+276         call     kpnerr()             2B585B49C570 ? 000000258 ?
                                                   000000000 ? 00000005E ?
                                                   7FFFD88ACA68 ? 2B585B49EE90 ?
upirtrc()+1919       call     kpnpst()             7FFFD88ACBE0 ? 000000258 ?
                                                   000000023 ? 000000000 ?
                                                   7FFFD88ACA68 ? 2B585B49EE90 ?
kpurcsc()+98         call     upirtrc()            7FFFD88ACBE0 ? 000000258 ?
                                                   000000023 ? 000000000 ?
                                                   7FFFD88ACA68 ? 2B585B4F4B10 ?
kpuexec()+10790      call     kpurcsc()            7FFFD88ACBE0 ? 000000258 ?
                                                   000000023 ? 000000000 ?
                                                   7FFFD88ACA68 ? 2B585B4F4B10 ?
kpnexe()+1517        call     kpuexec()            7FFFD88ACBE0 ? 000000258 ?
                                                   7FFFD88ACA60 ? 000000000 ?
                                                   7FFFD88ACA68 ? 7FFFD88B07B8 ?
opiexe()+32658       call     kpnexe()             2B585B2AF000 ? 2B585B4F4B10 ?
                                                   2B585B507EB8 ? 2B585B19A950 ?
                                                   000000001 ? 1000000000 ?
opipls()+2164        call     opiexe()             000000004 ? 000000005 ?
                                                   2B585B507EB8 ? 2B585B19A950 ?
                                                   000000001 ? 1000000000 ?
opiodr()+917         call     opipls()             000000066 ? 000000005 ?
                                                   2B585B507EB8 ? 2B585B19A950 ?
                                                   000000001 ? 1000000000 ?
rpidrus()+211        call     opiodr()             000000066 ? 000000007 ?
                                                   7FFFD88B3960 ? 2B585B19A950 ?
                                                   000000001 ? 1000000000 ?
skgmstack()+148      call     rpidrus()            7FFFD88B3338 ? 000000007 ?
                                                   7FFFD88B3960 ? 2B585B19A950 ?
                                                   000000001 ? 1000000000 ?
rpiswu2()+690        call     skgmstack()          7FFFD88B3310 ? 00C0CC600 ?
                                                   00000F618 ? 009698A9C ?
                                                   7FFFD88B3338 ? 1000000000 ?
rpidrv()+1327        call     rpiswu2()            2C511BF60 ? 00000006D ?
                                                   2C511BFE4 ? 000000002 ?
                                                   7FFFD88B3338 ? 1000000000 ?
psddr0()+473         call     rpidrv()             00000000B ? 000000066 ?
                                                   7FFFD88B3960 ? 000000038 ?
                                                   7FFFD88B3338 ? 1000000000 ?
psdnal()+457         call     psddr0()             00000000B ? 000000066 ?
                                                   7FFFD88B3960 ? 000000030 ?
                                                   7FFFD88B3338 ? 1000000000 ?
pevm_EXECC()+314     call     psdnal()             7FFFD88B50E0 ? 7FFFD88B52F0 ?
                                                   7FFFD88B3960 ? 2B585B2A2AE8 ?
                                                   22EC69508 ? 1000000000 ?
pfrinstr_EXECC()+80  call     pevm_EXECC()         2B585B1D5CA0 ? 2B585B409640 ?
                                                   000000020 ? 2B585B2A2AE8 ?
                                                   22EC69508 ? 1000000000 ?
pfrrun_no_tool()+63  call     pfrinstr_EXECC()     2B585B2A7258 ? 21D5E8DB4 ?
                                                   2B585B2A72C8 ? 2B585B2A2AE8 ?
                                                   22EC69508 ? 2B5800000020 ?
pfrrun()+627         call     pfrrun_no_tool()     2B585B2A7258 ? 21D5E8DB4 ?
                                                   2B585B2A72C8 ? 2B585B2A2AE8 ?
                                                   22EC69508 ? 2B5800000020 ?
plsql_run()+649      call     pfrrun()             2B585B2A7258 ? 21D5E8DB4 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 2B5800000020 ?
peicnt()+302         call     plsql_run()          2B585B2A7258 ? 21D5E8DB4 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 2B5800000020 ?
kkxexe()+525         call     peicnt()             7FFFD88B50E0 ? 2B585B2A7258 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 000000000 ?
opiexe()+18001       call     kkxexe()             2B585B2ABF80 ? 2B585B2A7258 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 000000000 ?
kpoal8()+2118        call     opiexe()             000000049 ? 000000003 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 000000000 ?
opiodr()+917         call     kpoal8()             00000005E ? 000000003 ?
                                                   2B585B2A72C8 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 000000000 ?
ttcpip()+2183        call     opiodr()             00000005E ? 00000001C ?
                                                   7FFFD88B9910 ? 7FFFD88B50E0 ?
                                                   22EC69508 ? 000000000 ?
opitsk()+1710        call     ttcpip()             00C0EA630 ? 00999BA90 ?
                                                   7FFFD88B9910 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
opiino()+969         call     opitsk()             00C0EA638 ? 000000000 ?
                                                   7FFFD88B9910 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
opiodr()+917         call     opiino()             00000003C ? 000000004 ?
                                                   7FFFD88BB108 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
opidrv()+570         call     opiodr()             00000003C ? 000000004 ?
                                                   7FFFD88BB108 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
sou2o()+103          call     opidrv()             00000003C ? 000000004 ?
                                                   7FFFD88BB108 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
opimai_real()+133    call     sou2o()              7FFFD88BB0E0 ? 00000003C ?
                                                   000000004 ? 7FFFD88BB108 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
ssthrdmain()+265     call     opimai_real()        000000002 ? 7FFFD88BB2D0 ?
                                                   000000004 ? 7FFFD88BB108 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
main()+201           call     ssthrdmain()         000000002 ? 7FFFD88BB2D0 ?
                                                   000000001 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
__libc_start_main()  call     main()               000000002 ? 7FFFD88BB478 ?
+244                                               000000001 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?
_start()+41          call     __libc_start_main()  000A29108 ? 000000002 ?
                                                   7FFFD88BB468 ? 000000000 ?
                                                   7FFFD88B9368 ? 7FFFD88B990C ?


--------------------- Binary Stack Dump ---------------------

在执行如下语句时报错:

merge into xxx@dblink1 r

using (select distinct substr(a.vin, length(vin) - 7, length(a.vin)) as vin,

                       a.offset_lon,

                       a.offset_lat,

                       a.utc,

                       a.address,

                       a.mileage,

                       b.orderno,

                       c.statusname,

                       c.statusflag

         from aaaa@dblink2 a,

              b                 b,

              c          c,

              dddd@dblink3    d

        where length(vin) > 7

          and substr(a.vin, length(vin) - 7, 8) = b.chassisnumber

          and b.carstatus = c.statusflag

          and a.vin = d.licenseplate

          and d.islogout = 0

          and substr(b.orderno, 0, 2) in ('31', '37', '40')

          and c.statusflag < 6

          and a.address is not null) d

on (r.row_id = 'ssi' || d.VIN)

when MATCHED then

  update

     set LONGITUDE          = d.offset_lon,

         DIMENSION          = d.offset_lat,

         gps_time           = d.utc,

         GPS_LOCATION       = d.address,

         GPS_LOCATION_SPLIT = d.address,

         STATUS             = d.statusname,

         GPS_MILEAGE        = d.mileage,

         GAUGE_MILEAGE      = '',

         created_by         = 'ssi',

         last_upd_by        = 'ssi',

         order_no           = d.orderno

when NOT MATCHED then

  insert

    (row_id,

     chassis_no,

     LONGITUDE,

     DIMENSION,

     GPS_TIME,

     GPS_LOCATION,

     GPS_LOCATION_SPLIT,

     STATUS,

     GPS_MILEAGE,

     GAUGE_MILEAGE,

     created_by,

     last_upd_by,

     order_no)

  VALUES

    ('ssi' || d.vin,

     d.vin,

     d.offset_lon,

     d.offset_lat,

     d.utc,

     d.address,

     d.address,

     d.statusname,

     d.mileage,

     '',

     'ssi',

     'ssi',

     d.orderno)

后面经询问了解到通过dblink所连接的远端数据库版本要低,而生产数据库升级前的版本为11.2.0.1,通过dblink进行数据更新是没有问题。这种通过dblink,且数据库版本不同的情况下经常遇到这种问题。在这种情况下,一般的处理方法就是通过dblink执行最简单的select,insert,update,delete语句看是否报错,如果没有出现故障,我们就可以将使用merge语法的SQL修改成最简单的select,insert,update,delete语句来达到相同的目的。也就像有多条路到达一个目的地,其中一条路因为某些原因不能通行,就改走另一路。

通过脚本来查看alert.log

create or replace directory data_dir as ‘/oracle/diag/rdbms/develop/develop/trace’;

CREATE TABLE alert_log
(
text_line varchar2(2000)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY data_dir
ACCESS PARAMETERS
(
records delimited by newline
fields
REJECT ROWS WITH ALL NULL FIELDS
)
LOCATION
(
‘alert_develop.log’
)
)
REJECT LIMIT unlimited;

select to_char(last_time,’dd-mon-yyyy hh24:mi’) shutdown,
to_char(start_time,’dd-mon-yyyy hh24:mi’) startup,
round((start_time-last_time)*24*60,2) mins_down,
round((last_time-lag(start_time) over (order by r)),2) days_up,
case when (lead(r) over (order by r) is null )
then round((sysdate-start_time),2)
end days_still_up
from (
select r,
to_date(last_time, ‘Dy Mon DD HH24:MI:SS YYYY’) last_time,
to_date(start_time,’Dy Mon DD HH24:MI:SS YYYY’) start_time
from (
select r,
text_line,
lag(text_line,1) over (order by r) start_time,
lag(text_line,2) over (order by r) last_time
from (
select rownum r, text_line
from alert_log
where text_line like ‘___ ___ __ __:__:__ 20__’
or text_line like ‘Starting ORACLE instance %’
)
)
where text_line like ‘Starting ORACLE instance %’
and start_time not like ‘Starting ORACLE instance %’ and last_time not like ‘Starting ORACLE instance %’

)

select to_char(‘Fri Mar 09 11:20:21 2012′,’dd-mon-yyyy hh24:mi’) from dual

创建合理的索引避免order by操作提升SQL性能的案例一

某医保系统的查询一年内一个中心所有医疗费登账记录的汇总情况执行3000多秒才能出结果,下面TOP SQL中执行时间最长的SQL语句它的功能是查询一年内一个中心所有医疗费登账记录的汇总情况,执行时间3286秒,则且这条SQL就是我们要找的SQL语句。

Elapsed Time (s)      CPU Time (s)	Executions	Elap per Exec (s)	% Total DB Time	 SQL Id          SQL Module	        SQL Text
3,286	              2,015	        1	        3,286		        18.59	         8aw9tm6w83usm	 JDBC Thin Client	select t1.account_id, ...
622	              316	        17	        36.60	                3.52	         ggrctzgtcg14s	 JDBC Thin Client	select t.pay_type as pay_...
222	              4	                1	        221.64	                1.25	         fvy1hnauapb8s	 JDBC Thin Client	select (select center_name fro...
204	              5	                1	        203.60	                1.15	         fcm5wrxny5b2u	 JDBC Thin Client	select count(1) cs from bs_ho...
198	              125	        31	        6.38	                1.12	         cfuhdxk446uzv	 JDBC Thin Client	select * from ( select a.hosp...
177	              20	        1	        177.00	                1.00	         42zd8jamnw4b4	 PL/SQL Developer	begin dbms_sqltune.execute_tu...
158	              88	        1	        158.21	                0.89	         17t35rynaz40v	 JDBC Thin Client	select u.center_name, x.hospit...
142	              89	        97	        1.46	                0.80	         a7dkwg8uhrwkj	 JDBC Thin Client	select * from ( select a.hosp...
135	              48	        1,417	        0.10	                0.76	         aay99a855zv3u	 JDBC Thin Client	SELECT distinct c.NAME, ...
116	              73	        303	        0.38	                0.66	         31tf0tfn8nv6f	 JDBC Thin Client	select a.pay_batch_no, a.pay_b...

其SQL如下所示:

select  t1.account_id,
       t1.hospital_id,
       (select hosp_level
          from bs_hospital a
         where t1.hospital_id = a.hospital_id
           and a.valid_flag = '1') as hosp_level,
       t1.serial_no,
       t1.icd,
       (select t4.disease
          from bs_disease t4
         where t1.icd = t4.icd(+)
           and t4.center_id = nvl(center.catalog_center, center.center_id)) as disease,
       t1.biz_type,
       t1.treatment_type,
       t1.disease_type,
       t1.reimburse_flag,
       t1.corp_id,
       t1.corp_type_code,
       t2.corp_name,
       t1.indi_id,
       t1.pers_type,
       t2.center_id,
       t2.district_code,
       decode(t2.center_id, t2.district_code, 0, 1) as out_type,
       t1.biz_flag,
       t1.biz_flag_old,
       t2.name,
       t2.idcard,
       t2.sex,
       t2.begin_date,
       t2.end_date,
       t1.fin_date,
       t2.reg_date,
       t1.disease_fee,
       t1.violate_flag,
       t1.patient_id,
       t1.in_days,
       decode(t1.allow_treat, '0', 0, 1) As allow_treat,
       Decode(T1.Treatment_Type_Last,
              '',
              T1.Treatment_Type,
              Decode(T1.Treatment_Type,
                     '120',
                     '120',
                     Decode(T1.Biz_Type,
                            '41',
                            '扣减已记帐金额',
                            '42',
                            '扣减已记帐金额',
                            T1.Treatment_Type_Last))) As Treatment_Type_Last,
       t1.violate,
       t1.pay_money,
       t1.pay_fund,
       t1.audit_money,
       '0' as finish_flag,
       t1.MONTH_DECL_SN,
       t1.deal_flag,
       t1.audit_deal_flag,
       t1.declare_icd_guide,
       t1.declare_audit_fee,
       t1.declare_audit_fee_cl,
       t1.first_icd_guide,
       t1.first_audit_fee,
       t1.first_audit_fee_cl,
       t1.second_icd_guide,
       t1.second_audit_fee,
       t1.second_audit_fee_cl,
       t1.PAY_otherfee,
       t1.PAY_FUND003,
       t1.PAY_FUND001,
       t1.PAY_FUND201,
       t1.PAY_FUND301,
       t1.PAY_FUND999,
       t1.PAY_FUND996,
       t1.PAY_begin
  from (select a.account_id,
               a.hospital_id,
               a.serial_no,
               a.center_id,
               a.icd,
               a.biz_type,
               a.treatment_type,
               a.reimburse_flag,
               a.corp_id,
               a.corp_type_code,
               a.indi_id,
               a.pers_type,
               a.biz_flag,
               a.biz_flag_old,
               a.fin_date,
               a.violate_flag,
               (select mbf.disease_type
                  from mt_biz_fin mbf
                 where mbf.valid_flag = '1'
                   and mbf.hospital_id = a.hospital_id
                   and mbf.serial_no = a.serial_no) disease_type,
               (Select aa1.allow_treat
                  From pm_account_scene aa1
                 where aa1.hospital_id = a.hospital_id
                   and aa1.serial_no = a.serial_no
                   and aa1.valid_flag = '1') as allow_treat,
               (select distinct bb1.patient_id
                  from mt_biz_fin bb1
                 where bb1.hospital_id = a.hospital_id
                   and bb1.serial_no = a.serial_no
                   and bb1.valid_flag = '1') as patient_id,
               (select distinct bb1.in_days
                  from mt_biz_fin bb1
                 where bb1.hospital_id = a.hospital_id
                   and bb1.serial_no = a.serial_no
                   and bb1.valid_flag = '1') as in_days,
               (select Max(audit_treat_value)
                  from pm_treat_audit aa
                 where aa.account_id = a.account_id
                   and aa.audit_staff_id = '2208'
                   and aa.audit_phase = '1'
                   and aa.valid_flag = '1') as treatment_type_last,
               a.violate,
               nvl((SELECT sum(pm_account_fund.pay_money)
                     FROM pm_account_fund
                    WHERE a.account_id = pm_account_fund.account_id
                      AND pm_account_fund.valid_flag = '1'),
                   0) as pay_money,
               nvl((SELECT sum(pm_account_fund.pay_money)
                     FROM pm_account_fund
                    WHERE a.account_id = pm_account_fund.account_id
                      AND pm_account_fund.valid_flag = '1'
                      AND pm_account_fund.fund_id not in
                          ('003', '999', '996', '997')),
                   0) as pay_fund,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '003'),
                   0) AS PAY_FUND003,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID in ('801', '001')),
                   0) AS PAY_FUND001,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '201'),
                   0) AS PAY_FUND201,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '301'),
                   0) AS PAY_FUND301,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '999'),
                   0) AS PAY_FUND999,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '996'),
                   0) AS PAY_FUND996,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID not in
                          ('801', '001', '003', '201', '999')),
                   0) AS PAY_otherfee,
               NVL((SELECT SUM(mprf.real_pay)
                     FROM mt_pay_record_fin mprf
                    WHERE A.hospital_id = mprf.hospital_id
                      AND A.serial_no = mprf.serial_no
                      AND mprf.policy_item_code = 'S01'),
                   0) AS PAY_begin,
               (select nvl(sum(b.AUDIT_MONEY), 0)
                  from pm_fee_audit b
                 where a.account_id = b.account_id
                   and b.audit_staff_id = '2208'
                   and b.AUDIT_TYPE = '1'
                   and b.AUDIT_PHASE = '1'
                   and b.valid_flag = '1') as audit_money,
               a.MONTH_DECL_SN,
               a.deal_flag,
               decode(nvl(a.disease_fee, 0),
                      0,
                      nvl((select nvl(max(ration), 0)
                            from pm_ration pr
                           where pr.center_id = a.center_id
                             and pr.hospital_id = a.hospital_id
                             and pr.treatment_type = a.treatment_type
                             and pr.year = to_char(a.fin_date, 'yyyy')
                             and (decode(a.pers_type,
                                         '21',
                                         '21',
                                         '22',
                                         '21',
                                         '23',
                                         '21',
                                         '24',
                                         '21',
                                         '41',
                                         '21',
                                         '3',
                                         '12',
                                         '4',
                                         '12',
                                         '5',
                                         '12',
                                         '7',
                                         '12',
                                         '2') = pr.insr_no or
                                 pr.insr_no = '0')
                             and rownum < 2
                             and pr.biz_flag = a.biz_flag),
                          0),
                      a.disease_fee) as disease_fee,
               a.audit_flag as audit_deal_flag,
               a.declare_icd_guide as declare_icd_guide,
               a.declare_audit_fee as declare_audit_fee,
               a.declare_audit_fee_cl as declare_audit_fee_cl,
               a.first_icd_guide as first_icd_guide,
               a.first_audit_fee as first_audit_fee,
               a.first_audit_fee_cl as first_audit_fee_cl,
               decode(a.biz_type,
                      '12',
                      decode(a.biz_flag_old, 5, '4304001', a.second_icd_guide),
                      a.second_icd_guide) second_icd_guide,
               a.second_audit_fee as second_audit_fee,
               a.second_audit_fee_cl as second_audit_fee_cl
          from pm_account_biz a
         where 1 = 1
           and ('0' = 'A' or a.reimburse_flag ='0')
           and ('00' = '00' or a.biz_type ='00')
           and a.center_id in ('430722')
           and (a.fin_date between to_date('2014-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
               to_date('2014-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss'))
           and a.valid_flag = '1'
           and a.deal_flag in ('0', '1', '2')
           and (nvl(a.staff1_finish_flag, '0') ='0' and
               nvl(a.staff2_finish_flag, '0') = '0' and
               nvl(a.staff3_finish_flag, '0') = '0')
         group by a.account_id,
                  a.hospital_id,
                  a.serial_no,
                  a.center_id,
                  a.icd,
                  a.biz_type,
                  a.treatment_type,
                  a.reimburse_flag,
                  a.corp_id,
                  a.corp_type_code,
                  a.indi_id,
                  a.pers_type,
                  a.biz_flag,
                  a.biz_flag_old,
                  a.disease_fee,
                  a.fin_date,
                  a.violate_flag,
                  a.violate,
                  a.MONTH_DECL_SN,
                  a.deal_flag,
                  a.audit_flag,
                  a.declare_icd_guide,
                  a.declare_audit_fee,
                  a.declare_audit_fee_cl,
                  a.first_icd_guide,
                  a.first_audit_fee,
                  a.first_audit_fee_cl,
                  a.second_icd_guide,
                  a.second_audit_fee,
                  a.second_audit_fee_cl) t1,
       mt_biz_fin t2,
       bs_center center
 where t1.serial_no = t2.serial_no
   AND t1.hospital_id = t2.hospital_id
   AND t2.valid_flag = '1'
   and t1.center_id = center.center_id(+)
 order by t1.hospital_id, t1.serial_no

上面SQL的执行计划如下:

----------------------------------------------------------------------------------------------------------------------------
| Id  | Order | Operation                         | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------------------------------
|   0 |    67 | SELECT STATEMENT                  |                                |       |       | 56871 (100)|          |
|   1 |     2 |  TABLE ACCESS BY INDEX ROWID      | BS_HOSPITAL                    |     1 |    16 |     1   (0)| 00:00:01 |
|   2 |     1 |   INDEX UNIQUE SCAN               | PK_BS_HOSPITAL                 |     1 |       |     1   (0)| 00:00:01 |
|   3 |     4 |  TABLE ACCESS BY INDEX ROWID      | BS_DISEASE                     |     1 |    33 |     1   (0)| 00:00:01 |
|   4 |     3 |   INDEX RANGE SCAN                | INX_BS_DISEASE_01              |     1 |       |     1   (0)| 00:00:01 |
|   5 |     6 |  TABLE ACCESS BY INDEX ROWID      | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|   6 |     5 |   INDEX RANGE SCAN                | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|   7 |    10 |  SORT AGGREGATE                   |                                |     1 |    33 |            |          |
|   8 |     9 |   COUNT STOPKEY                   |                                |       |       |            |          |
|   9 |     8 |    TABLE ACCESS BY INDEX ROWID    | PM_RATION                      |     1 |    33 |     1   (0)| 00:00:01 |
|  10 |     7 |     INDEX RANGE SCAN              | IDX_PM_RATION_1                |     1 |       |     1   (0)| 00:00:01 |
|  11 |    13 |  HASH UNIQUE                      |                                |     1 |    26 |     2  (50)| 00:00:01 |
|  12 |    12 |   TABLE ACCESS BY INDEX ROWID     | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|  13 |    11 |    INDEX RANGE SCAN               | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|  14 |    16 |  HASH UNIQUE                      |                                |     1 |    26 |     2  (50)| 00:00:01 |
|  15 |    15 |   TABLE ACCESS BY INDEX ROWID     | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|  16 |    14 |    INDEX RANGE SCAN               | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|  17 |    18 |  TABLE ACCESS BY INDEX ROWID      | PM_ACCOUNT_SCENE               |     1 |    28 |     1   (0)| 00:00:01 |
|  18 |    17 |   INDEX RANGE SCAN                | IDX_PM_ACCOUNT_SCENE_1         |     1 |       |     1   (0)| 00:00:01 |
|  19 |    21 |  SORT AGGREGATE                   |                                |     1 |    37 |            |          |
|  20 |    20 |   TABLE ACCESS BY INDEX ROWID     | PM_TREAT_AUDIT                 |     1 |    37 |     1   (0)| 00:00:01 |
|  21 |    19 |    INDEX RANGE SCAN               | PK_PM_TREAT_AUDIT              |     1 |       |     1   (0)| 00:00:01 |
|  22 |    24 |  SORT AGGREGATE                   |                                |     1 |    12 |            |          |
|  23 |    23 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    12 |     1   (0)| 00:00:01 |
|  24 |    22 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  25 |    27 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  26 |    26 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  27 |    25 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  28 |    30 |  SORT AGGREGATE                   |                                |     1 |    21 |            |          |
|  29 |    29 |   TABLE ACCESS BY INDEX ROWID     | PM_FEE_AUDIT                   |     1 |    21 |     1   (0)| 00:00:01 |
|  30 |    28 |    INDEX RANGE SCAN               | PK_PM_FEE_AUDIT                |     1 |       |     1   (0)| 00:00:01 |
|  31 |    33 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  32 |    32 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  33 |    31 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  34 |    36 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  35 |    35 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  36 |    34 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  37 |    40 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  38 |    39 |   INLIST ITERATOR                 |                                |       |       |            |          |
|  39 |    38 |    TABLE ACCESS BY INDEX ROWID    | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  40 |    37 |     INDEX RANGE SCAN              | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  41 |    43 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  42 |    42 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  43 |    41 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  44 |    46 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  45 |    45 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  46 |    44 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  47 |    49 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  48 |    48 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  49 |    47 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  50 |    52 |  SORT AGGREGATE                   |                                |     1 |    16 |            |          |
|  51 |    51 |   TABLE ACCESS BY INDEX ROWID     | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|  52 |    50 |    INDEX RANGE SCAN               | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  53 |    55 |  SORT AGGREGATE                   |                                |     1 |    39 |            |          |
|  54 |    54 |   TABLE ACCESS BY INDEX ROWID     | MT_PAY_RECORD_FIN              |     1 |    39 |     1   (0)| 00:00:01 |
|  55 |    53 |    INDEX RANGE SCAN               | IDX_MT_PAY_RECORD_FIN_2        |     1 |       |     1   (0)| 00:00:01 |
|  56 |    66 |  FILTER                           |                                |       |       |            |          |
|  57 |    65 |   SORT GROUP BY                   |                                |     1 |   309 | 56871   (1)| 00:11:23 |
|  58 |    64 |    TABLE ACCESS BY INDEX ROWID    | MT_BIZ_FIN                     |     1 |   119 |     1   (0)| 00:00:01 |
|  59 |    63 |     NESTED LOOPS                  |                                |     1 |   309 | 56869   (1)| 00:11:23 |
|  60 |    61 |      MERGE JOIN OUTER             |                                |     1 |   190 | 56868   (1)| 00:11:23 |
|  61 |    57 |       TABLE ACCESS BY INDEX ROWID | PM_ACCOUNT_BIZ                 |     1 |   164 | 56867   (1)| 00:11:23 |
|  62 |    56 |        INDEX RANGE SCAN           | IDX_PM_ACCOUNT_BIZ_SERIAL_DATE |   285K|       |   209   (1)| 00:00:03 |
|  63 |    60 |       BUFFER SORT                 |                                |     1 |    26 |     1   (0)| 00:00:01 |
|  64 |    59 |        TABLE ACCESS BY INDEX ROWID| BS_CENTER                      |     1 |    26 |     1   (0)| 00:00:01 |
|  65 |    58 |         INDEX UNIQUE SCAN         | PK_BS_CENTER                   |     1 |       |     1   (0)| 00:00:01 |
|  66 |    62 |      INDEX RANGE SCAN             | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------------------------------

从其执行计划中可以看到最消耗成本的操作就是对IDX_PM_ACCOUNT_BIZ_SERIAL_DATE索引执行索引范围扫描返回285K条记录再回表查询记录的成本是56867,而整个SQL语句的成本是56871。再与BS_CENTER表执行排序合并连接,再与MT_BIZ_FIN执行嵌套循环连接,再执行group by操作。因为表BS_CENTER表只有10几条记录,且表中的center_id是主键,表PM_ACCOUNT_BIZ中center_id与其它列存在复合索引,理想的执行计划应该是先访问表BS_CENTER再与PM_ACCOUNT_BIZ表执行嵌套循环连接,再与MT_BIZ_FIN连接。而且上述SQL中的group by子句是可以去掉了,这里group by真正的作用只起到了去掉重复记录的作用,而PM_ACCOUNT_BIZ表的主键是account_id,在查询列中,所以可以去掉这个group by 子句。

上面的SQL语句简化如下形式:

select ...省略
from pm_account_biz a
         where 1 = 1
           and ('0' = 'A' or a.reimburse_flag ='0')
           and ('00' = '00' or a.biz_type ='00')
           and a.center_id in ('430722')
           and (a.fin_date between to_date('2014-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
               to_date('2014-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss'))
           and a.valid_flag = '1'
           and a.deal_flag in ('0', '1', '2')
           and (nvl(a.staff1_finish_flag, '0') ='0' and
               nvl(a.staff2_finish_flag, '0') = '0' and
               nvl(a.staff3_finish_flag, '0') = '0')
         group by a.account_id,
                  a.hospital_id,
                  a.serial_no,
                  a.center_id,
                  a.icd,
                  a.biz_type,
                  a.treatment_type,
                  a.reimburse_flag,
                  a.corp_id,
                  a.corp_type_code,
                  a.indi_id,
                  a.pers_type,
                  a.biz_flag,
                  a.biz_flag_old,
                  a.disease_fee,
                  a.fin_date,
                  a.violate_flag,
                  a.violate,
                  a.MONTH_DECL_SN,
                  a.deal_flag,
                  a.audit_flag,
                  a.declare_icd_guide,
                  a.declare_audit_fee,
                  a.declare_audit_fee_cl,
                  a.first_icd_guide,
                  a.first_audit_fee,
                  a.first_audit_fee_cl,
                  a.second_icd_guide,
                  a.second_audit_fee,
                  a.second_audit_fee_cl) t1,
       mt_biz_fin t2,
       bs_center center
 where t1.serial_no = t2.serial_no
   AND t1.hospital_id = t2.hospital_id
   AND t2.valid_flag = '1'
   and t1.center_id = center.center_id(+)
 order by t1.hospital_id, t1.serial_no

可以看到表mt_biz_fin,与bs_center可以合并到内嵌视图t1中直接与表pm_account_biz进行表连接,前面已经说过了可以去掉group by子句,进行改写后的SQL如下:

select  t1.account_id,
       t1.hospital_id,
       (select hosp_level
          from bs_hospital a
         where t1.hospital_id = a.hospital_id
           and a.valid_flag = '1') as hosp_level,
       t1.serial_no,
       t1.icd,
       (select t4.disease
          from bs_disease t4
         where t1.icd = t4.icd(+)
           and t4.center_id = nvl(t1.catalog_center, t1.center_id)) as disease,
       t1.biz_type,
       t1.treatment_type,
       t1.disease_type,
       t1.reimburse_flag,
       t1.corp_id,
       t1.corp_type_code,
       t1.corp_name,
       t1.indi_id,
       t1.pers_type,
       t1.center_id,
       t1.district_code,
       t1.out_type,
       t1.biz_flag,
       t1.biz_flag_old,
       t1.name,
       t1.idcard,
       t1.sex,
       t1.begin_date,
       t1.end_date,
       t1.fin_date,
       t1.reg_date,
       t1.disease_fee,
       t1.violate_flag,
       t1.patient_id,
       t1.in_days,
       decode(t1.allow_treat, '0', 0, 1) As allow_treat,
       Decode(T1.Treatment_Type_Last,
              '',
              T1.Treatment_Type,
              Decode(T1.Treatment_Type,
                     '120',
                     '120',
                     Decode(T1.Biz_Type,
                            '41',
                            '扣减已记帐金额',
                            '42',
                            '扣减已记帐金额',
                            T1.Treatment_Type_Last))) As Treatment_Type_Last,
       t1.violate,
       t1.pay_money,
       t1.pay_fund,
       t1.audit_money,
       '0' as finish_flag,
       t1.MONTH_DECL_SN,
       t1.deal_flag,
       t1.audit_deal_flag,
       t1.declare_icd_guide,
       t1.declare_audit_fee,
       t1.declare_audit_fee_cl,
       t1.first_icd_guide,
       t1.first_audit_fee,
       t1.first_audit_fee_cl,
       t1.second_icd_guide,
       t1.second_audit_fee,
       t1.second_audit_fee_cl,
       t1.PAY_otherfee,
       t1.PAY_FUND003,
       t1.PAY_FUND001,
       t1.PAY_FUND201,
       t1.PAY_FUND301,
       t1.PAY_FUND999,
       t1.PAY_FUND996,
       t1.PAY_begin
  from (select a.account_id,
               a.hospital_id,
               a.serial_no,
               a.center_id,
               a.icd,
               a.biz_type,
               a.treatment_type,
               a.reimburse_flag,
               a.corp_id,
               a.corp_type_code,
               t2.corp_name,
               a.indi_id,
               a.pers_type,
               center.catalog_center,
               t2.district_code,
               decode(t2.center_id, t2.district_code, 0, 1) as out_type,
               a.biz_flag,
               a.biz_flag_old,
               t2.name,
               t2.idcard,
               t2.sex,
               t2.begin_date,
               t2.end_date,
               a.fin_date,
               t2.reg_date,
               a.violate_flag,
               (select mbf.disease_type
                  from mt_biz_fin mbf
                 where mbf.valid_flag = '1'
                   and mbf.hospital_id = a.hospital_id
                   and mbf.serial_no = a.serial_no) disease_type,
               (Select aa1.allow_treat
                  From pm_account_scene aa1
                 where aa1.hospital_id = a.hospital_id
                   and aa1.serial_no = a.serial_no
                   and aa1.valid_flag = '1') as allow_treat,
               (select distinct bb1.patient_id
                  from mt_biz_fin bb1
                 where bb1.hospital_id = a.hospital_id
                   and bb1.serial_no = a.serial_no
                   and bb1.valid_flag = '1') as patient_id,
               (select distinct bb1.in_days
                  from mt_biz_fin bb1
                 where bb1.hospital_id = a.hospital_id
                   and bb1.serial_no = a.serial_no
                   and bb1.valid_flag = '1') as in_days,
               (select Max(audit_treat_value)
                  from pm_treat_audit aa
                 where aa.account_id = a.account_id
                   and aa.audit_staff_id = '2208'
                   and aa.audit_phase = '1'
                   and aa.valid_flag = '1') as treatment_type_last,
               a.violate,
               nvl((SELECT sum(pm_account_fund.pay_money)
                     FROM pm_account_fund
                    WHERE a.account_id = pm_account_fund.account_id
                      AND pm_account_fund.valid_flag = '1'),
                   0) as pay_money,
               nvl((SELECT sum(pm_account_fund.pay_money)
                     FROM pm_account_fund
                    WHERE a.account_id = pm_account_fund.account_id
                      AND pm_account_fund.valid_flag = '1'
                      AND pm_account_fund.fund_id not in
                          ('003', '999', '996', '997')),
                   0) as pay_fund,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '003'),
                   0) AS PAY_FUND003,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID in ('801', '001')),
                   0) AS PAY_FUND001,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '201'),
                   0) AS PAY_FUND201,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '301'),
                   0) AS PAY_FUND301,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '999'),
                   0) AS PAY_FUND999,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID = '996'),
                   0) AS PAY_FUND996,
               NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
                     FROM PM_ACCOUNT_FUND
                    WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
                      AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
                      AND PM_ACCOUNT_FUND.FUND_ID not in
                          ('801', '001', '003', '201', '999')),
                   0) AS PAY_otherfee,
               NVL((SELECT SUM(mprf.real_pay)
                     FROM mt_pay_record_fin mprf
                    WHERE A.hospital_id = mprf.hospital_id
                      AND A.serial_no = mprf.serial_no
                      AND mprf.policy_item_code = 'S01'),
                   0) AS PAY_begin,
               (select nvl(sum(b.AUDIT_MONEY), 0)
                  from pm_fee_audit b
                 where a.account_id = b.account_id
                   and b.audit_staff_id = '2208'
                   and b.AUDIT_TYPE = '1'
                   and b.AUDIT_PHASE = '1'
                   and b.valid_flag = '1') as audit_money,
               a.MONTH_DECL_SN,
               a.deal_flag,
               decode(nvl(a.disease_fee, 0),
                      0,
                      nvl((select nvl(max(ration), 0)
                            from pm_ration pr
                           where pr.center_id = a.center_id
                             and pr.hospital_id = a.hospital_id
                             and pr.treatment_type = a.treatment_type
                             and pr.year = to_char(a.fin_date, 'yyyy')
                             and (decode(a.pers_type,
                                         '21',
                                         '21',
                                         '22',
                                         '21',
                                         '23',
                                         '21',
                                         '24',
                                         '21',
                                         '41',
                                         '21',
                                         '3',
                                         '12',
                                         '4',
                                         '12',
                                         '5',
                                         '12',
                                         '7',
                                         '12',
                                         '2') = pr.insr_no or
                                 pr.insr_no = '0')
                             and rownum < 2
                             and pr.biz_flag = a.biz_flag),
                          0),
                      a.disease_fee) as disease_fee,
               a.audit_flag as audit_deal_flag,
               a.declare_icd_guide as declare_icd_guide,
               a.declare_audit_fee as declare_audit_fee,
               a.declare_audit_fee_cl as declare_audit_fee_cl,
               a.first_icd_guide as first_icd_guide,
               a.first_audit_fee as first_audit_fee,
               a.first_audit_fee_cl as first_audit_fee_cl,
               decode(a.biz_type,
                      '12',
                      decode(a.biz_flag_old, 5, '4304001', a.second_icd_guide),
                      a.second_icd_guide) second_icd_guide,
               a.second_audit_fee as second_audit_fee,
               a.second_audit_fee_cl as second_audit_fee_cl
          from bs_center center,pm_account_biz a,mt_biz_fin t2
         where 1 = 1
           and ('0' = 'A' or a.reimburse_flag ='0')
           and ('00' = '00' or a.biz_type ='00')
           and a.center_id in ('430701')
           and (a.fin_date between to_date('2014-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
               to_date('2014-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss'))
           and a.valid_flag = '1'
           and a.deal_flag in ('0', '1', '2')
           and (nvl(a.staff1_finish_flag, '0') ='0' and
               nvl(a.staff2_finish_flag, '0') = '0' and
               nvl(a.staff3_finish_flag, '0') = '0')
           and a.serial_no = t2.serial_no
           AND a.hospital_id = t2.hospital_id
           AND t2.valid_flag = '1'
           and a.center_id = center.center_id(+)
           and center.center_id in ('430701')          
           order by a.hospital_id, a.serial_no
           )t1

来测试执行一次,但是执行了10分钟还是没有执行完,其执行计划如下:

------------------------------------------------------------------------------------------------------------------
| Id  | Operation                       | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |                                |     3 |   855 | 24453   (1)| 00:04:54 |
|*  1 |  TABLE ACCESS BY INDEX ROWID    | BS_HOSPITAL                    |     1 |    16 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN             | PK_BS_HOSPITAL                 |     1 |       |     1   (0)| 00:00:01 |
|   3 |  TABLE ACCESS BY INDEX ROWID    | BS_DISEASE                     |     1 |    33 |     1   (0)| 00:00:01 |
|*  4 |   INDEX RANGE SCAN              | INX_BS_DISEASE_01              |     1 |       |     1   (0)| 00:00:01 |
|*  5 |  TABLE ACCESS BY INDEX ROWID    | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|*  6 |   INDEX RANGE SCAN              | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|   7 |  SORT AGGREGATE                 |                                |     1 |    34 |            |          |
|*  8 |   COUNT STOPKEY                 |                                |       |       |            |          |
|*  9 |    TABLE ACCESS BY INDEX ROWID  | PM_RATION                      |     1 |    34 |     1   (0)| 00:00:01 |
|* 10 |     INDEX RANGE SCAN            | IDX_PM_RATION_1                |     1 |       |     1   (0)| 00:00:01 |
|  11 |  HASH UNIQUE                    |                                |     1 |    26 |     2  (50)| 00:00:01 |
|* 12 |   TABLE ACCESS BY INDEX ROWID   | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|* 13 |    INDEX RANGE SCAN             | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|  14 |  HASH UNIQUE                    |                                |     1 |    26 |     2  (50)| 00:00:01 |
|* 15 |   TABLE ACCESS BY INDEX ROWID   | MT_BIZ_FIN                     |     1 |    26 |     1   (0)| 00:00:01 |
|* 16 |    INDEX RANGE SCAN             | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
|* 17 |  TABLE ACCESS BY INDEX ROWID    | PM_ACCOUNT_SCENE               |     1 |    28 |     1   (0)| 00:00:01 |
|* 18 |   INDEX RANGE SCAN              | IDX_PM_ACCOUNT_SCENE_1         |     1 |       |     1   (0)| 00:00:01 |
|  19 |  SORT AGGREGATE                 |                                |     1 |    37 |            |          |
|* 20 |   TABLE ACCESS BY INDEX ROWID   | PM_TREAT_AUDIT                 |     1 |    37 |     1   (0)| 00:00:01 |
|* 21 |    INDEX RANGE SCAN             | PK_PM_TREAT_AUDIT              |     1 |       |     1   (0)| 00:00:01 |
|  22 |    SORT AGGREGATE               |                                |     1 |    37 |            |          |
|* 23 |     TABLE ACCESS BY INDEX ROWID | PM_TREAT_AUDIT                 |     1 |    37 |     1   (0)| 00:00:01 |
|* 24 |      INDEX RANGE SCAN           | PK_PM_TREAT_AUDIT              |     1 |       |     1   (0)| 00:00:01 |
|  25 |  SORT AGGREGATE                 |                                |     1 |    12 |            |          |
|* 26 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    12 |     1   (0)| 00:00:01 |
|* 27 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND1            |     1 |       |     1   (0)| 00:00:01 |
|  28 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 29 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 30 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  31 |  SORT AGGREGATE                 |                                |     1 |    21 |            |          |
|* 32 |   TABLE ACCESS BY INDEX ROWID   | PM_FEE_AUDIT                   |     1 |    21 |     1   (0)| 00:00:01 |
|* 33 |    INDEX RANGE SCAN             | PK_PM_FEE_AUDIT                |     1 |       |     1   (0)| 00:00:01 |
|  34 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 35 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 36 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  37 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 38 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 39 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  40 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|  41 |   INLIST ITERATOR               |                                |       |       |            |          |
|* 42 |    TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 43 |     INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  44 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 45 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 46 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  47 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 48 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 49 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  50 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 51 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 52 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  53 |  SORT AGGREGATE                 |                                |     1 |    16 |            |          |
|* 54 |   TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_FUND                |     1 |    16 |     1   (0)| 00:00:01 |
|* 55 |    INDEX RANGE SCAN             | PK_PM_ACCOUNT_FUND             |     1 |       |     1   (0)| 00:00:01 |
|  56 |  SORT AGGREGATE                 |                                |     1 |    39 |            |          |
|  57 |   TABLE ACCESS BY INDEX ROWID   | MT_PAY_RECORD_FIN              |     1 |    39 |     1   (0)| 00:00:01 |
|* 58 |    INDEX RANGE SCAN             | IDX_MT_PAY_RECORD_FIN_2        |     1 |       |     1   (0)| 00:00:01 |
|  59 |  SORT ORDER BY                  |                                |     3 |   855 | 24453   (1)| 00:04:54 |
|* 60 |   TABLE ACCESS BY INDEX ROWID   | MT_BIZ_FIN                     |     1 |   107 |     1   (0)| 00:00:01 |
|  61 |    NESTED LOOPS                 |                                |     3 |   855 | 24452   (1)| 00:04:54 |
|  62 |     NESTED LOOPS                |                                |     3 |   534 | 24451   (1)| 00:04:54 |
|  63 |      TABLE ACCESS BY INDEX ROWID| BS_CENTER                      |     1 |    14 |     1   (0)| 00:00:01 |
|* 64 |       INDEX UNIQUE SCAN         | PK_BS_CENTER                   |     1 |       |     1   (0)| 00:00:01 |
|* 65 |      TABLE ACCESS BY INDEX ROWID| PM_ACCOUNT_BIZ                 |     3 |   492 | 24450   (1)| 00:04:54 |
|* 66 |       INDEX RANGE SCAN          | IDX_PM_ACCOUNT_BIZ_SERIAL_DATE |   118K|       |    29   (7)| 00:00:01 |
|* 67 |     INDEX RANGE SCAN            | PK_MT_BIZ_FIN                  |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------------

其执行计划中现在是选访问表BS_CENTER后再与PM_ACCOUNT_BIZ执行嵌套循环连接,与我们设想的一样,这里慢的主要原因是因为要对表连接之后的记录执行order by a.hospital_id, a.serial_no操作。PM_ACCOUNT_BIZ表的查询条件中有and a.center_id in (‘430701’) and (a.fin_date between to_date(‘2014-01-01 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’) and
to_date(‘2014-12-31 23:59:59’, ‘yyyy-mm-dd hh24:mi:ss’)),而在CENTER_ID与FIN_DATE列上有存在复合索引。索引信息如下所示,在创建索引时,索引列的记录默认是按升序来存储的,而查询中要进行升序操作的列就是hospital_id,serial_no,而这两列已经存在复合索引IDX_PM_ACCOUNT_BIZ_SERIAL_NO中,而该索引中首列是CENTER_ID,我们只要把fin_date列加入这个复合索引中就可以让CBO在执行查询时使用该索引且能避免排序操作,而在IDX_PM_ACCOUNT_BIZ_SERIAL_DATE索引的有两列分别是CENTER_ID与FIN_DATE所以可以将这两个索引合并成一个新的索引(create index idx_pm_account_biz_center_id on pm_account_biz(center_id,hospital_id,serial_no,fin_date),并删除原先的这两个索引。

SQL> select a.index_name,a.column_name,a.column_position
  2   from user_ind_columns a  where a.table_name='PM_ACCOUNT_BIZ' and a.index_name='IDX_PM_ACCOUNT_BIZ_SERIAL_NO';
 
INDEX_NAME                     COLUMN_NAME                                                                      COLUMN_POSITION
------------------------------ -------------------------------------------------------------------------------- ---------------
IDX_PM_ACCOUNT_BIZ_SERIAL_NO   CENTER_ID                                                                                      1
IDX_PM_ACCOUNT_BIZ_SERIAL_NO   HOSPITAL_ID                                                                                    2
IDX_PM_ACCOUNT_BIZ_SERIAL_NO   SERIAL_NO                                                                                      3
SQL>  select a.index_name,a.column_name,a.column_position
  2   from user_ind_columns a  where a.table_name='PM_ACCOUNT_BIZ' and a.index_name='IDX_PM_ACCOUNT_BIZ_SERIAL_DATE';
 
INDEX_NAME                     COLUMN_NAME                                                                      COLUMN_POSITION
------------------------------ -------------------------------------------------------------------------------- ---------------
IDX_PM_ACCOUNT_BIZ_SERIAL_DATE CENTER_ID                                                                                      1
IDX_PM_ACCOUNT_BIZ_SERIAL_DATE FIN_DATE      

我们在创建新索引后,执行修改后的SQL语句:

SQL> set timing on
SQL> set autotrace traceonly  
SQL> select  t1.account_id,
  2         t1.hospital_id,
  3         (select hosp_level
  4            from bs_hospital a
  5           where t1.hospital_id = a.hospital_id
  6             and a.valid_flag = '1') as hosp_level,
  7         t1.serial_no,
  8         t1.icd,
  9         (select t4.disease
 10            from bs_disease t4
 11           where t1.icd = t4.icd(+)
 12             and t4.center_id = nvl(t1.catalog_center, t1.center_id)) as disease,
 13         t1.biz_type,
 14         t1.treatment_type,
 15         t1.disease_type,
 16         t1.reimburse_flag,
 17         t1.corp_id,
 18         t1.corp_type_code,
 19         t1.corp_name,
 20         t1.indi_id,
 21         t1.pers_type,
 22         t1.center_id,
 23         t1.district_code,
 24         t1.out_type,
 25         t1.biz_flag,
 26         t1.biz_flag_old,
 27         t1.name,
 28         t1.idcard,
 29         t1.sex,
 30         t1.begin_date,
 31         t1.end_date,
 32         t1.fin_date,
 33         t1.reg_date,
 34         t1.disease_fee,
 35         t1.violate_flag,
 36         t1.patient_id,
 37         t1.in_days,
 38         decode(t1.allow_treat, '0', 0, 1) As allow_treat,
 39         Decode(T1.Treatment_Type_Last,
 40                '',
 41                T1.Treatment_Type,
 42                Decode(T1.Treatment_Type,
 43                       '120',
 44                       '120',
 45                       Decode(T1.Biz_Type,
 46                              '41',
 47                              '扣减已记帐金额',
 48                              '42',
 49                              '扣减已记帐金额',
 50                              T1.Treatment_Type_Last))) As Treatment_Type_Last,
 51         t1.violate,
 52         t1.pay_money,
 53         t1.pay_fund,
 54         t1.audit_money,
 55         '0' as finish_flag,
 56         t1.MONTH_DECL_SN,
 57         t1.deal_flag,
 58         t1.audit_deal_flag,
 59         t1.declare_icd_guide,
 60         t1.declare_audit_fee,
 61         t1.declare_audit_fee_cl,
 62         t1.first_icd_guide,
 63         t1.first_audit_fee,
 64         t1.first_audit_fee_cl,
 65         t1.second_icd_guide,
 66         t1.second_audit_fee,
 67         t1.second_audit_fee_cl,
 68         t1.PAY_otherfee,
 69         t1.PAY_FUND003,
 70         t1.PAY_FUND001,
 71         t1.PAY_FUND201,
 72         t1.PAY_FUND301,
 73         t1.PAY_FUND999,
 74         t1.PAY_FUND996,
 75         t1.PAY_begin
 76    from (select a.account_id,
 77                 a.hospital_id,
 78                 a.serial_no,
 79                 a.center_id,
 80                 a.icd,
 81                 a.biz_type,
 82                 a.treatment_type,
 83                 a.reimburse_flag,
 84                 a.corp_id,
 85                 a.corp_type_code,
 86                 t2.corp_name,
 87                 a.indi_id,
 88                 a.pers_type,
 89                 center.catalog_center,
 90                 t2.district_code,
 91                 decode(t2.center_id, t2.district_code, 0, 1) as out_type,
 92                 a.biz_flag,
 93                 a.biz_flag_old,
 94                 t2.name,
 95                 t2.idcard,
 96                 t2.sex,
 97                 t2.begin_date,
 98                 t2.end_date,
 99                 a.fin_date,
100                 t2.reg_date,
101                 a.violate_flag,
102                 (select mbf.disease_type
103                    from mt_biz_fin mbf
104                   where mbf.valid_flag = '1'
105                     and mbf.hospital_id = a.hospital_id
106                     and mbf.serial_no = a.serial_no) disease_type,
107                 (Select aa1.allow_treat
108                    From pm_account_scene aa1
109                   where aa1.hospital_id = a.hospital_id
110                     and aa1.serial_no = a.serial_no
111                     and aa1.valid_flag = '1') as allow_treat,
112                 (select distinct bb1.patient_id
113                    from mt_biz_fin bb1
114                   where bb1.hospital_id = a.hospital_id
115                     and bb1.serial_no = a.serial_no
116                     and bb1.valid_flag = '1') as patient_id,
117                 (select distinct bb1.in_days
118                    from mt_biz_fin bb1
119                   where bb1.hospital_id = a.hospital_id
120                     and bb1.serial_no = a.serial_no
121                     and bb1.valid_flag = '1') as in_days,
122                 (select Max(audit_treat_value)
123                    from pm_treat_audit aa
124                   where aa.account_id = a.account_id
125                     and aa.audit_staff_id = '2208'
126                     and aa.audit_phase = '1'
127                     and aa.valid_flag = '1') as treatment_type_last,
128                 a.violate,
129                 nvl((SELECT sum(pm_account_fund.pay_money)
130                       FROM pm_account_fund
131                      WHERE a.account_id = pm_account_fund.account_id
132                        AND pm_account_fund.valid_flag = '1'),
133                     0) as pay_money,
134                 nvl((SELECT sum(pm_account_fund.pay_money)
135                       FROM pm_account_fund
136                      WHERE a.account_id = pm_account_fund.account_id
137                        AND pm_account_fund.valid_flag = '1'
138                        AND pm_account_fund.fund_id not in
139                            ('003', '999', '996', '997')),
140                     0) as pay_fund,
141                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
142                       FROM PM_ACCOUNT_FUND
143                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
144                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
145                        AND PM_ACCOUNT_FUND.FUND_ID = '003'),
146                     0) AS PAY_FUND003,
147                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
148                       FROM PM_ACCOUNT_FUND
149                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
150                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
151                        AND PM_ACCOUNT_FUND.FUND_ID in ('801', '001')),
152                     0) AS PAY_FUND001,
153                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
154                       FROM PM_ACCOUNT_FUND
155                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
156                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
157                        AND PM_ACCOUNT_FUND.FUND_ID = '201'),
158                     0) AS PAY_FUND201,
159                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
160                       FROM PM_ACCOUNT_FUND
161                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
162                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
163                        AND PM_ACCOUNT_FUND.FUND_ID = '301'),
164                     0) AS PAY_FUND301,
165                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
166                       FROM PM_ACCOUNT_FUND
167                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
168                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
169                        AND PM_ACCOUNT_FUND.FUND_ID = '999'),
170                     0) AS PAY_FUND999,
171                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
172                       FROM PM_ACCOUNT_FUND
173                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
174                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
175                        AND PM_ACCOUNT_FUND.FUND_ID = '996'),
176                     0) AS PAY_FUND996,
177                 NVL((SELECT SUM(PM_ACCOUNT_FUND.PAY_MONEY)
178                       FROM PM_ACCOUNT_FUND
179                      WHERE A.ACCOUNT_ID = PM_ACCOUNT_FUND.ACCOUNT_ID
180                        AND PM_ACCOUNT_FUND.VALID_FLAG = '1'
181                        AND PM_ACCOUNT_FUND.FUND_ID not in
182                            ('801', '001', '003', '201', '999')),
183                     0) AS PAY_otherfee,
184                 NVL((SELECT SUM(mprf.real_pay)
185                       FROM mt_pay_record_fin mprf
186                      WHERE A.hospital_id = mprf.hospital_id
187                        AND A.serial_no = mprf.serial_no
188                        AND mprf.policy_item_code = 'S01'),
189                     0) AS PAY_begin,
190                 (select nvl(sum(b.AUDIT_MONEY), 0)
191                    from pm_fee_audit b
192                   where a.account_id = b.account_id
193                     and b.audit_staff_id = '2208'
194                     and b.AUDIT_TYPE = '1'
195                     and b.AUDIT_PHASE = '1'
196                     and b.valid_flag = '1') as audit_money,
197                 a.MONTH_DECL_SN,
198                 a.deal_flag,
199                 decode(nvl(a.disease_fee, 0),
200                        0,
201                        nvl((select nvl(max(ration), 0)
202                              from pm_ration pr
203                             where pr.center_id = a.center_id
204                               and pr.hospital_id = a.hospital_id
205                               and pr.treatment_type = a.treatment_type
206                               and pr.year = to_char(a.fin_date, 'yyyy')
207                               and (decode(a.pers_type,
208                                           '21',
209                                           '21',
210                                           '22',
211                                           '21',
212                                           '23',
213                                           '21',
214                                           '24',
215                                           '21',
216                                           '41',
217                                           '21',
218                                           '3',
219                                           '12',
220                                           '4',
221                                           '12',
222                                           '5',
223                                           '12',
224                                           '7',
225                                           '12',
226                                           '2') = pr.insr_no or
227                                   pr.insr_no = '0')
228                               and rownum < 2
229                               and pr.biz_flag = a.biz_flag),
230                            0),
231                        a.disease_fee) as disease_fee,
232                 a.audit_flag as audit_deal_flag,
233                 a.declare_icd_guide as declare_icd_guide,
234                 a.declare_audit_fee as declare_audit_fee,
235                 a.declare_audit_fee_cl as declare_audit_fee_cl,
236                 a.first_icd_guide as first_icd_guide,
237                 a.first_audit_fee as first_audit_fee,
238                 a.first_audit_fee_cl as first_audit_fee_cl,
239                 decode(a.biz_type,
240                        '12',
241                        decode(a.biz_flag_old, 5, '4304001', a.second_icd_guide),
242                        a.second_icd_guide) second_icd_guide,
243                 a.second_audit_fee as second_audit_fee,
244                 a.second_audit_fee_cl as second_audit_fee_cl
245            from bs_center center,pm_account_biz a,mt_biz_fin t2
246           where 1 = 1
247             and ('0' = 'A' or a.reimburse_flag ='0')
248             and ('00' = '00' or a.biz_type ='00')
249             and a.center_id in ('430701')
250             and (a.fin_date between to_date('2014-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
251                 to_date('2014-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss'))
252             and a.valid_flag = '1'
253             and a.deal_flag in ('0', '1', '2')
254             and (nvl(a.staff1_finish_flag, '0') ='0' and
255                 nvl(a.staff2_finish_flag, '0') = '0' and
256                 nvl(a.staff3_finish_flag, '0') = '0')
257             and a.serial_no = t2.serial_no
258             AND a.hospital_id = t2.hospital_id
259             AND t2.valid_flag = '1'
260             and a.center_id = center.center_id(+)
261             and center.center_id in ('430701')          
262             order by a.hospital_id, a.serial_no
263             )t1
264  ;

189998 rows selected.

Elapsed: 00:01:37.15

Execution Plan
----------------------------------------------------------
Plan hash value: 1778792342

---------------------------------------------------------------------------------------------------------------
| Id  | Operation                      | Name                         | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |                              |     3 |   855 | 20157   (1)| 00:04:02 |
|*  1 |  TABLE ACCESS BY INDEX ROWID   | BS_HOSPITAL                  |     1 |    16 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN            | PK_BS_HOSPITAL               |     1 |       |     1   (0)| 00:00:01 |
|   3 |  TABLE ACCESS BY INDEX ROWID   | BS_DISEASE                   |     1 |    33 |     1   (0)| 00:00:01 |
|*  4 |   INDEX RANGE SCAN             | INX_BS_DISEASE_01            |     1 |       |     1   (0)| 00:00:01 |
|*  5 |  TABLE ACCESS BY INDEX ROWID   | MT_BIZ_FIN                   |     1 |    26 |     1   (0)| 00:00:01 |
|*  6 |   INDEX RANGE SCAN             | PK_MT_BIZ_FIN                |     1 |       |     1   (0)| 00:00:01 |
|   7 |  SORT AGGREGATE                |                              |     1 |    34 |            |          |
|*  8 |   COUNT STOPKEY                |                              |       |       |            |          |
|*  9 |    TABLE ACCESS BY INDEX ROWID | PM_RATION                    |     1 |    34 |     1   (0)| 00:00:01 |
|* 10 |     INDEX RANGE SCAN           | IDX_PM_RATION_1              |     1 |       |     1   (0)| 00:00:01 |
|  11 |  HASH UNIQUE                   |                              |     1 |    26 |     2  (50)| 00:00:01 |
|* 12 |   TABLE ACCESS BY INDEX ROWID  | MT_BIZ_FIN                   |     1 |    26 |     1   (0)| 00:00:01 |
|* 13 |    INDEX RANGE SCAN            | PK_MT_BIZ_FIN                |     1 |       |     1   (0)| 00:00:01 |
|  14 |  HASH UNIQUE                   |                              |     1 |    26 |     2  (50)| 00:00:01 |
|* 15 |   TABLE ACCESS BY INDEX ROWID  | MT_BIZ_FIN                   |     1 |    26 |     1   (0)| 00:00:01 |
|* 16 |    INDEX RANGE SCAN            | PK_MT_BIZ_FIN                |     1 |       |     1   (0)| 00:00:01 |
|* 17 |  TABLE ACCESS BY INDEX ROWID   | PM_ACCOUNT_SCENE             |     1 |    28 |     1   (0)| 00:00:01 |
|* 18 |   INDEX RANGE SCAN             | IDX_PM_ACCOUNT_SCENE_1       |     1 |       |     1   (0)| 00:00:01 |
|  19 |  SORT AGGREGATE                |                              |     1 |    37 |            |          |
|* 20 |   TABLE ACCESS BY INDEX ROWID  | PM_TREAT_AUDIT               |     1 |    37 |     1   (0)| 00:00:01 |
|* 21 |    INDEX RANGE SCAN            | PK_PM_TREAT_AUDIT            |     1 |       |     1   (0)| 00:00:01 |
|  22 |    SORT AGGREGATE              |                              |     1 |    37 |            |          |
|* 23 |     TABLE ACCESS BY INDEX ROWID| PM_TREAT_AUDIT               |     1 |    37 |     1   (0)| 00:00:01 |
|* 24 |      INDEX RANGE SCAN          | PK_PM_TREAT_AUDIT            |     1 |       |     1   (0)| 00:00:01 |
|  25 |  SORT AGGREGATE                |                              |     1 |    12 |            |          |
|* 26 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    12 |     1   (0)| 00:00:01 |
|* 27 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND1          |     1 |       |     1   (0)| 00:00:01 |
|  28 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 29 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 30 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  31 |  SORT AGGREGATE                |                              |     1 |    21 |            |          |
|* 32 |   TABLE ACCESS BY INDEX ROWID  | PM_FEE_AUDIT                 |     1 |    21 |     1   (0)| 00:00:01 |
|* 33 |    INDEX RANGE SCAN            | PK_PM_FEE_AUDIT              |     1 |       |     1   (0)| 00:00:01 |
|  34 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 35 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 36 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  37 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 38 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 39 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  40 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|  41 |   INLIST ITERATOR              |                              |       |       |            |          |
|* 42 |    TABLE ACCESS BY INDEX ROWID | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 43 |     INDEX RANGE SCAN           | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  44 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 45 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 46 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  47 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 48 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 49 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  50 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 51 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 52 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  53 |  SORT AGGREGATE                |                              |     1 |    16 |            |          |
|* 54 |   TABLE ACCESS BY INDEX ROWID  | PM_ACCOUNT_FUND              |     1 |    16 |     1   (0)| 00:00:01 |
|* 55 |    INDEX RANGE SCAN            | PK_PM_ACCOUNT_FUND           |     1 |       |     1   (0)| 00:00:01 |
|  56 |  SORT AGGREGATE                |                              |     1 |    39 |            |          |
|  57 |   TABLE ACCESS BY INDEX ROWID  | MT_PAY_RECORD_FIN            |     1 |    39 |     1   (0)| 00:00:01 |
|* 58 |    INDEX RANGE SCAN            | IDX_MT_PAY_RECORD_FIN_2      |     1 |       |     1   (0)| 00:00:01 |
|* 59 |  TABLE ACCESS BY INDEX ROWID   | MT_BIZ_FIN                   |     1 |   107 |     1   (0)| 00:00:01 |
|  60 |   NESTED LOOPS                 |                              |     3 |   855 | 20157   (1)| 00:04:02 |
|  61 |    NESTED LOOPS                |                              |     3 |   534 | 20156   (1)| 00:04:02 |
|  62 |     TABLE ACCESS BY INDEX ROWID| BS_CENTER                    |     1 |    14 |     1   (0)| 00:00:01 |
|* 63 |      INDEX UNIQUE SCAN         | PK_BS_CENTER                 |     1 |       |     1   (0)| 00:00:01 |
|* 64 |     TABLE ACCESS BY INDEX ROWID| PM_ACCOUNT_BIZ               |     3 |   492 | 20155   (1)| 00:04:02 |
|* 65 |      INDEX RANGE SCAN          | IDX_PM_ACCOUNT_BIZ_CENTER_ID |   118K|       |   178   (4)| 00:00:03 |
|* 66 |    INDEX RANGE SCAN            | PK_MT_BIZ_FIN                |     1 |       |     1   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("A"."VALID_FLAG"='1')
   2 - access("A"."HOSPITAL_ID"=:B1)
   4 - access("T4"."CENTER_ID"=NVL(:B1,:B2) AND "T4"."ICD"=:B3)
   5 - filter("MBF"."VALID_FLAG"='1')
   6 - access("MBF"."HOSPITAL_ID"=:B1 AND "MBF"."SERIAL_NO"=:B2)
   8 - filter(ROWNUM<2)
   9 - filter(("PR"."INSR_NO"=TO_NUMBER(DECODE(:B1,'21','21','22','21','23','21','24','21','41','21','3
              ','12','4','12','5','12','7','12','2')) OR "PR"."INSR_NO"=0) AND TO_NUMBER("PR"."BIZ_FLAG")=:B2)
  10 - access("PR"."YEAR"=TO_NUMBER(TO_CHAR(:B1,'yyyy')) AND "PR"."HOSPITAL_ID"=:B2 AND
              "PR"."TREATMENT_TYPE"=:B3 AND "PR"."CENTER_ID"=:B4)
       filter("PR"."TREATMENT_TYPE"=:B1 AND "PR"."CENTER_ID"=:B2)
  12 - filter("BB1"."VALID_FLAG"='1')
  13 - access("BB1"."HOSPITAL_ID"=:B1 AND "BB1"."SERIAL_NO"=:B2)
  15 - filter("BB1"."VALID_FLAG"='1')
  16 - access("BB1"."HOSPITAL_ID"=:B1 AND "BB1"."SERIAL_NO"=:B2)
  17 - filter("AA1"."VALID_FLAG"='1')
  18 - access("AA1"."HOSPITAL_ID"=:B1 AND "AA1"."SERIAL_NO"=:B2)
  20 - filter("AA"."VALID_FLAG"='1')
  21 - access("AA"."ACCOUNT_ID"=:B1 AND "AA"."AUDIT_PHASE"='1' AND "AA"."AUDIT_STAFF_ID"='2208')
       filter("AA"."AUDIT_STAFF_ID"='2208' AND "AA"."AUDIT_PHASE"='1')
  23 - filter("AA"."VALID_FLAG"='1')
  24 - access("AA"."ACCOUNT_ID"=:B1 AND "AA"."AUDIT_PHASE"='1' AND "AA"."AUDIT_STAFF_ID"='2208')
       filter("AA"."AUDIT_STAFF_ID"='2208' AND "AA"."AUDIT_PHASE"='1')
  26 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  27 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1)
  29 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  30 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1)
       filter("PM_ACCOUNT_FUND"."FUND_ID"<>'003' AND "PM_ACCOUNT_FUND"."FUND_ID"<>'999' AND
              "PM_ACCOUNT_FUND"."FUND_ID"<>'996' AND "PM_ACCOUNT_FUND"."FUND_ID"<>'997')
  32 - filter("B"."VALID_FLAG"='1')
  33 - access("B"."ACCOUNT_ID"=:B1 AND "B"."AUDIT_TYPE"='1' AND "B"."AUDIT_PHASE"='1' AND
              "B"."AUDIT_STAFF_ID"='2208')
       filter("B"."AUDIT_STAFF_ID"='2208')
  35 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  36 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1)
       filter("PM_ACCOUNT_FUND"."FUND_ID"<>'003' AND "PM_ACCOUNT_FUND"."FUND_ID"<>'999' AND
              "PM_ACCOUNT_FUND"."FUND_ID"<>'001' AND "PM_ACCOUNT_FUND"."FUND_ID"<>'801' AND
              "PM_ACCOUNT_FUND"."FUND_ID"<>'201')
  38 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  39 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND "PM_ACCOUNT_FUND"."FUND_ID"='003')
  42 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  43 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND ("PM_ACCOUNT_FUND"."FUND_ID"='001' OR
              "PM_ACCOUNT_FUND"."FUND_ID"='801'))
  45 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  46 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND "PM_ACCOUNT_FUND"."FUND_ID"='201')
  48 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  49 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND "PM_ACCOUNT_FUND"."FUND_ID"='301')
  51 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  52 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND "PM_ACCOUNT_FUND"."FUND_ID"='999')
  54 - filter("PM_ACCOUNT_FUND"."VALID_FLAG"='1')
  55 - access("PM_ACCOUNT_FUND"."ACCOUNT_ID"=:B1 AND "PM_ACCOUNT_FUND"."FUND_ID"='996')
  58 - access("MPRF"."HOSPITAL_ID"=:B1 AND "MPRF"."SERIAL_NO"=:B2 AND "MPRF"."POLICY_ITEM_CODE"='S01')
  59 - filter("T2"."VALID_FLAG"='1')
  63 - access("CENTER"."CENTER_ID"='430701')
  64 - filter(NVL("A"."STAFF2_FINISH_FLAG",'0')='0' AND NVL("A"."STAFF1_FINISH_FLAG",'0')='0' AND
              NVL("A"."STAFF3_FINISH_FLAG",'0')='0' AND "A"."REIMBURSE_FLAG"='0' AND "A"."VALID_FLAG"='1' AND
              ("A"."DEAL_FLAG"='0' OR "A"."DEAL_FLAG"='1' OR "A"."DEAL_FLAG"='2'))
  65 - access("A"."CENTER_ID"="CENTER"."CENTER_ID" AND "A"."FIN_DATE">=TO_DATE(' 2014-01-01 00:00:00',
              'syyyy-mm-dd hh24:mi:ss') AND "A"."FIN_DATE"< =TO_DATE(' 2014-12-31 23:59:59', 'syyyy-mm-dd               hh24:mi:ss'))        filter("A"."FIN_DATE">=TO_DATE(' 2014-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
              "A"."CENTER_ID"='430701' AND "A"."FIN_DATE"< =TO_DATE(' 2014-12-31 23:59:59', 'syyyy-mm-dd hh24:mi:ss'))
  66 - access("A"."HOSPITAL_ID"="T2"."HOSPITAL_ID" AND "A"."SERIAL_NO"="T2"."SERIAL_NO")


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
    2319157  consistent gets
       8301  physical reads
        576  redo size
    2809640  bytes sent via SQL*Net to client
      24709  bytes received via SQL*Net from client
       1335  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      189998  rows processed

从上面的实际执行情况可以看到返回了接近19万条记录花费的时间是1分37秒,也就是97秒与3286秒,已经是几十倍的提升了。

小结:这里优化的方法是通过修改SQL语句,改变表连接的方法,去掉不必要的分组(group by)操作,创建来合理的复合索引来避免排序(order by)来达到性能的提升。

乱用Hint造成性能问题案例一

某系统上午9点到11点的AWR报告中TOP SQL,其中消耗时间最长的花了9770秒,该SQL_ID为36cbabzyq13gy
这条SQL语句与SQL_ID为0frcad5600xdu,g1a0qu2b42j83所对应的SQL语句除了文本值不一样外,其它部分是相同的,这里没有使用绑定变量

SQL ordered by Elapsed Time
Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
% Total DB Time is the Elapsed Time of the SQL statement divided into the Total Database Time multiplied by 100
Elapsed Time (s)	CPU Time (s)	Executions	Elap per Exec (s)	% Total DB Time	          SQL Id	         SQL Module	        SQL Text
9,770	            6,418	        1	          9769.94	            16.02	           36cbabzyq13gy	 w3wp.exe	        SELECT /*+ index(lt, PK_LV_U...
5,831	            3,851	        2	          2915.28	             9.56	           0frcad5600xdu	 w3wp.exe	        SELECT /*+ index(lt, PK_LV_U...
2,495	            1,643	        1	          2495.48	             4.09	           g1a0qu2b42j83	 w3wp.exe	        SELECT /*+ index(lt, PK_LV_U...
2,348	            1,482	        62	            37.86	             3.85	           a7dkwg8uhrwkj	 JDBC Thin  Client	select * from ( select a.hosp...
772	              341	        22	            35.10	             1.27	           2vpny9ut5dcm6	 JDBC Thin  Client	select t.pay_type as pay_...
670	              438	        29	            23.10	             1.10	           acj1640jvr3u5	 JDBC Thin  Client	select t.biz_flag, t1.name, t1...
618	              317	        60	            10.30	             1.01	           ggrctzgtcg14s	 JDBC Thin  Client	select t.pay_type as pay_...
617	              39	        2	           308.52	             1.01	           c5m1092x9vg2y	 JDBC Thin  Client	select w.hospital_id, t.hospit...
605	              398	        1	           604.51	             0.99	           3yy1wbuvsxm93	 w3wp.exe	        SELECT /*+ index(lt, PK_LV_U...
381	              55	        1	           380.52	             0.62	           6q1xuznmvsu5d	 w3wp.exe	        SELECT t_center.center_name, ...

从awrsqrpt报告中可以看到,该SQL的逻辑读为3亿多次

Stat Name                   Statement Total  Per Execution  % Snap Total
Elapsed Time (ms)           9,769,937        9,769,936.85    16.02
CPU Time (ms)               6,417,920        6,417,920.27    20.69
Executions                  1      
Buffer Gets                 361,831,845      361,831,845.00  15.35
Disk Reads                  23,989  23,      989.00          0.05
Parse Calls                 1                1.00            0.00
Rows                        0                0.00   
User I/O Wait Time (ms)     27,723      
Cluster Wait Time (ms)      0      
Application Wait Time (ms)  0      
Concurrency Wait Time (ms)  460      
Invalidations               0      
Version Count               2      
Sharable Mem(KB)            275     

SQL_ID为36cbabzyq13gy的SQL语句如下:

SELECT /*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */
 bi.indi_id,
 bi.name,
 pt.pers_name,
 bs.sex_name,
 lt.pay_money,
 bi.idcard,
 bi.birthday,
 bf.headed_name,
 lt.fac_pay_date,
 lbb.audit_man,
 tab_hosp.hospital_name as hospital_name,
 to_char(lbb.make_bill_tm, 'yyyy-mm-dd') as make_bill_tm,
 bf.telephone,
 nvl((decode(lt.intensive_disability_flag,
             1,
             decode(lt.lowflag, 1, '重症伤残,', '重症伤残'),
             '') ||
     decode(lt.lowflag, 1, decode(lt.nothing_flag, 1, '低保,', '低保'), '') ||
     decode(lt.nothing_flag, 1, '三无', '')),
     '标准') as subsidykide
  FROM lv_urban_topay_tmp lt,
       bs_insured bi,
       bs_sex bs,
       bs_person_type pt,
       bs_pres_insur bpi,
       bs_family bf,
       lv_busi_bill lbb,
       lv_busi_record lbr,
       lv_busi_assign lba,
       (select bh.hospital_name, bph.indi_id
          from bs_pers_hosp bph, bs_hospital bh
         where bph.hospital_id = bh.hospital_id
           and bph.first_flag = 1
           and bph.end_year = '2015') tab_hosp
 WHERE nvl(lt.busi_asg_no, 0) <> 0
   AND nvl(lt.busi_asg_no, 0) not in (-999, -998, -997, -981, -980)
   AND lt.fac_pay_date is not null
   AND bi.indi_id = lt.indi_id
   AND bs.sex = bi.sex
   AND bi.indi_id = tab_hosp.indi_id(+)
   AND lbr.busi_reco_no = lba.busi_reco_no
   AND lbr.busi_bill_sn = lbb.busi_bill_sn
   AND lt.center_id = lbb.center_id
   AND lt.busi_asg_no = lba.busi_asg_no
   AND lt.indi_id = bi.indi_id
   AND pt.pers_type = bi.pers_type
   AND bpi.indi_id = bi.indi_id
   AND lt.center_id = pt.center_id
   AND bf.family_id = bi.family_id
   AND bf.family_sta = 1
   AND bi.indi_sta = 1
   AND bpi.indi_join_sta = 1
   AND bf.center_id = lt.center_id
   AND bf.corp_id = lt.corp_id
   AND lt.policy_item_code like '%INDI_TOPAY'
   AND lt.corp_id = '19159'
   AND bpi.insr_detail_code = 21
   AND lt.center_id = '430726'
   AND lt.curr_year = '2015'
   AND lt.fac_pay_date >= to_date('2014-12-01 00:00:00',
                                  'yyyy-MM-dd hh24:mi:ss')
   AND lt.fac_pay_date < =
       to_date('2015-01-05 23:59:59', 'yyyy-MM-dd hh24:mi:ss')
   and exists (select 'X'
          FROM lv_busi_bill       lbb,
               lv_busi_record     lbr,
               lv_busi_assign     lba,
               lv_urban_topay_tmp lutt
         WHERE lbr.busi_reco_no = lba.busi_reco_no
           AND lbr.busi_bill_sn = lbb.busi_bill_sn
           AND lbb.center_id = '430726'
           AND lutt.corp_id = '19159'
           AND lutt.center_id = lbb.center_id
           AND lutt.busi_asg_no = lba.busi_asg_no
           and lba.busi_asg_no = lt.busi_asg_no
           and lutt.indi_id = bi.indi_id)
 order by lt.fac_pay_date, bi.indi_id, bi.name

通过执地xplan脚本来获得SQL_ID为36cbabzyq13gy的执行计划,其执行计划如下

-----------------------------------------------------------------------------------------------------------------------------------
| Id  | Order | Operation                               | Name                           | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-----------------------------------------------------------------------------------------------------------------------------------
|   0 |    45 | SELECT STATEMENT                        |                                |        |       | 20484 (100)|          |
|   1 |    44 |  SORT ORDER BY                          |                                |      1 |   290 | 20484   (3)| 00:04:06 |
|   2 |    43 |   NESTED LOOPS                          |                                |      1 |   290 | 20483   (3)| 00:04:06 |
|   3 |    40 |    NESTED LOOPS                         |                                |      1 |   265 | 20482   (3)| 00:04:06 |
|   4 |    37 |     NESTED LOOPS                        |                                |      1 |   254 | 20481   (3)| 00:04:06 |
|   5 |    34 |      NESTED LOOPS                       |                                |      1 |   242 | 20480   (3)| 00:04:06 |
|   6 |    32 |       NESTED LOOPS                      |                                |      1 |   230 | 20479   (3)| 00:04:06 |
|   7 |    29 |        HASH JOIN                        |                                |    168 | 27720 | 17063   (3)| 00:03:25 |
|   8 |    13 |         VIEW                            | VW_SQ_1                        |    168 |  2016 |  2441   (1)| 00:00:30 |
|   9 |    12 |          HASH UNIQUE                    |                                |    168 |  9408 |            |          |
|  10 |    11 |           NESTED LOOPS                  |                                |    168 |  9408 |  2441   (1)| 00:00:30 |
|  11 |     8 |            NESTED LOOPS                 |                                |   1718 | 75592 |  2097   (1)| 00:00:26 |
|  12 |     5 |             NESTED LOOPS                |                                |   1758 | 58014 |  1745   (1)| 00:00:21 |
|  13 |     2 |              TABLE ACCESS BY INDEX ROWID| LV_URBAN_TOPAY_TMP             |   1742 | 36582 |  1397   (1)| 00:00:17 |
|  14 |     1 |               INDEX RANGE SCAN          | IDX_LV_URBAN_TOPAY_TMP_CORP_ID |  18770 |       |    14   (0)| 00:00:01 |
|  15 |     4 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_ASSIGN                 |      1 |    12 |     1   (0)| 00:00:01 |
|  16 |     3 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_ASSIGN              |      1 |       |     1   (0)| 00:00:01 |
|  17 |     7 |             TABLE ACCESS BY INDEX ROWID | LV_BUSI_RECORD                 |      1 |    11 |     1   (0)| 00:00:01 |
|  18 |     6 |              INDEX UNIQUE SCAN          | PK_LV_BUSI_RECORD              |      1 |       |     1   (0)| 00:00:01 |
|  19 |    10 |            TABLE ACCESS BY INDEX ROWID  | LV_BUSI_BILL                   |      1 |    12 |     1   (0)| 00:00:01 |
|  20 |     9 |             INDEX UNIQUE SCAN           | PK_LV_BUSI_BILL                |      1 |       |     1   (0)| 00:00:01 |
|  21 |    28 |         NESTED LOOPS OUTER              |                                |    123 | 18819 | 14622   (3)| 00:02:56 |
|  22 |    21 |          HASH JOIN                      |                                |    123 | 13776 | 14375   (3)| 00:02:53 |
|  23 |    15 |           TABLE ACCESS BY INDEX ROWID   | BS_FAMILY                      |    102 |  3264 |    93   (0)| 00:00:02 |
|  24 |    14 |            INDEX RANGE SCAN             | IDX_BS_FAMILY_CORP_ID          |   1203 |       |     1   (0)| 00:00:01 |
|  25 |    20 |           HASH JOIN                     |                                |   1081K|    82M| 14272   (3)| 00:02:52 |
|  26 |    16 |            TABLE ACCESS FULL            | BS_PERSON_TYPE                 |     11 |   198 |     3   (0)| 00:00:01 |
|  27 |    19 |            HASH JOIN                    |                                |   1080K|    63M| 14258   (3)| 00:02:52 |
|  28 |    17 |             TABLE ACCESS FULL           | BS_SEX                         |      4 |    24 |     3   (0)| 00:00:01 |
|  29 |    18 |             TABLE ACCESS FULL           | BS_INSURED                     |   1080K|    57M| 14244   (3)| 00:02:51 |
|  30 |    27 |          VIEW PUSHED PREDICATE          |                                |      1 |    41 |     2   (0)| 00:00:01 |
|  31 |    26 |           NESTED LOOPS                  |                                |      1 |    57 |     2   (0)| 00:00:01 |
|  32 |    23 |            TABLE ACCESS BY INDEX ROWID  | BS_PERS_HOSP                   |      1 |    25 |     1   (0)| 00:00:01 |
|  33 |    22 |             INDEX RANGE SCAN            | PK_BS_PERS_HOSP                |      2 |       |     1   (0)| 00:00:01 |
|  34 |    25 |            TABLE ACCESS BY INDEX ROWID  | BS_HOSPITAL                    |      1 |    32 |     1   (0)| 00:00:01 |
|  35 |    24 |             INDEX UNIQUE SCAN           | PK_BS_HOSPITAL                 |      1 |       |     1   (0)| 00:00:01 |
|  36 |    31 |        TABLE ACCESS BY INDEX ROWID      | LV_URBAN_TOPAY_TMP             |      1 |    65 |  3416   (4)| 00:00:41 |
|  37 |    30 |         INDEX FULL SCAN                 | PK_LV_URBAN_TOPAY_TMP          |      1 |       |  3416   (4)| 00:00:41 |
|  38 |    33 |       INDEX UNIQUE SCAN                 | INDEX_BS_PRES_INSUR_UNIQUE     |      1 |    12 |     1   (0)| 00:00:01 |
|  39 |    36 |      TABLE ACCESS BY INDEX ROWID        | LV_BUSI_ASSIGN                 |      1 |    12 |     1   (0)| 00:00:01 |
|  40 |    35 |       INDEX UNIQUE SCAN                 | PK_LV_BUSI_ASSIGN              |      1 |       |     1   (0)| 00:00:01 |
|  41 |    39 |     TABLE ACCESS BY INDEX ROWID         | LV_BUSI_RECORD                 |      1 |    11 |     1   (0)| 00:00:01 |
|  42 |    38 |      INDEX UNIQUE SCAN                  | PK_LV_BUSI_RECORD              |      1 |       |     1   (0)| 00:00:01 |
|  43 |    42 |    TABLE ACCESS BY INDEX ROWID          | LV_BUSI_BILL                   |      1 |    25 |     1   (0)| 00:00:01 |
|  44 |    41 |     INDEX UNIQUE SCAN                   | PK_LV_BUSI_BILL                |      1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------------------------------------


***********
Table Level
***********


Table                   Number                 Empty Average    Chain Average Global User           Sample Date
Name                   of Rows   Blocks       Blocks   Space    Count Row Len Stats  Stats            Size MM-DD-YYYY
--------------- -------------- -------- ------------ ------- -------- ------- ------ ------ -------------- ----------
LV_URBAN_TOPAY_     22,991,252  580,702        8,018     904        5     175 YES    NO          5,747,813 12-08-2014
TMP


Column                    Column                       Distinct          Number     Number Global User           Sample Date
Name                      Details                        Values Density Buckets      Nulls Stats  Stats            Size MM-DD-YYYY
------------------------- ------------------------ ------------ ------- ------- ---------- ------ ------ -------------- ----------
URBAN_TOPAY_SN            NUMBER(12,0) NOT NULL      17,882,394       0     254          0 YES    NO          5,747,813 12-08-2014
CORP_ID                   NUMBER(12,0) NOT NULL             848       0     254          0 YES    NO              5,935 12-08-2014
INDI_ID                   NUMBER(12,0) NOT NULL       1,934,475       0     254          0 YES    NO          5,747,813 12-08-2014
NAME                      VARCHAR2(20)                  380,042       0     254          0 YES    NO            580,870 12-08-2014
IDCARD                    VARCHAR2(20)                  857,905       0       1  4,611,866 YES    NO            464,507 12-08-2014
PERS_TYPE                 NUMBER(2,0)                         4       0       4          0 YES    NO              5,935 12-08-2014
POLICY_ITEM_CODE          VARCHAR2(50) NOT NULL               9       0       9          0 YES    NO              5,935 12-08-2014
POLICY_ITEM_NAME          VARCHAR2(50)                        9       0       9          0 YES    NO              5,935 12-08-2014
INTENSIVE_DISABILITY_FLAG NUMBER(1,0)                         2       1       1          0 YES    NO              5,935 12-08-2014
VETERAN_BENEFIT_FLAG      NUMBER(1,0)                         1       1       1          0 YES    NO              5,935 12-08-2014
STIPEND_FLAG              NUMBER(1,0)                         1       1       1          0 YES    NO              5,935 12-08-2014
LOANS_FLAG                NUMBER(1,0)                         2       1       1          0 YES    NO              5,935 12-08-2014
REGISTERED_NUMBER         VARCHAR2(20)                        0       0       0 ########## YES    NO                    12-08-2014
PAY_INFO_NO               NUMBER(12,0)                6,086,462       0       1          0 YES    NO          5,747,813 12-08-2014
MONEY_NO                  NUMBER(12,0)               17,398,621       0       1          0 YES    NO          5,747,813 12-08-2014
INDIPAYSER                NUMBER(12,0)               22,991,252       0       1          0 YES    NO            580,870 12-08-2014
CALC_PRD                  VARCHAR2(6)                        28       0      28          0 YES    NO              5,935 12-08-2014
SRC_TYPE                  NUMBER(2,0)                         4       0       4          0 YES    NO              5,935 12-08-2014
MONEY_ID                  NUMBER(3,0)                         8       0       8          0 YES    NO              5,935 12-08-2014
PAY_MONEY                 NUMBER(12,2)                       16       0      16          0 YES    NO              5,935 12-08-2014
DO_FLAG                   NUMBER(1,0)                         1       0       1          0 YES    NO              5,935 12-08-2014
CENTER_ID                 VARCHAR2(10)                       10       0      10          0 YES    NO              5,935 12-08-2014
LOWFLAG                   NUMBER(1,0)                         2       1       1          0 YES    NO              5,935 12-08-2014
NOTHING_FLAG              NUMBER(1,0)                         2       1       1          0 YES    NO              5,935 12-08-2014
FAMILY_ID                 NUMBER(12,0)                  660,682       0       1    714,868 YES    NO            562,833 12-08-2014
URBAN_TYPE                NUMBER(2,0)                         4       0       4          0 YES    NO              5,935 12-08-2014
URBAN_TYPE_NAME           VARCHAR2(50)                        4       0       1          0 YES    NO              5,935 12-08-2014
BUSI_ASG_NO               NUMBER(12,0)                    2,223       0     254  3,836,517 YES    NO              4,967 12-08-2014
FAC_PAY_DATE              DATE                              907       0     254  3,836,517 YES    NO              4,967 12-08-2014
CURR_YEAR                 VARCHAR2(4) NOT NULL                8       0       8          0 YES    NO              5,935 12-08-2014
MOD_TIMESTAMP             TIMESTAMP(6)(11)                    0       0       0 ########## YES    NO                    12-08-2014
IS_PRINT                  NUMBER(1,0)                         1       1       1          0 YES    NO              5,935 12-08-2014
CURR_YEAR_BEG_PRD         VARCHAR2(6)                         8       0       1          0 YES    NO              5,935 12-08-2014
CURR_YEAR_END_PRD         VARCHAR2(6)                         8       0       1          0 YES    NO              5,935 12-08-2014

                              B                                        Average     Average
Index                      Tree Leaf       Distinct         Number Leaf Blocks Data Blocks      Cluster Global User           Sample Date
Name            Unique    Level Blks           Keys        of Rows     Per Key     Per Key       Factor Stats  Stats            Size MM-DD-YYYY
--------------- --------- ----- ---- -------------- -------------- ----------- ----------- ------------ ------ ------ -------------- ----------
PK_LV_URBAN_TOP UNIQUE        2 ####     21,623,824     21,623,824           1           1    2,737,193 YES    NO            137,855 12-08-2014
AY_TMP

DX_LV_URBAN_TOP NONUNIQUE     2 ####      6,086,462     22,613,945           1           1   10,946,874 YES    NO            429,108 12-08-2014
AY_TMP_PAY

IDX_LV_URBAN_TO NONUNIQUE     2 ####          2,223     18,979,800          17         708    1,574,170 YES    NO            530,075 12-08-2014
PAY_TMP_BUS

IDX_LV_URBAN_TO NONUNIQUE     3 ####            223     22,474,402         393      25,642    5,718,207 YES    NO            284,917 12-08-2014
PAY_TMP_CENTER

IDX_LV_URBAN_TO NONUNIQUE     2 ####        660,682     21,796,771           1          17   11,256,787 YES    NO            407,563 12-08-2014
PAY_TMP_FAM

IDX_LV_URBAN_TO NONUNIQUE     2 ####      1,934,475     21,768,753           1           7   15,125,646 YES    NO            381,994 12-08-2014
PAY_TMP_INDI

IDX_LV_URBAN_TO NONUNIQUE     2 ####     17,398,621     21,880,953           1           1   11,499,589 YES    NO            423,560 12-08-2014
PAY_TMP_NO

IDX_LV_URBAN_TO NONUNIQUE     2 ####     22,485,115     22,485,115           1           1    2,820,116 YES    NO            121,303 12-08-2014
PAY_TMP_SOME

IDX_LV_URBAN_TO NONUNIQUE     3 ####            996     22,727,163          80       8,493    8,459,953 YES    NO            311,063 12-08-2014
PAY_TMP_CORP_ID

IDX_LV_URBAN_TO NONUNIQUE     2 ####              8     23,228,508       7,131     #######      828,346 YES    NO            465,750 12-08-2014
PAY_TMP_YEAR


Index           Column                     Col Column
Name            Name                       Pos Details
--------------- ------------------------- ---- ------------------------
DX_LV_URBAN_TOP PAY_INFO_NO                  1 NUMBER(12,0)
AY_TMP_PAY

                PAY_INFO_NO                  1 NUMBER(12,0)
IDX_LV_URBAN_TO BUSI_ASG_NO                  1 NUMBER(12,0)
PAY_TMP_BUS

                BUSI_ASG_NO                  1 NUMBER(12,0)
IDX_LV_URBAN_TO CENTER_ID                    1 VARCHAR2(10)
PAY_TMP_CENTER

                CENTER_ID                    1 VARCHAR2(10)
                CURR_YEAR                    2 VARCHAR2(4) NOT NULL
                CURR_YEAR                    2 VARCHAR2(4) NOT NULL
                PERS_TYPE                    3 NUMBER(2,0)
                PERS_TYPE                    3 NUMBER(2,0)
IDX_LV_URBAN_TO CORP_ID                      1 NUMBER(12,0) NOT NULL
PAY_TMP_CORP_ID

                CORP_ID                      1 NUMBER(12,0) NOT NULL
                CURR_YEAR                    2 VARCHAR2(4) NOT NULL
                CURR_YEAR                    2 VARCHAR2(4) NOT NULL
                PERS_TYPE                    3 NUMBER(2,0)
                PERS_TYPE                    3 NUMBER(2,0)
IDX_LV_URBAN_TO FAMILY_ID                    1 NUMBER(12,0)
PAY_TMP_FAM

                FAMILY_ID                    1 NUMBER(12,0)
IDX_LV_URBAN_TO INDI_ID                      1 NUMBER(12,0) NOT NULL
PAY_TMP_INDI

                INDI_ID                      1 NUMBER(12,0) NOT NULL
IDX_LV_URBAN_TO MONEY_NO                     1 NUMBER(12,0)
PAY_TMP_NO

                MONEY_NO                     1 NUMBER(12,0)
IDX_LV_URBAN_TO URBAN_TOPAY_SN               1 NUMBER(12,0) NOT NULL
PAY_TMP_SOME

                URBAN_TOPAY_SN               1 NUMBER(12,0) NOT NULL
                INDI_ID                      2 NUMBER(12,0) NOT NULL
                INDI_ID                      2 NUMBER(12,0) NOT NULL
                POLICY_ITEM_CODE             3 VARCHAR2(50) NOT NULL
                POLICY_ITEM_CODE             3 VARCHAR2(50) NOT NULL
                CORP_ID                      4 NUMBER(12,0) NOT NULL
                CORP_ID                      4 NUMBER(12,0) NOT NULL
                BUSI_ASG_NO                  5 NUMBER(12,0)
                BUSI_ASG_NO                  5 NUMBER(12,0)
                CURR_YEAR                    6 VARCHAR2(4) NOT NULL
                CURR_YEAR                    6 VARCHAR2(4) NOT NULL
IDX_LV_URBAN_TO CURR_YEAR                    1 VARCHAR2(4) NOT NULL
PAY_TMP_YEAR

                CURR_YEAR                    1 VARCHAR2(4) NOT NULL

PK_LV_URBAN_TOP URBAN_TOPAY_SN               1 NUMBER(12,0) NOT NULLAY_TMP
                INDI_ID                      2 NUMBER(12,0) NOT NULL
                POLICY_ITEM_CODE             3 VARCHAR2(50) NOT NULL
                CORP_ID                      4 NUMBER(12,0) NOT NULL
                CURR_YEAR                    5 VARCHAR2(4) NOT NULL


从上面的显示结果可以看到执行计划的第一步执行的是对索引IDX_LV_URBAN_TOPAY_TMP_CORP_ID执行索引范围扫描并没有使用Hint所指定的PK_LV_URBAN_TOPAY_TMP,而且从上面的显示的索引信息部分可以看到索引PK_LV_URBAN_TOPAY_TMP是由列URBAN_TOPAY_SN, INDI_ID, POLICY_ITEM_CODE, CORP_ID, CURR_YEAR组成的复合索引而查询条件没有URBAN_TOPAY_SN字段使用不了这个索引从上面的执行计划可以看到这一结果。上面执行计划中步骤28与步骤29执行哈希连接,都是全表扫描,其中步骤Id=29是对BS_INSURED执行100多万条记录执行全表扫并且将它们的结果与表BS_PERSON_TYPE执行哈希连接最后与BS_FAMILY执行哈希连接返回记录123条数据,其成本是14375,其中对表BS_INSURED执行全表扫描的成本就是14244。而执行计划执行的第一步就是访问LV_URBAN_TOPAY_TMP,而where条件中有 bi.indi_id = lt.indi_id AND bs.sex = bi.sex and pt.pers_type = bi.pers_type,且这条连接条件都存在索引,那么就不应该那对BS_INSURED,BS_SEX,BS_PERSON_TYPE,BS_FAMILY 这四个表之间进行哈希连接后再与这四个表(LV_URBAN_TOPAY_TMP,LV_BUSI_ASSIGN,LV_BUSI_RECORD,LV_BUSI_BILL)连接后的结果集之间执行哈希连接。而是应该
在这四个表(LV_URBAN_TOPAY_TMP,LV_BUSI_ASSIGN,LV_BUSI_RECORD,LV_BUSI_BILL)连接后与BS_INSURED执行嵌套循环连接,因为有where条件bi.indi_id = lt.indi_id 而且indi_id在表BS_INSURED中是主键,从上面的索引信息中可以看到IDX_LV_URBAN_TOPAY_TMP_CORP_ID索引的distinct key是996,而where条件能使用CBO选择使用该索引从执行计划的Order列为1的步骤可知首先执行的就是对索引IDX_LV_URBAN_TOPAY_TMP_CORP_ID的索引范围扫描。当LV_URBAN_TOPAY_TMP与BS_INSURED执行完嵌套循环连接后因为有where条件bs.sex = bi.sex and pt.pers_type = bi.pers_type所以对BS_SEX,BS_PERSON_TYPE,BS_FAMILY表都应该执行嵌套循环连接。

这里优化步骤是首先删除原来SQL语句中所使用的Hint”/*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */”后并执行SQL语句:

SQL>SELECT 
 bi.indi_id,
 bi.name,
 pt.pers_name,
 bs.sex_name,
 lt.pay_money,
 bi.idcard,
 bi.birthday,
 bf.headed_name,
 lt.fac_pay_date,
 lbb.audit_man,
 tab_hosp.hospital_name as hospital_name,
 to_char(lbb.make_bill_tm, 'yyyy-mm-dd') as make_bill_tm,
 bf.telephone,
 nvl((decode(lt.intensive_disability_flag,
             1,
             decode(lt.lowflag, 1, '重症伤残,', '重症伤残'),
             '') ||
     decode(lt.lowflag, 1, decode(lt.nothing_flag, 1, '低保,', '低保'), '') ||
     decode(lt.nothing_flag, 1, '三无', '')),
     '标准') as subsidykide
  FROM lv_urban_topay_tmp lt,
       bs_insured bi,
       bs_sex bs,
       bs_person_type pt,
       bs_pres_insur bpi,
       bs_family bf,
       lv_busi_bill lbb,
       lv_busi_record lbr,
       lv_busi_assign lba,
       (select bh.hospital_name, bph.indi_id
          from bs_pers_hosp bph, bs_hospital bh
         where bph.hospital_id = bh.hospital_id
           and bph.first_flag = 1
           and bph.end_year = '2015') tab_hosp
 WHERE nvl(lt.busi_asg_no, 0) <> 0
   AND nvl(lt.busi_asg_no, 0) not in (-999, -998, -997, -981, -980)
   AND lt.fac_pay_date is not null
   AND bi.indi_id = lt.indi_id
   AND bs.sex = bi.sex
   AND bi.indi_id = tab_hosp.indi_id(+)
   AND lbr.busi_reco_no = lba.busi_reco_no
   AND lbr.busi_bill_sn = lbb.busi_bill_sn
   AND lt.center_id = lbb.center_id
   AND lt.busi_asg_no = lba.busi_asg_no
   AND lt.indi_id = bi.indi_id
   AND pt.pers_type = bi.pers_type
   AND bpi.indi_id = bi.indi_id
   AND lt.center_id = pt.center_id
   AND bf.family_id = bi.family_id
   AND bf.family_sta = 1
   AND bi.indi_sta = 1
   AND bpi.indi_join_sta = 1
   AND bf.center_id = lt.center_id
   AND bf.corp_id = lt.corp_id
   AND lt.policy_item_code like '%INDI_TOPAY'
   AND lt.corp_id = '19159'
   AND bpi.insr_detail_code = 21
   AND lt.center_id = '430726'
   AND lt.curr_year = '2015'
   AND lt.fac_pay_date >= to_date('2014-12-01 00:00:00',
                                  'yyyy-MM-dd hh24:mi:ss')
   AND lt.fac_pay_date < =
       to_date('2015-01-05 23:59:59', 'yyyy-MM-dd hh24:mi:ss')
   and exists (select 'X'
          FROM lv_busi_bill       lbb,
               lv_busi_record     lbr,
               lv_busi_assign     lba,
               lv_urban_topay_tmp lutt
         WHERE lbr.busi_reco_no = lba.busi_reco_no
           AND lbr.busi_bill_sn = lbb.busi_bill_sn
           AND lbb.center_id = '430726'
           AND lutt.corp_id = '19159'
           AND lutt.center_id = lbb.center_id
           AND lutt.busi_asg_no = lba.busi_asg_no
           and lba.busi_asg_no = lt.busi_asg_no
           and lutt.indi_id = bi.indi_id)
 order by lt.fac_pay_date, bi.indi_id, bi.name
;
....省略输出结果
2304 rows selected.

Elapsed: 00:00:08.83

一共返回了2304条记录,使用时间是8.83秒,其执行计划如下,现在消除了对BS_INSURED的全表扫描,选择最优的执行计划

--------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |                                |     1 |   278 |    88   (2)| 00:00:02 |
|   1 |  SORT ORDER BY                          |                                |     1 |   278 |    88   (2)| 00:00:02 |
|   2 |   NESTED LOOPS                          |                                |     1 |   278 |    82   (0)| 00:00:01 |
|   3 |    NESTED LOOPS OUTER                   |                                |     1 |   272 |    81   (0)| 00:00:01 |
|   4 |     NESTED LOOPS                        |                                |     1 |   231 |    79   (0)| 00:00:01 |
|   5 |      NESTED LOOPS                       |                                |     1 |   219 |    78   (0)| 00:00:01 |
|   6 |       NESTED LOOPS                      |                                |     1 |   201 |    77   (0)| 00:00:01 |
|   7 |        NESTED LOOPS                     |                                |     1 |   169 |    76   (0)| 00:00:01 |
|   8 |         NESTED LOOPS                    |                                |     1 |   113 |    75   (0)| 00:00:01 |
|   9 |          NESTED LOOPS                   |                                |     1 |    88 |    74   (0)| 00:00:01 |
|  10 |           NESTED LOOPS                  |                                |     1 |    77 |    73   (0)| 00:00:01 |
|* 11 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |     1 |    65 |    72   (0)| 00:00:01 |
|* 12 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_CORP_ID |   954 |       |     1   (0)| 00:00:01 |
|  13 |            TABLE ACCESS BY INDEX ROWID  | LV_BUSI_ASSIGN                 |     1 |    12 |     1   (0)| 00:00:01 |
|* 14 |             INDEX UNIQUE SCAN           | PK_LV_BUSI_ASSIGN              |     1 |       |     1   (0)| 00:00:01 |
|  15 |           TABLE ACCESS BY INDEX ROWID   | LV_BUSI_RECORD                 |     1 |    11 |     1   (0)| 00:00:01 |
|* 16 |            INDEX UNIQUE SCAN            | PK_LV_BUSI_RECORD              |     1 |       |     1   (0)| 00:00:01 |
|* 17 |          TABLE ACCESS BY INDEX ROWID    | LV_BUSI_BILL                   |     1 |    25 |     1   (0)| 00:00:01 |
|* 18 |           INDEX UNIQUE SCAN             | PK_LV_BUSI_BILL                |     1 |       |     1   (0)| 00:00:01 |
|* 19 |         TABLE ACCESS BY INDEX ROWID     | BS_INSURED                     |     1 |    56 |     1   (0)| 00:00:01 |
|* 20 |          INDEX UNIQUE SCAN              | PK_BS_INSURED                  |     1 |       |     1   (0)| 00:00:01 |
|  21 |           NESTED LOOPS                  |                                |     1 |    56 |     5   (0)| 00:00:01 |
|  22 |            NESTED LOOPS                 |                                |     1 |    35 |     3   (0)| 00:00:01 |
|  23 |             NESTED LOOPS                |                                |     1 |    23 |     2   (0)| 00:00:01 |
|  24 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_ASSIGN                 |     1 |    12 |     1   (0)| 00:00:01 |
|* 25 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_ASSIGN              |     1 |       |     1   (0)| 00:00:01 |
|  26 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_RECORD                 |  1679K|    17M|     1   (0)| 00:00:01 |
|* 27 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_RECORD              |     1 |       |     1   (0)| 00:00:01 |
|* 28 |             TABLE ACCESS BY INDEX ROWID | LV_BUSI_BILL                   | 83405 |   977K|     1   (0)| 00:00:01 |
|* 29 |              INDEX UNIQUE SCAN          | PK_LV_BUSI_BILL                |     1 |       |     1   (0)| 00:00:01 |
|* 30 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |     1 |    21 |     2   (0)| 00:00:01 |
|* 31 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_INDI    |    12 |       |     1   (0)| 00:00:01 |
|* 32 |        TABLE ACCESS BY INDEX ROWID      | BS_FAMILY                      |     1 |    32 |     1   (0)| 00:00:01 |
|* 33 |         INDEX UNIQUE SCAN               | PK_BS_FAMILY                   |     1 |       |     1   (0)| 00:00:01 |
|  34 |       TABLE ACCESS BY INDEX ROWID       | BS_PERSON_TYPE                 |     1 |    18 |     1   (0)| 00:00:01 |
|* 35 |        INDEX UNIQUE SCAN                | PK_BS_PERSON_TYPE              |     1 |       |     1   (0)| 00:00:01 |
|* 36 |      INDEX UNIQUE SCAN                  | INDEX_BS_PRES_INSUR_UNIQUE     |     1 |    12 |     1   (0)| 00:00:01 |
|  37 |     VIEW PUSHED PREDICATE               |                                |     1 |    41 |     2   (0)| 00:00:01 |
|  38 |      NESTED LOOPS                       |                                |     1 |    57 |     2   (0)| 00:00:01 |
|* 39 |       TABLE ACCESS BY INDEX ROWID       | BS_PERS_HOSP                   |     1 |    25 |     1   (0)| 00:00:01 |
|* 40 |        INDEX RANGE SCAN                 | PK_BS_PERS_HOSP                |     2 |       |     1   (0)| 00:00:01 |
|  41 |       TABLE ACCESS BY INDEX ROWID       | BS_HOSPITAL                    |     1 |    32 |     1   (0)| 00:00:01 |
|* 42 |        INDEX UNIQUE SCAN                | PK_BS_HOSPITAL                 |     1 |       |     1   (0)| 00:00:01 |
|  43 |    TABLE ACCESS BY INDEX ROWID          | BS_SEX                         |     1 |     6 |     1   (0)| 00:00:01 |
|* 44 |     INDEX UNIQUE SCAN                   | PK_BS_SEX                      |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------------------------

从上面的执行计划可以看到,当去掉Hint”/*+ index(lt, PK_LV_URBAN_TOPAY_TMP) */”后,CBO选择的执行计划果然是先访问IDX_LV_URBAN_TOPAY_TMP_CORP_ID
来访问表LV_URBAN_TOPAY_TMP,然后因为“lbr.busi_reco_no = lba.busi_reco_no AND lbr.busi_bill_sn = lbb.busi_bill_sn AND lt.center_id = lbb.center_id
AND lt.busi_asg_no = lba.busi_asg_no ”,表LV_BUSI_ASSIGN,LV_BUSI_RECORD,LV_BUSI_BILL与LV_URBAN_TOPAY_TMP连接的列都是各个表的主键,所以会分别与这三个执行嵌套循环连接。且存在where条件bi.indi_id = lt.indi_id ,而indi_id是表BS_INSURED表的主键所以也执行嵌套循环连接。而又因为与表BS_INSURED进行表连接的列也存在相关索引因此,依次类推整个执行计划都是执行嵌套循环连接。而且从下面的执行计划信息中可以看到,整个语句的逻辑读只有96762,而原来的逻辑读为3亿多次。这可是数据量级的减少。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Order | Operation                               | Name                           | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   1 |    44 |  SORT ORDER BY                          |                                |      1 |      1 |   2304 |00:00:00.39 |   96762 |   407K|   407K|  361K (0)|
|   2 |    43 |   NESTED LOOPS                          |                                |      1 |      1 |   2304 |00:00:00.39 |   96762 |       |       |          |
|   3 |    40 |    NESTED LOOPS OUTER                   |                                |      1 |      1 |   2304 |00:00:00.37 |   94456 |       |       |          |
|   4 |    33 |     NESTED LOOPS                        |                                |      1 |      1 |   2304 |00:00:00.30 |   80900 |       |       |          |
|   5 |    31 |      NESTED LOOPS                       |                                |      1 |      1 |   2304 |00:00:00.29 |   76290 |       |       |          |
|   6 |    28 |       NESTED LOOPS                      |                                |      1 |      1 |   2304 |00:00:00.27 |   73984 |       |       |          |
|   7 |    25 |        NESTED LOOPS                     |                                |      1 |      1 |   2304 |00:00:00.25 |   67070 |       |       |          |
|   8 |    11 |         NESTED LOOPS                    |                                |      1 |      1 |   2304 |00:00:00.09 |   21695 |       |       |          |
|   9 |     8 |          NESTED LOOPS                   |                                |      1 |      1 |   2304 |00:00:00.07 |   14781 |       |       |          |
|  10 |     5 |           NESTED LOOPS                  |                                |      1 |      1 |   2304 |00:00:00.05 |    7867 |       |       |          |
|* 11 |     2 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |      1 |      1 |   2304 |00:00:00.02 |     953 |       |       |          |
|* 12 |     1 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_CORP_ID |      1 |    954 |   6986 |00:00:00.01 |      33 |       |       |          |
|  13 |     4 |            TABLE ACCESS BY INDEX ROWID  | LV_BUSI_ASSIGN                 |   2304 |      1 |   2304 |00:00:00.02 |    6914 |       |       |          |
|* 14 |     3 |             INDEX UNIQUE SCAN           | PK_LV_BUSI_ASSIGN              |   2304 |      1 |   2304 |00:00:00.01 |    4610 |       |       |          |
|  15 |     7 |           TABLE ACCESS BY INDEX ROWID   | LV_BUSI_RECORD                 |   2304 |      1 |   2304 |00:00:00.02 |    6914 |       |       |          |
|* 16 |     6 |            INDEX UNIQUE SCAN            | PK_LV_BUSI_RECORD              |   2304 |      1 |   2304 |00:00:00.01 |    4610 |       |       |          |
|* 17 |    10 |          TABLE ACCESS BY INDEX ROWID    | LV_BUSI_BILL                   |   2304 |      1 |   2304 |00:00:00.02 |    6914 |       |       |          |
|* 18 |     9 |           INDEX UNIQUE SCAN             | PK_LV_BUSI_BILL                |   2304 |      1 |   2304 |00:00:00.01 |    4610 |       |       |          |
|* 19 |    24 |         TABLE ACCESS BY INDEX ROWID     | BS_INSURED                     |   2304 |      1 |   2304 |00:00:00.15 |   45375 |       |       |          |
|* 20 |    23 |          INDEX UNIQUE SCAN              | PK_BS_INSURED                  |   2304 |      1 |   2304 |00:00:00.13 |   43071 |       |       |          |
|  21 |    22 |           NESTED LOOPS                  |                                |   2304 |      1 |   2304 |00:00:00.11 |   38461 |       |       |          |
|  22 |    19 |            NESTED LOOPS                 |                                |   2304 |      1 |   2304 |00:00:00.06 |   27648 |       |       |          |
|  23 |    16 |             NESTED LOOPS                |                                |   2304 |      1 |   2304 |00:00:00.04 |   18432 |       |       |          |
|  24 |    13 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_ASSIGN                 |   2304 |      1 |   2304 |00:00:00.02 |    9216 |       |       |          |
|* 25 |    12 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_ASSIGN              |   2304 |      1 |   2304 |00:00:00.01 |    6912 |       |       |          |
|  26 |    15 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_RECORD                 |   2304 |   1679K|   2304 |00:00:00.02 |    9216 |       |       |          |
|* 27 |    14 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_RECORD              |   2304 |      1 |   2304 |00:00:00.01 |    6912 |       |       |          |
|* 28 |    18 |             TABLE ACCESS BY INDEX ROWID | LV_BUSI_BILL                   |   2304 |  83405 |   2304 |00:00:00.02 |    9216 |       |       |          |
|* 29 |    17 |              INDEX UNIQUE SCAN          | PK_LV_BUSI_BILL                |   2304 |      1 |   2304 |00:00:00.01 |    6912 |       |       |          |
|* 30 |    21 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |   2304 |      1 |   2304 |00:00:00.04 |   10813 |       |       |          |
|* 31 |    20 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_INDI    |   2304 |     12 |   7564 |00:00:00.02 |    6920 |       |       |          |
|* 32 |    27 |        TABLE ACCESS BY INDEX ROWID      | BS_FAMILY                      |   2304 |      1 |   2304 |00:00:00.02 |    6914 |       |       |          |
|* 33 |    26 |         INDEX UNIQUE SCAN               | PK_BS_FAMILY                   |   2304 |      1 |   2304 |00:00:00.01 |    4610 |       |       |          |
|  34 |    30 |       TABLE ACCESS BY INDEX ROWID       | BS_PERSON_TYPE                 |   2304 |      1 |   2304 |00:00:00.01 |    2306 |       |       |          |
|* 35 |    29 |        INDEX UNIQUE SCAN                | PK_BS_PERSON_TYPE              |   2304 |      1 |   2304 |00:00:00.01 |       2 |       |       |          |
|* 36 |    32 |      INDEX UNIQUE SCAN                  | INDEX_BS_PRES_INSUR_UNIQUE     |   2304 |      1 |   2304 |00:00:00.01 |    4610 |       |       |          |
|  37 |    39 |     VIEW PUSHED PREDICATE               |                                |   2304 |      1 |   2297 |00:00:00.06 |   13556 |       |       |          |
|  38 |    38 |      NESTED LOOPS                       |                                |   2304 |      1 |   2297 |00:00:00.05 |   13556 |       |       |          |
|* 39 |    35 |       TABLE ACCESS BY INDEX ROWID       | BS_PERS_HOSP                   |   2304 |      1 |   2297 |00:00:00.04 |    8960 |       |       |          |
|* 40 |    34 |        INDEX RANGE SCAN                 | PK_BS_PERS_HOSP                |   2304 |      2 |   4327 |00:00:00.02 |    4633 |       |       |          |
|  41 |    37 |       TABLE ACCESS BY INDEX ROWID       | BS_HOSPITAL                    |   2297 |      1 |   2297 |00:00:00.01 |    4596 |       |       |          |
|* 42 |    36 |        INDEX UNIQUE SCAN                | PK_BS_HOSPITAL                 |   2297 |      1 |   2297 |00:00:00.01 |    2299 |       |       |          |
|  43 |    42 |    TABLE ACCESS BY INDEX ROWID          | BS_SEX                         |   2304 |      1 |   2304 |00:00:00.01 |    2306 |       |       |          |
|* 44 |    41 |     INDEX UNIQUE SCAN                   | PK_BS_SEX                      |   2304 |      1 |   2304 |00:00:00.01 |       2 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
-----------------------------------------------------------

  11 - filter(("LT"."FAC_PAY_DATE">=TO_DATE(' 2014-12-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "LT"."CENTER_ID"='430726' AND
              "LT"."POLICY_ITEM_CODE" LIKE '%INDI_TOPAY' AND "LT"."FAC_PAY_DATE" IS NOT NULL AND "LT"."BUSI_ASG_NO" IS NOT NULL AND NVL("LT"."BUSI_ASG_NO",0)<>0 AND
              NVL("LT"."BUSI_ASG_NO",0)<>(-980) AND NVL("LT"."BUSI_ASG_NO",0)<>(-981) AND NVL("LT"."BUSI_ASG_NO",0)<>(-997) AND NVL("LT"."BUSI_ASG_NO",0)<>(-998)
              AND NVL("LT"."BUSI_ASG_NO",0)<>(-999) AND "LT"."FAC_PAY_DATE"< =TO_DATE(' 2015-01-05 23:59:59', 'syyyy-mm-dd hh24:mi:ss')))
  12 - access("LT"."CORP_ID"=19159 AND "LT"."CURR_YEAR"='2015')
  14 - access("LT"."BUSI_ASG_NO"="LBA"."BUSI_ASG_NO")
  16 - access("LBR"."BUSI_RECO_NO"="LBA"."BUSI_RECO_NO")
  17 - filter("LBB"."CENTER_ID"='430726')
  18 - access("LBR"."BUSI_BILL_SN"="LBB"."BUSI_BILL_SN")
  19 - filter(("BI"."FAMILY_ID" IS NOT NULL AND "BI"."INDI_STA"=1))
  20 - access("BI"."INDI_ID"="LT"."INDI_ID")
       filter( IS NOT NULL)
  25 - access("LBA"."BUSI_ASG_NO"=:B1)
  27 - access("LBR"."BUSI_RECO_NO"="LBA"."BUSI_RECO_NO")
  28 - filter("LBB"."CENTER_ID"='430726')
  29 - access("LBR"."BUSI_BILL_SN"="LBB"."BUSI_BILL_SN")
  30 - filter(("LUTT"."BUSI_ASG_NO"=:B1 AND "LUTT"."CORP_ID"=19159 AND "LUTT"."CENTER_ID"='430726'))
  31 - access("LUTT"."INDI_ID"=:B1)
  32 - filter(("BF"."CORP_ID"=19159 AND "BF"."CENTER_ID"='430726' AND "BF"."FAMILY_STA"=1))
  33 - access("BF"."FAMILY_ID"="BI"."FAMILY_ID")
  35 - access("PT"."PERS_TYPE"="BI"."PERS_TYPE" AND "PT"."CENTER_ID"='430726')
  36 - access("BPI"."INDI_ID"="BI"."INDI_ID" AND "BPI"."INSR_DETAIL_CODE"=21 AND "BPI"."INDI_JOIN_STA"=1)
  39 - filter("BPH"."END_YEAR"='2015')
  40 - access("BPH"."INDI_ID"="BI"."INDI_ID" AND "BPH"."FIRST_FLAG"=1)
       filter("BPH"."FIRST_FLAG"=1)
  42 - access("BPH"."HOSPITAL_ID"="BH"."HOSPITAL_ID")
  44 - access("BS"."SEX"=TO_NUMBER("BI"."SEX"))

现在由于没有办法修改代码,所以选择使用SQL Profile来固定该SQL的执行计划。生成SQL Profile有两种方法,这里使用手工生成SQL Profile。使用coe_xfr_sql_profile.sql脚本先对原SQL语句生成SQL Profile文件。

SQL> @/oracle/sqlt/utl/coe_xfr_sql_profile.sql

Parameter 1:
SQL_ID (required)

Enter value for 1: 36cbabzyq13gy


PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     1849931106    6280.149

Parameter 2:
PLAN_HASH_VALUE (required)

Enter value for 2: 1849931106

Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "36cbabzyq13gy"
PLAN_HASH_VALUE: "1849931106"

SQL>BEGIN
  2    IF :sql_text IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20100, 'SQL_TEXT for SQL_ID &&sql_id. was not found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;
SQL>BEGIN
  2    IF :other_xml IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20101, 'PLAN for SQL_ID &&sql_id. and PHV &&plan_hash_value. was not found in memory (gv$sql_plan) or AWR (dba_hist_sql_plan).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;

Execute coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql
on TARGET system in order to create a custom SQL Profile
with plan 1849931106 linked to adjusted sql_text.


对去掉Hint后的SQL生成SQL Profile

SQL>@E:\scripts\ch\coe_xfr_sql_profile.sql

Parameter 1:
SQL_ID (required)

输入 1 的值:  46fc6316z15mh


PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     3748535674        1.24

Parameter 2:
PLAN_HASH_VALUE (required)

输入 2 的值:  3748535674

Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "46fc6316z15mh"
PLAN_HASH_VALUE: "3748535674"

SQL>BEGIN
  2    IF :sql_text IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20100, 'SQL_TEXT for SQL_ID &&sql_id. was not
found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;
SQL>BEGIN
  2    IF :other_xml IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20101, 'PLAN for SQL_ID &&sql_id. and PHV &&pl
an_hash_value. was not found in memory (gv$sql_plan) or AWR (dba_hist_sql_plan).
');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;

Execute coe_xfr_sql_profile_46fc6316z15mh_3748535674.sql
on TARGET system in order to create a custom SQL Profile
with plan 3748535674 linked to adjusted sql_text.

原SQL的SQL Profile文件coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql中有如下记录:

h := SYS.SQLPROF_ATTR(
q'[BEGIN_OUTLINE_DATA]',
q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.4')]',
q'[OPT_PARAM('optimizer_index_cost_adj' 20)]',
q'[OPT_PARAM('optimizer_index_caching' 90)]',
q'[ALL_ROWS]',
q'[OUTLINE_LEAF(@"SEL$3")]',
q'[OUTLINE_LEAF(@"SEL$639F1A6F")]',
q'[PUSH_PRED(@"SEL$1" "TAB_HOSP"@"SEL$1" 12)]',
q'[OUTLINE_LEAF(@"SEL$1")]',
q'[OUTLINE(@"SEL$3")]',
q'[OUTLINE(@"SEL$2")]',
q'[OUTLINE(@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$1" "LT"@"SEL$1" ("LV_URBAN_TOPAY_TMP"."CORP_ID" "LV_URBAN_TOPAY_TMP"."CURR_YEAR" "LV_URBAN_TOPAY_TMP"."PERS_TYPE"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBA"@"SEL$1" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBR"@"SEL$1" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBB"@"SEL$1" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
q'[INDEX_RS_ASC(@"SEL$1" "BI"@"SEL$1" ("BS_INSURED"."INDI_ID"))]',
q'[INDEX_RS_ASC(@"SEL$1" "BF"@"SEL$1" ("BS_FAMILY"."FAMILY_ID"))]',
q'[INDEX_RS_ASC(@"SEL$1" "PT"@"SEL$1" ("BS_PERSON_TYPE"."PERS_TYPE" "BS_PERSON_TYPE"."CENTER_ID"))]',
q'[INDEX(@"SEL$1" "BPI"@"SEL$1" ("BS_PRES_INSUR"."INDI_ID" "BS_PRES_INSUR"."INSR_DETAIL_CODE" "BS_PRES_INSUR"."INDI_JOIN_STA"))]',
q'[NO_ACCESS(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$1" "BS"@"SEL$1" ("BS_SEX"."SEX"))]',
q'[LEADING(@"SEL$1" "LT"@"SEL$1" "LBA"@"SEL$1" "LBR"@"SEL$1" "LBB"@"SEL$1" "BI"@"SEL$1" "BF"@"SEL$1" "PT"@"SEL$1" "BPI"@"SEL$1" "TAB_HOSP"@"SEL$1" "BS"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBA"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBR"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBB"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BI"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BF"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "PT"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BPI"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BS"@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BPH"@"SEL$2" ("BS_PERS_HOSP"."INDI_ID" "BS_PERS_HOSP"."HOSPITAL_ID" "BS_PERS_HOSP"."FIRST_FLAG" "BS_PERS_HOSP"."BEG_YEAR" "BS_PERS_HOSP"."BIZ_TYPE"))]',
q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BH"@"SEL$2" ("BS_HOSPITAL"."HOSPITAL_ID"))]',
q'[LEADING(@"SEL$639F1A6F" "BPH"@"SEL$2" "BH"@"SEL$2")]',
q'[USE_NL(@"SEL$639F1A6F" "BH"@"SEL$2")]',
q'[INDEX_RS_ASC(@"SEL$3" "LBA"@"SEL$3" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LBR"@"SEL$3" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LBB"@"SEL$3" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LUTT"@"SEL$3" ("LV_URBAN_TOPAY_TMP"."INDI_ID"))]',
q'[LEADING(@"SEL$3" "LBA"@"SEL$3" "LBR"@"SEL$3" "LBB"@"SEL$3" "LUTT"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LBR"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LBB"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LUTT"@"SEL$3")]',
q'[END_OUTLINE_DATA]');

用去掉Hint后SQL语句所对应的SQL Profile文件coe_xfr_sql_profile_46fc6316z15mh_3748535674.sql中的如下记录替换coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql文件中上述内容:

h := SYS.SQLPROF_ATTR(
q'[BEGIN_OUTLINE_DATA]',
q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.4')]',
q'[OPT_PARAM('optimizer_index_cost_adj' 20)]',
q'[OPT_PARAM('optimizer_index_caching' 90)]',
q'[ALL_ROWS]',
q'[OUTLINE_LEAF(@"SEL$3")]',
q'[OUTLINE_LEAF(@"SEL$639F1A6F")]',
q'[PUSH_PRED(@"SEL$1" "TAB_HOSP"@"SEL$1" 12)]',
q'[OUTLINE_LEAF(@"SEL$1")]',
q'[OUTLINE(@"SEL$3")]',
q'[OUTLINE(@"SEL$2")]',
q'[OUTLINE(@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$1" "LT"@"SEL$1" ("LV_URBAN_TOPAY_TMP"."CORP_ID" "LV_URBAN_TOPAY_TMP"."CURR_YEAR" "LV_URBAN_TOPAY_TMP"."PERS_TYPE"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBA"@"SEL$1" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBR"@"SEL$1" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]',
q'[INDEX_RS_ASC(@"SEL$1" "LBB"@"SEL$1" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
q'[INDEX_RS_ASC(@"SEL$1" "BI"@"SEL$1" ("BS_INSURED"."INDI_ID"))]',
q'[INDEX_RS_ASC(@"SEL$1" "BF"@"SEL$1" ("BS_FAMILY"."FAMILY_ID"))]',
q'[INDEX_RS_ASC(@"SEL$1" "PT"@"SEL$1" ("BS_PERSON_TYPE"."PERS_TYPE" "BS_PERSON_TYPE"."CENTER_ID"))]',
q'[INDEX(@"SEL$1" "BPI"@"SEL$1" ("BS_PRES_INSUR"."INDI_ID" "BS_PRES_INSUR"."INSR_DETAIL_CODE" "BS_PRES_INSUR"."INDI_JOIN_STA"))]',
q'[NO_ACCESS(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$1" "BS"@"SEL$1" ("BS_SEX"."SEX"))]',
q'[LEADING(@"SEL$1" "LT"@"SEL$1" "LBA"@"SEL$1" "LBR"@"SEL$1" "LBB"@"SEL$1" "BI"@"SEL$1" "BF"@"SEL$1" "PT"@"SEL$1" "BPI"@"SEL$1" "TAB_HOSP"@"SEL$1" "BS"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBA"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBR"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "LBB"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BI"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BF"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "PT"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BPI"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
q'[USE_NL(@"SEL$1" "BS"@"SEL$1")]',
q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BPH"@"SEL$2" ("BS_PERS_HOSP"."INDI_ID" "BS_PERS_HOSP"."HOSPITAL_ID" "BS_PERS_HOSP"."FIRST_FLAG" "BS_PERS_HOSP"."BEG_YEAR" "BS_PERS_HOSP"."BIZ_TYPE"))]',
q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BH"@"SEL$2" ("BS_HOSPITAL"."HOSPITAL_ID"))]',
q'[LEADING(@"SEL$639F1A6F" "BPH"@"SEL$2" "BH"@"SEL$2")]',
q'[USE_NL(@"SEL$639F1A6F" "BH"@"SEL$2")]',
q'[INDEX_RS_ASC(@"SEL$3" "LBA"@"SEL$3" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LBR"@"SEL$3" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LBB"@"SEL$3" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
q'[INDEX_RS_ASC(@"SEL$3" "LUTT"@"SEL$3" ("LV_URBAN_TOPAY_TMP"."INDI_ID"))]',
q'[LEADING(@"SEL$3" "LBA"@"SEL$3" "LBR"@"SEL$3" "LBB"@"SEL$3" "LUTT"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LBR"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LBB"@"SEL$3")]',
q'[USE_NL(@"SEL$3" "LUTT"@"SEL$3")]',
q'[END_OUTLINE_DATA]');

替换后保存coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql文件,并执行该文件:

SQL>@C:\Users\Administrator\coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql
SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql 11.4.
3.5 2015/01/05 carlos.sierra $
SQL>REM
SQL>REM Copyright (c) 2000-2011, Oracle Corporation. All rights reserved.
SQL>REM
SQL>REM AUTHOR
SQL>REM   carlos.sierra@oracle.com
SQL>REM
SQL>REM SCRIPT
SQL>REM   coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql
SQL>REM
SQL>REM DESCRIPTION
SQL>REM   This script is generated by coe_xfr_sql_profile.sql
SQL>REM   It contains the SQL*Plus commands to create a custom
SQL>REM   SQL Profile for SQL_ID 36cbabzyq13gy based on plan hash
SQL>REM   value 1849931106.
SQL>REM   The custom SQL Profile to be created by this script
SQL>REM   will affect plans for SQL commands with signature
SQL>REM   matching the one for SQL Text below.
SQL>REM   Review SQL Text and adjust accordingly.
SQL>REM
SQL>REM PARAMETERS
SQL>REM   None.
SQL>REM
SQL>REM EXAMPLE
SQL>REM   SQL> START coe_xfr_sql_profile_36cbabzyq13gy_1849931106.sql;
SQL>REM
SQL>REM NOTES
SQL>REM   1. Should be run as SYSTEM or SYSDBA.
SQL>REM   2. User must have CREATE ANY SQL PROFILE privilege.
SQL>REM   3. SOURCE and TARGET systems can be the same or similar.
SQL>REM   4. To drop this custom SQL Profile after it has been created:
SQL>REM  EXEC DBMS_SQLTUNE.DROP_SQL_PROFILE('coe_36cbabzyq13gy_1849931106');
SQL>REM   5. Be aware that using DBMS_SQLTUNE requires a license
SQL>REM  for the Oracle Tuning Pack.
SQL>REM
SQL>WHENEVER SQLERROR EXIT SQL.SQLCODE;
SQL>REM
SQL>VAR signature NUMBER;
SQL>REM
SQL>DECLARE
  2  sql_txt CLOB;
  3  h       SYS.SQLPROF_ATTR;
  4  BEGIN
  5  sql_txt := q'[
  6  SELECT /*+ index(lt,
  7  PK_LV_URBAN_TOPAY_TMP)
  8  */                 bi.indi_id,
  9  bi.name,
 10  pt.pers_name,
 11  bs.sex_name,
 12  lt.pay_money,
 13  bi.idcard,
 14  bi.birthday,
 15  bf.headed_name,
 16  lt.fac_pay_date,
 17  lbb.audit_man,
 18  tab_hosp.hospital_name as hospital_name,
 19  to_char(lbb.make_bill_tm,
 20  'yyyy-mm-dd')
 21  as make_bill_tm,
 22  bf.telephone,
 23  nvl((decode(lt.intensive_disability_flag,
 24  1,
 25  decode(lt.lowflag,
 26  1,
 27  '重症伤残,
 28  ',
 29  '重症伤残')
 30  ,
 31  '')
 32  ||                 decode(lt.lowflag,
 33  1,
 34  decode(lt.nothing_flag,
 35  1,
 36  '低保,
 37  ',
 38  '低保')
 39  ,
 40  '')
 41  ||                 decode(lt.nothing_flag,
 42  1,
 43  '三无',
 44  '')
 45  )
 46  ,
 47  '标准')
 48  as subsidykide           FROM lv_urban_topay_tmp lt,
 49  bs_insured         bi,
 50  bs_sex             bs,
 51  bs_person_type     pt,
 52  bs_pres_insur      bpi,
 53  bs_family          bf,
 54  lv_busi_bill       lbb,
 55  lv_busi_record     lbr,
 56  lv_busi_assign     lba,
 57  (select bh.hospital_name,
 58  bph.indi_id        from bs_pers_hosp bph,
 59  bs_hospital bh        where bph.hospital_id = bh.hospital_id        and bph
.first_flag = 1       and bph.end_year = '2015')
 60  tab_hosp           WHERE nvl(lt.busi_asg_no,
 61  0)
 62  <> 0             AND nvl(lt.busi_asg_no,
 63  0)
 64  not in (-999,
 65  -998,
 66  -997,
 67  -981,
 68  -980)
 69  AND lt.fac_pay_date is not null             AND bi.indi_id = lt.indi_id
         AND bs.sex = bi.sex             AND bi.indi_id = tab_hosp.indi_id(+)
 70  AND lbr.busi_reco_no = lba.busi_reco_no                 AND lbr.busi_bill_s
n = lbb.busi_bill_sn                 AND lt.center_id = lbb.center_id
    AND lt.busi_asg_no = lba.busi_asg_no                AND lt.indi_id =  bi.ind
i_id             AND pt.pers_type = bi.pers_type             AND bpi.indi_id = b
i.indi_id             AND lt.center_id = pt.center_id             AND bf.family_
id = bi.family_id            AND bf.family_sta = 1             AND bi.indi_sta =
 1             AND bpi.indi_join_sta = 1            AND bf.center_id = lt.center
_id             AND bf.corp_id = lt.corp_id             AND   lt.policy_item_cod
e like '%INDI_TOPAY'
 71  AND   lt.corp_id='19159'             AND   bpi.insr_detail_code=21
     AND   lt.center_id= '430726'              AND   lt.curr_year= '2015'
    AND  lt.fac_pay_date >= to_date('2014-12-01 00:00:00',
 72  'yyyy-MM-dd hh24:mi:ss')
 73  AND  lt.fac_pay_date < = to_date('2015-01-05 23:59:59',
 74  'yyyy-MM-dd hh24:mi:ss')
 75  and exists             (select 'X' FROM lv_busi_bill       lbb,
 76  lv_busi_record     lbr,
 77  lv_busi_assign     lba,
 78  lv_urban_topay_tmp lutt           WHERE lbr.busi_reco_no = lba.busi_reco_no
             AND lbr.busi_bill_sn = lbb.busi_bill_sn               AND   lbb.cen
ter_id= '430726'             AND   lutt.corp_id='19159'            AND lutt.cent
er_id = lbb.center_id             AND lutt.busi_asg_no = lba.busi_asg_no
      and  lba.busi_asg_no  =   lt.busi_asg_no              and lutt.indi_id =
bi.indi_id)
 79  order by lt.fac_pay_date,
 80  bi.indi_id,
 81  bi.name
 82  ]';
 83  h := SYS.SQLPROF_ATTR(
 84  q'[BEGIN_OUTLINE_DATA]',
 85  q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
 86  q'[OPTIMIZER_FEATURES_ENABLE('10.2.0.4')]',
 87  q'[OPT_PARAM('optimizer_index_cost_adj' 20)]',
 88  q'[OPT_PARAM('optimizer_index_caching' 90)]',
 89  q'[ALL_ROWS]',
 90  q'[OUTLINE_LEAF(@"SEL$3")]',
 91  q'[OUTLINE_LEAF(@"SEL$639F1A6F")]',
 92  q'[PUSH_PRED(@"SEL$1" "TAB_HOSP"@"SEL$1" 12)]',
 93  q'[OUTLINE_LEAF(@"SEL$1")]',
 94  q'[OUTLINE(@"SEL$3")]',
 95  q'[OUTLINE(@"SEL$2")]',
 96  q'[OUTLINE(@"SEL$1")]',
 97  q'[INDEX_RS_ASC(@"SEL$1" "LT"@"SEL$1" ("LV_URBAN_TOPAY_TMP"."CORP_ID" "LV_U
RBAN_TOPAY_TMP"."CURR_YEAR" "LV_URBAN_TOPAY_TMP"."PERS_TYPE"))]',
 98  q'[INDEX_RS_ASC(@"SEL$1" "LBA"@"SEL$1" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',

 99  q'[INDEX_RS_ASC(@"SEL$1" "LBR"@"SEL$1" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]'
,
100  q'[INDEX_RS_ASC(@"SEL$1" "LBB"@"SEL$1" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
101  q'[INDEX_RS_ASC(@"SEL$1" "BI"@"SEL$1" ("BS_INSURED"."INDI_ID"))]',
102  q'[INDEX_RS_ASC(@"SEL$1" "BF"@"SEL$1" ("BS_FAMILY"."FAMILY_ID"))]',
103  q'[INDEX_RS_ASC(@"SEL$1" "PT"@"SEL$1" ("BS_PERSON_TYPE"."PERS_TYPE" "BS_PER
SON_TYPE"."CENTER_ID"))]',
104  q'[INDEX(@"SEL$1" "BPI"@"SEL$1" ("BS_PRES_INSUR"."INDI_ID" "BS_PRES_INSUR".
"INSR_DETAIL_CODE" "BS_PRES_INSUR"."INDI_JOIN_STA"))]',
105  q'[NO_ACCESS(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
106  q'[INDEX_RS_ASC(@"SEL$1" "BS"@"SEL$1" ("BS_SEX"."SEX"))]',
107  q'[LEADING(@"SEL$1" "LT"@"SEL$1" "LBA"@"SEL$1" "LBR"@"SEL$1" "LBB"@"SEL$1"
"BI"@"SEL$1" "BF"@"SEL$1" "PT"@"SEL$1" "BPI"@"SEL$1" "TAB_HOSP"@"SEL$1" "BS"@"SE
L$1")]',
108  q'[USE_NL(@"SEL$1" "LBA"@"SEL$1")]',
109  q'[USE_NL(@"SEL$1" "LBR"@"SEL$1")]',
110  q'[USE_NL(@"SEL$1" "LBB"@"SEL$1")]',
111  q'[USE_NL(@"SEL$1" "BI"@"SEL$1")]',
112  q'[USE_NL(@"SEL$1" "BF"@"SEL$1")]',
113  q'[USE_NL(@"SEL$1" "PT"@"SEL$1")]',
114  q'[USE_NL(@"SEL$1" "BPI"@"SEL$1")]',
115  q'[USE_NL(@"SEL$1" "TAB_HOSP"@"SEL$1")]',
116  q'[USE_NL(@"SEL$1" "BS"@"SEL$1")]',
117  q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BPH"@"SEL$2" ("BS_PERS_HOSP"."INDI_ID" "BS
_PERS_HOSP"."HOSPITAL_ID" "BS_PERS_HOSP"."FIRST_FLAG" "BS_PERS_HOSP"."BEG_YEAR"
"BS_PERS_HOSP"."BIZ_TYPE"))]',
118  q'[INDEX_RS_ASC(@"SEL$639F1A6F" "BH"@"SEL$2" ("BS_HOSPITAL"."HOSPITAL_ID"))
]',
119  q'[LEADING(@"SEL$639F1A6F" "BPH"@"SEL$2" "BH"@"SEL$2")]',
120  q'[USE_NL(@"SEL$639F1A6F" "BH"@"SEL$2")]',
121  q'[INDEX_RS_ASC(@"SEL$3" "LBA"@"SEL$3" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))]',

122  q'[INDEX_RS_ASC(@"SEL$3" "LBR"@"SEL$3" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))]'
,
123  q'[INDEX_RS_ASC(@"SEL$3" "LBB"@"SEL$3" ("LV_BUSI_BILL"."BUSI_BILL_SN"))]',
124  q'[INDEX_RS_ASC(@"SEL$3" "LUTT"@"SEL$3" ("LV_URBAN_TOPAY_TMP"."INDI_ID"))]'
,
125  q'[LEADING(@"SEL$3" "LBA"@"SEL$3" "LBR"@"SEL$3" "LBB"@"SEL$3" "LUTT"@"SEL$3
")]',
126  q'[USE_NL(@"SEL$3" "LBR"@"SEL$3")]',
127  q'[USE_NL(@"SEL$3" "LBB"@"SEL$3")]',
128  q'[USE_NL(@"SEL$3" "LUTT"@"SEL$3")]',
129  q'[END_OUTLINE_DATA]');
130  :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
131  DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
132  sql_text    => sql_txt,
133  profile     => h,
134  name        => 'coe_36cbabzyq13gy_1849931106',
135  description => 'coe 36cbabzyq13gy 1849931106 '||:signature||'',
136  category    => 'DEFAULT',
137  validate    => TRUE,
138  replace     => TRUE,
139  force_match => TRUE /* TRUE:FORCE (match even when different literals in SQ
L). FALSE:EXACT (similar to CURSOR_SHARING) */ );
140  END;
141  /

PL/SQL 过程已成功完成。

SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;

            SIGNATURE
---------------------
   718970022444771957


... manual custom SQL Profile has been created


COE_XFR_SQL_PROFILE_36cbabzyq13gy_1849931106 completed

现在我们再来执行原SQL语句来检测SQL Profile是否生效:

SQL>SELECT /*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */
 bi.indi_id,
 bi.name,
 pt.pers_name,
 bs.sex_name,
 lt.pay_money,
 bi.idcard,
 bi.birthday,
 bf.headed_name,
 lt.fac_pay_date,
 lbb.audit_man,
 tab_hosp.hospital_name as hospital_name,
 to_char(lbb.make_bill_tm, 'yyyy-mm-dd') as make_bill_tm,
 bf.telephone,
 nvl((decode(lt.intensive_disability_flag,
             1,
             decode(lt.lowflag, 1, '重症伤残,', '重症伤残'),
             '') ||
     decode(lt.lowflag, 1, decode(lt.nothing_flag, 1, '低保,', '低保'), '') ||
     decode(lt.nothing_flag, 1, '三无', '')),
     '标准') as subsidykide
  FROM lv_urban_topay_tmp lt,
       bs_insured bi,
       bs_sex bs,
       bs_person_type pt,
       bs_pres_insur bpi,
       bs_family bf,
       lv_busi_bill lbb,
       lv_busi_record lbr,
       lv_busi_assign lba,
       (select bh.hospital_name, bph.indi_id
          from bs_pers_hosp bph, bs_hospital bh
         where bph.hospital_id = bh.hospital_id
           and bph.first_flag = 1
           and bph.end_year = '2015') tab_hosp
 WHERE nvl(lt.busi_asg_no, 0) <> 0
   AND nvl(lt.busi_asg_no, 0) not in (-999, -998, -997, -981, -980)
   AND lt.fac_pay_date is not null
   AND bi.indi_id = lt.indi_id
   AND bs.sex = bi.sex
   AND bi.indi_id = tab_hosp.indi_id(+)
   AND lbr.busi_reco_no = lba.busi_reco_no
   AND lbr.busi_bill_sn = lbb.busi_bill_sn
   AND lt.center_id = lbb.center_id
   AND lt.busi_asg_no = lba.busi_asg_no
   AND lt.indi_id = bi.indi_id
   AND pt.pers_type = bi.pers_type
   AND bpi.indi_id = bi.indi_id
   AND lt.center_id = pt.center_id
   AND bf.family_id = bi.family_id
   AND bf.family_sta = 1
   AND bi.indi_sta = 1
   AND bpi.indi_join_sta = 1
   AND bf.center_id = lt.center_id
   AND bf.corp_id = lt.corp_id
   AND lt.policy_item_code like '%INDI_TOPAY'
   AND lt.corp_id = '19159'
   AND bpi.insr_detail_code = 21
   AND lt.center_id = '430726'
   AND lt.curr_year = '2015'
   AND lt.fac_pay_date >= to_date('2014-12-01 00:00:00',
                                  'yyyy-MM-dd hh24:mi:ss')
   AND lt.fac_pay_date < =
       to_date('2015-01-05 23:59:59', 'yyyy-MM-dd hh24:mi:ss')
   and exists (select 'X'
          FROM lv_busi_bill       lbb,
               lv_busi_record     lbr,
               lv_busi_assign     lba,
               lv_urban_topay_tmp lutt
         WHERE lbr.busi_reco_no = lba.busi_reco_no
           AND lbr.busi_bill_sn = lbb.busi_bill_sn
           AND lbb.center_id = '430726'
           AND lutt.corp_id = '19159'
           AND lutt.center_id = lbb.center_id
           AND lutt.busi_asg_no = lba.busi_asg_no
           and lba.busi_asg_no = lt.busi_asg_no
           and lutt.indi_id = bi.indi_id)
 order by lt.fac_pay_date, bi.indi_id, bi.name
...省略输入结果
2304 rows selected.

Elapsed: 00:00:08.08

SQL> select * from table(dbms_xplan.display_cursor('3z8rmv9d64xyx',0,'advanced'));                                                                           

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  3z8rmv9d64xyx, child number 0
-------------------------------------
SELECT /*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */  bi.indi_id,  bi.name,  pt.pers_name,  bs.sex_name,  lt.pay_money,
bi.idcard,  bi.birthday,  bf.headed_name,  lt.fac_pay_date,  lbb.audit_man,  tab_hosp.hospital_name as
hospital_name,  to_char(lbb.make_bill_tm, 'yyyy-mm-dd') as make_bill_tm,  bf.telephone,
nvl((decode(lt.intensive_disability_flag,              1,              decode(lt.lowflag, 1, '重症伤残,', '重症伤残'),
          '') ||      decode(lt.lowflag, 1, decode(lt.nothing_flag, 1, '低保,', '低保'), '') ||
decode(lt.nothing_flag, 1, '三无', '')),      '标准') as subsidykide   FROM lv_urban_topay_tmp lt,        bs_insured
bi,        bs_sex bs,        bs_person_type pt,        bs_pres_insur bpi,        bs_family bf,
lv_busi_bill lbb,        lv_busi_record lbr,        lv_busi_assign lba,        (select bh.hospital_name,
bph.indi_id           from bs_pers_hosp bph, bs_hospital bh          where bph.hospital_id = bh.hospital_id
     and bph.first_flag = 1

Plan hash value: 3748535674

--------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |                                |       |       |    88 (100)|          |
|   1 |  SORT ORDER BY                          |                                |     1 |   278 |    88   (2)| 00:00:02 |
|   2 |   NESTED LOOPS                          |                                |     1 |   278 |    82   (0)| 00:00:01 |
|   3 |    NESTED LOOPS OUTER                   |                                |     1 |   272 |    81   (0)| 00:00:01 |
|   4 |     NESTED LOOPS                        |                                |     1 |   231 |    79   (0)| 00:00:01 |
|   5 |      NESTED LOOPS                       |                                |     1 |   219 |    78   (0)| 00:00:01 |
|   6 |       NESTED LOOPS                      |                                |     1 |   201 |    77   (0)| 00:00:01 |
|   7 |        NESTED LOOPS                     |                                |     1 |   169 |    76   (0)| 00:00:01 |
|   8 |         NESTED LOOPS                    |                                |     1 |   113 |    75   (0)| 00:00:01 |
|   9 |          NESTED LOOPS                   |                                |     1 |    88 |    74   (0)| 00:00:01 |
|  10 |           NESTED LOOPS                  |                                |     1 |    77 |    73   (0)| 00:00:01 |
|* 11 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |     1 |    65 |    72   (0)| 00:00:01 |
|* 12 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_CORP_ID |   954 |       |     1   (0)| 00:00:01 |
|  13 |            TABLE ACCESS BY INDEX ROWID  | LV_BUSI_ASSIGN                 |     1 |    12 |     1   (0)| 00:00:01 |
|* 14 |             INDEX UNIQUE SCAN           | PK_LV_BUSI_ASSIGN              |     1 |       |     1   (0)| 00:00:01 |
|  15 |           TABLE ACCESS BY INDEX ROWID   | LV_BUSI_RECORD                 |     1 |    11 |     1   (0)| 00:00:01 |
|* 16 |            INDEX UNIQUE SCAN            | PK_LV_BUSI_RECORD              |     1 |       |     1   (0)| 00:00:01 |
|* 17 |          TABLE ACCESS BY INDEX ROWID    | LV_BUSI_BILL                   |     1 |    25 |     1   (0)| 00:00:01 |
|* 18 |           INDEX UNIQUE SCAN             | PK_LV_BUSI_BILL                |     1 |       |     1   (0)| 00:00:01 |
|* 19 |         TABLE ACCESS BY INDEX ROWID     | BS_INSURED                     |     1 |    56 |     1   (0)| 00:00:01 |
|* 20 |          INDEX UNIQUE SCAN              | PK_BS_INSURED                  |     1 |       |     1   (0)| 00:00:01 |
|  21 |           NESTED LOOPS                  |                                |     1 |    56 |     5   (0)| 00:00:01 |
|  22 |            NESTED LOOPS                 |                                |     1 |    35 |     3   (0)| 00:00:01 |
|  23 |             NESTED LOOPS                |                                |     1 |    23 |     2   (0)| 00:00:01 |
|  24 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_ASSIGN                 |     1 |    12 |     1   (0)| 00:00:01 |
|* 25 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_ASSIGN              |     1 |       |     1   (0)| 00:00:01 |
|  26 |              TABLE ACCESS BY INDEX ROWID| LV_BUSI_RECORD                 |     1 |    11 |     1   (0)| 00:00:01 |
|* 27 |               INDEX UNIQUE SCAN         | PK_LV_BUSI_RECORD              |     1 |       |     1   (0)| 00:00:01 |
|* 28 |             TABLE ACCESS BY INDEX ROWID | LV_BUSI_BILL                   |     1 |    12 |     1   (0)| 00:00:01 |
|* 29 |              INDEX UNIQUE SCAN          | PK_LV_BUSI_BILL                |     1 |       |     1   (0)| 00:00:01 |
|* 30 |            TABLE ACCESS BY INDEX ROWID  | LV_URBAN_TOPAY_TMP             |     1 |    21 |     2   (0)| 00:00:01 |
|* 31 |             INDEX RANGE SCAN            | IDX_LV_URBAN_TOPAY_TMP_INDI    |    12 |       |     1   (0)| 00:00:01 |
|* 32 |        TABLE ACCESS BY INDEX ROWID      | BS_FAMILY                      |     1 |    32 |     1   (0)| 00:00:01 |
|* 33 |         INDEX UNIQUE SCAN               | PK_BS_FAMILY                   |     1 |       |     1   (0)| 00:00:01 |
|  34 |       TABLE ACCESS BY INDEX ROWID       | BS_PERSON_TYPE                 |     1 |    18 |     1   (0)| 00:00:01 |
|* 35 |        INDEX UNIQUE SCAN                | PK_BS_PERSON_TYPE              |     1 |       |     1   (0)| 00:00:01 |
|* 36 |      INDEX UNIQUE SCAN                  | INDEX_BS_PRES_INSUR_UNIQUE     |     1 |    12 |     1   (0)| 00:00:01 |
|  37 |     VIEW PUSHED PREDICATE               |                                |     1 |    41 |     2   (0)| 00:00:01 |
|  38 |      NESTED LOOPS                       |                                |     1 |    57 |     2   (0)| 00:00:01 |
|* 39 |       TABLE ACCESS BY INDEX ROWID       | BS_PERS_HOSP                   |     1 |    25 |     1   (0)| 00:00:01 |
|* 40 |        INDEX RANGE SCAN                 | PK_BS_PERS_HOSP                |     2 |       |     1   (0)| 00:00:01 |
|  41 |       TABLE ACCESS BY INDEX ROWID       | BS_HOSPITAL                    |     1 |    32 |     1   (0)| 00:00:01 |
|* 42 |        INDEX UNIQUE SCAN                | PK_BS_HOSPITAL                 |     1 |       |     1   (0)| 00:00:01 |
|  43 |    TABLE ACCESS BY INDEX ROWID          | BS_SEX                         |     1 |     6 |     1   (0)| 00:00:01 |
|* 44 |     INDEX UNIQUE SCAN                   | PK_BS_SEX                      |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1
  11 - SEL$1        / LT@SEL$1
  12 - SEL$1        / LT@SEL$1
  13 - SEL$1        / LBA@SEL$1
  14 - SEL$1        / LBA@SEL$1
  15 - SEL$1        / LBR@SEL$1
  16 - SEL$1        / LBR@SEL$1
  17 - SEL$1        / LBB@SEL$1
  18 - SEL$1        / LBB@SEL$1
  19 - SEL$1        / BI@SEL$1
  20 - SEL$1        / BI@SEL$1
  21 - SEL$3
  24 - SEL$3        / LBA@SEL$3
  25 - SEL$3        / LBA@SEL$3
  26 - SEL$3        / LBR@SEL$3
  27 - SEL$3        / LBR@SEL$3
  28 - SEL$3        / LBB@SEL$3
  29 - SEL$3        / LBB@SEL$3
  30 - SEL$3        / LUTT@SEL$3
  31 - SEL$3        / LUTT@SEL$3
  32 - SEL$1        / BF@SEL$1
  33 - SEL$1        / BF@SEL$1
  34 - SEL$1        / PT@SEL$1
  35 - SEL$1        / PT@SEL$1
  36 - SEL$1        / BPI@SEL$1
  37 - SEL$639F1A6F / TAB_HOSP@SEL$1
  38 - SEL$639F1A6F
  39 - SEL$639F1A6F / BPH@SEL$2
  40 - SEL$639F1A6F / BPH@SEL$2
  41 - SEL$639F1A6F / BH@SEL$2
  42 - SEL$639F1A6F / BH@SEL$2
  43 - SEL$1        / BS@SEL$1
  44 - SEL$1        / BS@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('10.2.0.4')
      OPT_PARAM('optimizer_index_cost_adj' 20)
      OPT_PARAM('optimizer_index_caching' 90)
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$3")
      OUTLINE_LEAF(@"SEL$639F1A6F")
      PUSH_PRED(@"SEL$1" "TAB_HOSP"@"SEL$1" 12)
      OUTLINE_LEAF(@"SEL$1")
      OUTLINE(@"SEL$3")
      OUTLINE(@"SEL$2")
      OUTLINE(@"SEL$1")
      INDEX_RS_ASC(@"SEL$1" "LT"@"SEL$1" ("LV_URBAN_TOPAY_TMP"."CORP_ID" "LV_URBAN_TOPAY_TMP"."CURR_YEAR"
              "LV_URBAN_TOPAY_TMP"."PERS_TYPE"))
      INDEX_RS_ASC(@"SEL$1" "LBA"@"SEL$1" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))
      INDEX_RS_ASC(@"SEL$1" "LBR"@"SEL$1" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))
      INDEX_RS_ASC(@"SEL$1" "LBB"@"SEL$1" ("LV_BUSI_BILL"."BUSI_BILL_SN"))
      INDEX_RS_ASC(@"SEL$1" "BI"@"SEL$1" ("BS_INSURED"."INDI_ID"))
      INDEX_RS_ASC(@"SEL$1" "BF"@"SEL$1" ("BS_FAMILY"."FAMILY_ID"))
      INDEX_RS_ASC(@"SEL$1" "PT"@"SEL$1" ("BS_PERSON_TYPE"."PERS_TYPE" "BS_PERSON_TYPE"."CENTER_ID"))
      INDEX(@"SEL$1" "BPI"@"SEL$1" ("BS_PRES_INSUR"."INDI_ID" "BS_PRES_INSUR"."INSR_DETAIL_CODE"
              "BS_PRES_INSUR"."INDI_JOIN_STA"))
      NO_ACCESS(@"SEL$1" "TAB_HOSP"@"SEL$1")
      INDEX_RS_ASC(@"SEL$1" "BS"@"SEL$1" ("BS_SEX"."SEX"))
      LEADING(@"SEL$1" "LT"@"SEL$1" "LBA"@"SEL$1" "LBR"@"SEL$1" "LBB"@"SEL$1" "BI"@"SEL$1" "BF"@"SEL$1" "PT"@"SEL$1"
              "BPI"@"SEL$1" "TAB_HOSP"@"SEL$1" "BS"@"SEL$1")
      USE_NL(@"SEL$1" "LBA"@"SEL$1")
      USE_NL(@"SEL$1" "LBR"@"SEL$1")
      USE_NL(@"SEL$1" "LBB"@"SEL$1")
      USE_NL(@"SEL$1" "BI"@"SEL$1")
      USE_NL(@"SEL$1" "BF"@"SEL$1")
      USE_NL(@"SEL$1" "PT"@"SEL$1")
      USE_NL(@"SEL$1" "BPI"@"SEL$1")
      USE_NL(@"SEL$1" "TAB_HOSP"@"SEL$1")
      USE_NL(@"SEL$1" "BS"@"SEL$1")
      INDEX_RS_ASC(@"SEL$639F1A6F" "BPH"@"SEL$2" ("BS_PERS_HOSP"."INDI_ID" "BS_PERS_HOSP"."HOSPITAL_ID"
              "BS_PERS_HOSP"."FIRST_FLAG" "BS_PERS_HOSP"."BEG_YEAR" "BS_PERS_HOSP"."BIZ_TYPE"))
      INDEX_RS_ASC(@"SEL$639F1A6F" "BH"@"SEL$2" ("BS_HOSPITAL"."HOSPITAL_ID"))
      LEADING(@"SEL$639F1A6F" "BPH"@"SEL$2" "BH"@"SEL$2")
      USE_NL(@"SEL$639F1A6F" "BH"@"SEL$2")
      INDEX_RS_ASC(@"SEL$3" "LBA"@"SEL$3" ("LV_BUSI_ASSIGN"."BUSI_ASG_NO"))
      INDEX_RS_ASC(@"SEL$3" "LBR"@"SEL$3" ("LV_BUSI_RECORD"."BUSI_RECO_NO"))
      INDEX_RS_ASC(@"SEL$3" "LBB"@"SEL$3" ("LV_BUSI_BILL"."BUSI_BILL_SN"))
      INDEX_RS_ASC(@"SEL$3" "LUTT"@"SEL$3" ("LV_URBAN_TOPAY_TMP"."INDI_ID"))
      LEADING(@"SEL$3" "LBA"@"SEL$3" "LBR"@"SEL$3" "LBB"@"SEL$3" "LUTT"@"SEL$3")
      USE_NL(@"SEL$3" "LBR"@"SEL$3")
      USE_NL(@"SEL$3" "LBB"@"SEL$3")
      USE_NL(@"SEL$3" "LUTT"@"SEL$3")
      END_OUTLINE_DATA
  */

Predicate Information (identified by operation id):
---------------------------------------------------

  11 - filter(("LT"."FAC_PAY_DATE">=TO_DATE(' 2014-12-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
              "LT"."CENTER_ID"='430726' AND "LT"."POLICY_ITEM_CODE" LIKE '%INDI_TOPAY' AND "LT"."FAC_PAY_DATE" IS NOT NULL AND
              "LT"."BUSI_ASG_NO" IS NOT NULL AND NVL("LT"."BUSI_ASG_NO",0)<>0 AND NVL("LT"."BUSI_ASG_NO",0)<>(-980) AND
              NVL("LT"."BUSI_ASG_NO",0)<>(-981) AND NVL("LT"."BUSI_ASG_NO",0)<>(-997) AND NVL("LT"."BUSI_ASG_NO",0)<>(-998) AND
              NVL("LT"."BUSI_ASG_NO",0)<>(-999) AND "LT"."FAC_PAY_DATE"< =TO_DATE(' 2015-01-05 23:59:59', 'syyyy-mm-dd
              hh24:mi:ss')))
  12 - access("LT"."CORP_ID"=19159 AND "LT"."CURR_YEAR"='2015')
  14 - access("LT"."BUSI_ASG_NO"="LBA"."BUSI_ASG_NO")
  16 - access("LBR"."BUSI_RECO_NO"="LBA"."BUSI_RECO_NO")
  17 - filter("LBB"."CENTER_ID"='430726')
  18 - access("LBR"."BUSI_BILL_SN"="LBB"."BUSI_BILL_SN")
  19 - filter(("BI"."FAMILY_ID" IS NOT NULL AND "BI"."INDI_STA"=1))
  20 - access("BI"."INDI_ID"="LT"."INDI_ID")
       filter( IS NOT NULL)
  25 - access("LBA"."BUSI_ASG_NO"=:B1)
  27 - access("LBR"."BUSI_RECO_NO"="LBA"."BUSI_RECO_NO")
  28 - filter("LBB"."CENTER_ID"='430726')
  29 - access("LBR"."BUSI_BILL_SN"="LBB"."BUSI_BILL_SN")
  30 - filter(("LUTT"."BUSI_ASG_NO"=:B1 AND "LUTT"."CORP_ID"=19159 AND "LUTT"."CENTER_ID"='430726'))
  31 - access("LUTT"."INDI_ID"=:B1)
  32 - filter(("BF"."CORP_ID"=19159 AND "BF"."CENTER_ID"='430726' AND "BF"."FAMILY_STA"=1))
  33 - access("BF"."FAMILY_ID"="BI"."FAMILY_ID")
  35 - access("PT"."PERS_TYPE"="BI"."PERS_TYPE" AND "PT"."CENTER_ID"='430726')
  36 - access("BPI"."INDI_ID"="BI"."INDI_ID" AND "BPI"."INSR_DETAIL_CODE"=21 AND "BPI"."INDI_JOIN_STA"=1)
  39 - filter("BPH"."END_YEAR"='2015')
  40 - access("BPH"."INDI_ID"="BI"."INDI_ID" AND "BPH"."FIRST_FLAG"=1)
       filter("BPH"."FIRST_FLAG"=1)
  42 - access("BPH"."HOSPITAL_ID"="BH"."HOSPITAL_ID")
  44 - access("BS"."SEX"=TO_NUMBER("BI"."SEX"))

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - (#keys=3) "LT"."FAC_PAY_DATE"[DATE,7], "BI"."INDI_ID"[NUMBER,22], "BI"."NAME"[VARCHAR2,20],
       "BF"."TELEPHONE"[VARCHAR2,20], TO_CHAR(INTERNAL_FUNCTION("LBB"."MAKE_BILL_TM"),'yyyy-mm-dd')[10],
       "PT"."PERS_NAME"[VARCHAR2,20], "BS"."SEX_NAME"[VARCHAR2,5], "LT"."PAY_MONEY"[NUMBER,22],
       "BI"."IDCARD"[VARCHAR2,21], "BI"."BIRTHDAY"[DATE,7], "BF"."HEADED_NAME"[VARCHAR2,50],
       NVL(DECODE("LT"."INTENSIVE_DISABILITY_FLAG",1,DECODE("LT"."LOWFLAG",1,'重症伤残,','重症伤残'),'')||DECODE("LT"."LOWFLAG",1
       ,DECODE("LT"."NOTHING_FLAG",1,'低保,','低保'),'')||DECODE("LT"."NOTHING_FLAG",1,'三无',''),'标准')[18],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "TAB_HOSP"."HOSPITAL_NAME"[VARCHAR2,70]
   2 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."NAME"[VARCHAR2,20], "BI"."BIRTHDAY"[DATE,7],
       "BI"."IDCARD"[VARCHAR2,21], "BF"."HEADED_NAME"[VARCHAR2,50], "BF"."TELEPHONE"[VARCHAR2,20],
       "PT"."PERS_NAME"[VARCHAR2,20], "TAB_HOSP"."HOSPITAL_NAME"[VARCHAR2,70], "BS"."SEX_NAME"[VARCHAR2,5]
   3 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."NAME"[VARCHAR2,20], "BI"."SEX"[CHARACTER,1],
       "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BF"."HEADED_NAME"[VARCHAR2,50],
       "BF"."TELEPHONE"[VARCHAR2,20], "PT"."PERS_NAME"[VARCHAR2,20], "TAB_HOSP"."HOSPITAL_NAME"[VARCHAR2,70]
   4 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."NAME"[VARCHAR2,20], "BI"."SEX"[CHARACTER,1],
       "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BF"."HEADED_NAME"[VARCHAR2,50],
       "BF"."TELEPHONE"[VARCHAR2,20], "PT"."PERS_NAME"[VARCHAR2,20]
   5 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."NAME"[VARCHAR2,20], "BI"."SEX"[CHARACTER,1],
       "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BF"."HEADED_NAME"[VARCHAR2,50],
       "BF"."TELEPHONE"[VARCHAR2,20], "PT"."PERS_NAME"[VARCHAR2,20]
   6 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."PERS_TYPE"[NUMBER,22], "BI"."NAME"[VARCHAR2,20],
       "BI"."SEX"[CHARACTER,1], "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BF"."HEADED_NAME"[VARCHAR2,50],
       "BF"."TELEPHONE"[VARCHAR2,20]
   7 - "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22], "LT"."LOWFLAG"[NUMBER,22],
       "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7],
       "LBB"."AUDIT_MAN"[VARCHAR2,50], "BI"."INDI_ID"[NUMBER,22], "BI"."PERS_TYPE"[NUMBER,22], "BI"."NAME"[VARCHAR2,20],
       "BI"."SEX"[CHARACTER,1], "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BI"."FAMILY_ID"[NUMBER,22]
   8 - "LT"."INDI_ID"[NUMBER,22], "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22],
       "LT"."LOWFLAG"[NUMBER,22], "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."BUSI_ASG_NO"[NUMBER,22],
       "LT"."FAC_PAY_DATE"[DATE,7], "LBB"."MAKE_BILL_TM"[DATE,7], "LBB"."AUDIT_MAN"[VARCHAR2,50]
   9 - "LT"."INDI_ID"[NUMBER,22], "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22],
       "LT"."LOWFLAG"[NUMBER,22], "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."BUSI_ASG_NO"[NUMBER,22],
       "LT"."FAC_PAY_DATE"[DATE,7], "LBR"."BUSI_BILL_SN"[NUMBER,22]
  10 - "LT"."INDI_ID"[NUMBER,22], "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22],
       "LT"."LOWFLAG"[NUMBER,22], "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."BUSI_ASG_NO"[NUMBER,22],
       "LT"."FAC_PAY_DATE"[DATE,7], "LBA"."BUSI_RECO_NO"[NUMBER,22]
  11 - "LT"."INDI_ID"[NUMBER,22], "LT"."INTENSIVE_DISABILITY_FLAG"[NUMBER,22], "LT"."PAY_MONEY"[NUMBER,22],
       "LT"."LOWFLAG"[NUMBER,22], "LT"."NOTHING_FLAG"[NUMBER,22], "LT"."BUSI_ASG_NO"[NUMBER,22],
       "LT"."FAC_PAY_DATE"[DATE,7]
  12 - "SYS_ALIAS_1".ROWID[ROWID,10]
  13 - "LBA"."BUSI_RECO_NO"[NUMBER,22]
  14 - "LBA".ROWID[ROWID,10]
  15 - "LBR"."BUSI_BILL_SN"[NUMBER,22]
  16 - "LBR".ROWID[ROWID,10]
  17 - "LBB"."MAKE_BILL_TM"[DATE,7], "LBB"."AUDIT_MAN"[VARCHAR2,50]
  18 - "LBB".ROWID[ROWID,10]
  19 - "BI"."INDI_ID"[NUMBER,22], "BI"."PERS_TYPE"[NUMBER,22], "BI"."NAME"[VARCHAR2,20], "BI"."SEX"[CHARACTER,1],
       "BI"."BIRTHDAY"[DATE,7], "BI"."IDCARD"[VARCHAR2,21], "BI"."FAMILY_ID"[NUMBER,22]
  20 - "SYS_ALIAS_2".ROWID[ROWID,10], "BI"."INDI_ID"[NUMBER,22]
  23 - "LBR"."BUSI_BILL_SN"[NUMBER,22]
  24 - "LBA"."BUSI_RECO_NO"[NUMBER,22]
  25 - "LBA".ROWID[ROWID,10]
  26 - "LBR"."BUSI_BILL_SN"[NUMBER,22]
  27 - "LBR".ROWID[ROWID,10]
  29 - "LBB".ROWID[ROWID,10]
  31 - "LUTT".ROWID[ROWID,10]
  32 - "BF"."HEADED_NAME"[VARCHAR2,50], "BF"."TELEPHONE"[VARCHAR2,20]
  33 - "BF".ROWID[ROWID,10]
  34 - "PT"."PERS_NAME"[VARCHAR2,20]
  35 - "PT".ROWID[ROWID,10]
  37 - "TAB_HOSP"."HOSPITAL_NAME"[VARCHAR2,70]
  38 - "BH"."HOSPITAL_NAME"[VARCHAR2,70]
  39 - "BPH"."HOSPITAL_ID"[VARCHAR2,20]
  40 - "BPH".ROWID[ROWID,10], "BPH"."HOSPITAL_ID"[VARCHAR2,20]
  41 - "BH"."HOSPITAL_NAME"[VARCHAR2,70]
  42 - "BH".ROWID[ROWID,10]
  43 - "BS"."SEX_NAME"[VARCHAR2,5]
  44 - "BS".ROWID[ROWID,10]

Note
-----
   - SQL profile "coe_36cbabzyq13gy_1849931106" used for this statement


276 rows selected.

从Note部分的SQL profile "coe_36cbabzyq13gy_1849931106" used for this statement可知SQL Profile对该SQL生效了,而且从执行计划中可知现在与去掉Hint“/*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */”之后的SQL执行计划一样的。至此,对该SQL的优化也就完成。

这个SQL语句执行缓慢的原因就是因为使用了Hint"/*+ index(lt,PK_LV_URBAN_TOPAY_TMP) */"后,使CBO选择了错误的执行计划而造成的。

Proudly powered by WordPress | Indrajeet by Sus Hill.