一条sql语句的优化

今天跟踪程序,无意中发现一条不良的SQL语句
今天跟踪程序,无意中发现一条不良的SQL语句

分析如下:

SELECT   fee_stat_name, NVL (t1.b, 0)
    FROM (SELECT   fee_code, NVL (SUM (tot_cost), 0) b
              FROM (SELECT "IF_BALANCELIST"."FEE_CODE",
                           "IF_BALANCELIST"."TOT_COST", '0', balance_dtime,
                           "IF_BALANCELIST"."OPER_CODE"
                      FROM "IF_BALANCELIST"
                     WHERE NVL (waste_flag, '0') '2'
                    UNION ALL
                    SELECT "IF_BALANCELIST"."FEE_CODE",
                           -"IF_BALANCELIST"."TOT_COST", '1', waste_dtime,
                           "OPER_CODE"
                      FROM "IF_BALANCELIST"
                     WHERE waste_flag = '1') ti1,
                   his_report_arg
             WHERE balance_dtime BETWEEN his_report_arg.start_dt
                                     AND his_report_arg.end_dt
               AND ti1.oper_code = his_report_arg.oper_code
               AND his_report_arg.SID = USERENV ('sessionid')
          GROUP BY fee_code) t1,
         (SELECT DISTINCT "IA_FEESTATNEXUS"."FEE_STAT_NAME",
                          "IA_FEESTATNEXUS"."FEE_STAT_CATE",
                          "IA_FEESTATNEXUS"."PRINT_ORDER"
                     FROM "IA_FEESTATNEXUS"
                    WHERE report_code = 'ZY01') t2
   WHERE t1.fee_code(+) = t2.fee_stat_cate
ORDER BY t2.print_order

执行计划如下:

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=44 Card=54 Bytes=2268)
   1    0   SORT (ORDER BY) (Cost=44 Card=54 Bytes=2268)
   2    1     HASH JOIN (OUTER) (Cost=40 Card=54 Bytes=2268)
   3    2       VIEW (Cost=5 Card=54 Bytes=1404)
   4    3         SORT (UNIQUE) (Cost=5 Card=54 Bytes=1296)
   5    4           TABLE ACCESS (BY INDEX ROWID) OF 'IA_FEESTATNEXUS' (Cost=1 Card=54 Bytes=1296)
   6    5             INDEX (RANGE SCAN) OF 'PK_IA_FEESTATNEXUS' (UNIQUE) (Cost=1 Card=54)
   7    2       VIEW (Cost=35 Card=4 Bytes=64)
   8    7         SORT (GROUP BY) (Cost=35 Card=4 Bytes=216)
   9    8           NESTED LOOPS (Cost=31 Card=4 Bytes=216)
  10    9             TABLE ACCESS (BY INDEX ROWID) OF 'HIS_REPORT_ARG' (Cost=1 Card=1 Bytes=24)
  11   10               INDEX (UNIQUE SCAN) OF 'PK_HIS_REPORT_ARG' (UNIQUE)
  12    9             VIEW
  13   12               UNION-ALL
  14   13                 TABLE ACCESS (FULL) OF 'IF_BALANCELIST' (Cost=15 Card=3047 Bytes=63987)
  15   13                 TABLE ACCESS (FULL) OF 'IF_BALANCELIST' (Cost=15 Card=20313 Bytes=304695)

Statistics
----------------------------------------------------------
          0  recursive calls
         10  db block gets
       1059  consistent gets
          0  physical reads
          0  redo size
       1713  bytes sent via SQL*Net to client
        535  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          3  sorts (memory)
          0  sorts (disk)
         23  rows processed

主要的问题在于IF_BALANCELIST要执行两次全表扫描。而判断条件中使用balance_dtime BETWEEN
his_report_arg.start_dt AND his_report_arg.end_dt,没有很好地利用IF_BALANCELIST建立的
balance_dtime与waste_dtime索引,应该修改如下:

SELECT   fee_stat_name, NVL (SUM (c), 0)
    FROM (SELECT fee_stat_name, NVL (t1.b, 0) c, t2.print_order
            FROM (SELECT   fee_code, NVL (SUM (tot_cost), 0) b
                      FROM (SELECT "IF_BALANCELIST"."FEE_CODE",
                                   "IF_BALANCELIST"."TOT_COST", '0',
                                   balance_dtime,
                                   "IF_BALANCELIST"."OPER_CODE"
                              FROM "IF_BALANCELIST"
                             WHERE NVL (waste_flag, '0') '2') ti1,
                           his_report_arg
                     WHERE balance_dtime BETWEEN his_report_arg.start_dt
                                             AND his_report_arg.end_dt
                       AND ti1.oper_code = his_report_arg.oper_code
                       AND his_report_arg.SID = USERENV ('sessionid')
                  GROUP BY fee_code) t1,
                 (SELECT DISTINCT "IA_FEESTATNEXUS"."FEE_STAT_NAME",
                                  "IA_FEESTATNEXUS"."FEE_STAT_CATE",
                                  "IA_FEESTATNEXUS"."PRINT_ORDER"
                             FROM "IA_FEESTATNEXUS"
                            WHERE report_code = 'ZY01') t2
           WHERE t1.fee_code(+) = t2.fee_stat_cate
          UNION ALL
          SELECT fee_stat_name, NVL (t1.b, 0) c, t2.print_order
            FROM (SELECT   fee_code, NVL (SUM (tot_cost), 0) b
                      FROM (SELECT "IF_BALANCELIST"."FEE_CODE",
                                   -"IF_BALANCELIST"."TOT_COST" tot_cost, '1',
                                   waste_dtime, "OPER_CODE"
                              FROM "IF_BALANCELIST"
                             WHERE waste_flag = '1') ti1,
                           his_report_arg
                     WHERE waste_dtime BETWEEN his_report_arg.start_dt
                                           AND his_report_arg.end_dt
                       AND ti1.oper_code = his_report_arg.oper_code
                       AND his_report_arg.SID = USERENV ('sessionid')
                  GROUP BY fee_code) t1,
                 (SELECT DISTINCT "IA_FEESTATNEXUS"."FEE_STAT_NAME",
                                  "IA_FEESTATNEXUS"."FEE_STAT_CATE",
                                  "IA_FEESTATNEXUS"."PRINT_ORDER"
                             FROM "IA_FEESTATNEXUS"
                            WHERE report_code = 'ZY01') t2
           WHERE t1.fee_code(+) = t2.fee_stat_cate)
GROUP BY fee_stat_name, print_order
ORDER BY print_order

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=26 Card=108 Bytes=3888)
   1    0   SORT (GROUP BY) (Cost=26 Card=108 Bytes=3888)
   2    1     VIEW (Cost=22 Card=108 Bytes=3888)
   3    2       UNION-ALL
   4    3         HASH JOIN (OUTER) (Cost=11 Card=54 Bytes=2268)
   5    4           VIEW (Cost=5 Card=54 Bytes=1404)
   6    5             SORT (UNIQUE) (Cost=5 Card=54 Bytes=1296)
   7    6               TABLE ACCESS (BY INDEX ROWID) OF 'IA_FEESTATNEXUS' (Cost=1 Card=54 Bytes=1296)
   8    7                 INDEX (RANGE SCAN) OF 'PK_IA_FEESTATNEXUS' (UNIQUE) (Cost=1 Card =54)
   9    4           VIEW (Cost=6 Card=1 Bytes=16)
  10    9             SORT (GROUP BY) (Cost=6 Card=1 Bytes=45)
  11   10               NESTED LOOPS (Cost=2 Card=1 Bytes=45)
  12   11                 TABLE ACCESS (BY INDEX ROWID) OF 'HIS_REPORT_ARG' (Cost=1 Card=1 Bytes=24)
  13   12                   INDEX (UNIQUE SCAN) OF 'PK_HIS_REPORT_ARG' (UNIQUE)
  14   11                 TABLE ACCESS (BY INDEX ROWID) OF 'IF_BALANCELIST' (Cost=1 Card=3047 Bytes=63987)
  15   14                   INDEX (RANGE SCAN) OF 'I_IF_BLANCELIST_BALANCE_DTIME' (NON-UNIQUE)
  16    3         HASH JOIN (OUTER) (Cost=11 Card=54 Bytes=2268)
  17   16           VIEW (Cost=5 Card=54 Bytes=1404)
  18   17             SORT (UNIQUE) (Cost=5 Card=54 Bytes=1296)
  19   18               TABLE ACCESS (BY INDEX ROWID) OF 'IA_FEESTATNEXUS' (Cost=1 Card=54 Bytes=1296)
  20   19                 INDEX (RANGE SCAN) OF 'PK_IA_FEESTATNEXUS' (UNIQUE) (Cost=1 Card =54)
  21   16           VIEW (Cost=6 Card=3 Bytes=48)
  22   21             SORT (GROUP BY) (Cost=6 Card=3 Bytes=117)
  23   22               NESTED LOOPS (Cost=2 Card=3 Bytes=117)
  24   23                 TABLE ACCESS (BY INDEX ROWID) OF 'HIS_REPORT_ARG' (Cost=1 Card=1 Bytes=24)
  25   24                   INDEX (UNIQUE SCAN) OF 'PK_HIS_REPORT_ARG' (UNIQUE)
  26   23                 TABLE ACCESS (BY INDEX ROWID) OF 'IF_BALANCELIST' (Cost=1 Card=20313 Bytes=304695)
  27   26                   INDEX (RANGE SCAN) OF 'I_IF_BALANCELIST_WASTE_DTIME' (NON-UNIQUE)

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         17  consistent gets
          0  physical reads
          0  redo size
       1715  bytes sent via SQL*Net to client
       1131  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
         16  sorts (memory)
          0  sorts (disk)
         23  rows processed

这样就能够充分利用IF_BALANCELIST建立的balance_dtime与waste_dtime索引。

时间: 2024-10-01 13:38:31

一条sql语句的优化的相关文章

sql server-求助:sqlserver一条sql语句的优化 是否需要索引 建立什么样的索引合适

问题描述 求助:sqlserver一条sql语句的优化 是否需要索引 建立什么样的索引合适 1C select COUNT(*) total from (select distinct(device_token) from MDM_POLICY_UPDATE where len(device_token)=64 and SW='crmi_poly') a 解决方案 我觉得直接可以用count(device_token) 然后直接groupby device个人感觉最拖后腿的应该是len()这个函

请教大神一条sql语句的优化

问题描述 请教大神一条sql语句的优化 表名pm,以temp分组,count两个信息,一个是全部个数,另一个是status为1 id temp status 1 1 0 2 1 1 3 2 0 4 2 1 5 2 1 select total.temp ,used.c1,total.c2 from (select temp,count(1) c1 from pm group by temp ) total left join (select temp,count(1) c2 from pm wh

[20150715]一条sql语句的优化.txt

[20150715]一条sql语句的优化.txt --生产系统发现一条语句. update presc_check t set t.diagnosis=replace(t.diagnosis,',慢性病drugs','') --第1眼看到的感觉真的很想骂人,什么能没有where条件呢? --我把这个表拷贝过来.这个表占用1G多1点,在测试环境执行看看: -- copy from system/xxxx@ip:1521/tyt create presc_check using select * f

[20151209]一条sql语句的优化(续).txt

[20151209]一条sql语句的优化(续).txt http://blog.itpub.net/267265/viewspace-1852195/ --上次提到其中1条sql语句: 1.环境: SYSTEM@192.168.99.105:1521/dbcn> @ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- -------------

[20140210]一条sql语句的优化(11g).txt

  [20140210]一条sql语句的优化(11g).txt 今天下午看生产系统数据库,无意中发现一个错误,同时优化也有点小问题,写一个测试脚本. 1.建立测试环境: SCOTT@test> @ver BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -

[20130319]一条sql语句的优化.txt

[20130319]一条sql语句的优化.txt 生产系统,遇到这样一条语句:SELECT MAX (LENGTH (pe_id)) FROM pe_master_index WHERE SUBSTR (pe_id, 1, 2) = 'TJ'; --真不知道开发人员如何想的,写出这样的语句.字段pe_id是主键.--数据库版本 SQL> select * from v$version where rownum BANNER                                     

[20131025]一条sql语句的优化.txt

[20131025]一条sql语句的优化.txt 最近在优化一条sql语句,做一个测试例子测试看看.遇到一些问题记录一下: 1.建立环境: SCOTT@test> @ver BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Productio

分享几条sql语句命令优化技巧

1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过 搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分 开的操作尽量分开处理,提高每次的响应速度:在数据窗口使用SQL时,尽量把使 用的索引放在选择的首列:算法的结构尽量简单:在查询时,不要过多地使用通配 符如SELECT * FROM T1语句,要用到几列就选择几列如:SELECT COL1,COL2 FROM T1:在可能的情况下尽量限制尽量结果集行数如:SELECT TOP 300 COL1,COL

[20120319]一条sql语句的优化.txt

前天检查数据库,发现一天sql语句执行如下:SELECT MAX (undrug_code)   FROM undrug_info  WHERE SUBSTR (undrug_code, 1, 1) = 'F'; undrug_code是表undrug_info的主键.开始看见这个语句,感觉这样写不好,我想像的执行计划,全扫描索引,然后sort aggregate,找到最大值. 不如这样写:SELECT MAX (undrug_code)   FROM undrug_info  WHERE un