zoukankan      html  css  js  c++  java
  • 分页查询中绑定变量

    在分页查询时,oracle绑定变量的使用优化

    var a number
    var b number
    var c number
    exec :a:=0
    exec :b:=10
    exec :c:=0
    set serveroutput off;
    alter session set statistics_level=all;
    SELECT * FROM (SELECT T1_.*, rownum ROWNUM_ FROM (
    SELECT O.* FROM yyf.testa O
    WHERE 1=1
    AND O.CREATE_TIME >= to_date('20150101 12:00:00', 'yyyymmdd hh24:mi:ss')
    AND O.CREATE_TIME < to_date('20150723 23:59:59', 'yyyymmdd hh24:mi:ss')
    order by o.CREATE_TIME desc
    ) T1_ WHERE ROWNUM <= (:a +:b )) WHERE ROWNUM_ >= (:c +1);

    select * from table(dbms_xplan.display_cursor(null,null,'runstats_last'));

    对应的执行计划为:

    Plan hash value: 2754511479
    
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------------------------------
    | Id  | Operation                | Name        | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
    -----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT         |             |      1 |        |     10 |00:00:20.07 |     684K|    310K|
    |*  1 |  VIEW                    |             |      1 |   4133K|     10 |00:00:20.07 |     684K|    310K|
    |*  2 |   COUNT STOPKEY          |             |      1 |        |     10 |00:00:20.07 |     684K|    310K|
    |   3 |    VIEW                  |             |      1 |   4133K|     10 |00:00:20.07 |     684K|    310K|
    |*  4 |     SORT ORDER BY STOPKEY|             |      1 |   4133K|     10 |00:00:20.07 |     684K|    310K|
    |*  5 |      TABLE ACCESS FULL   | testa       |      1 |   4133K|   5351K|00:00:15.40 |     684K|    310K|
    -----------------------------------------------------------------------------------------------------------
    
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("ROWNUM_">=:C+1)
       2 - filter(ROWNUM<=:A+:B)
       4 - filter(ROWNUM<=:A+:B)
       5 - filter(("O"."CREATE_TIME">=TIMESTAMP' 2015-01-01 12:00:00' AND "O"."CREATE_TIME"<TIMESTAMP'
                  2015-07-23 23:59:59'))
    
    
    31 rows selected.

    从上面执行计划可以看出,虽然在create_time 是not null ,并且有索引,而且该索引的可选择度非常高。但是该sql依然走的全表扫描。

    接着对sql进行如下调整,去掉绑定变量的运算,即把条件由"WHERE ROWNUM <= (:a +:b )) WHERE ROWNUM_ >= (:c +1)" 改为 "WHERE ROWNUM <= (:b )) WHERE ROWNUM_ >= (:c) " ; 其执行计划为:

    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                       | Name                       | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
    ---------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                |                            |      1 |        |     10 |00:00:00.03 |      49 |      4 |
    |*  1 |  VIEW                           |                            |      1 |     10 |     10 |00:00:00.03 |      49 |      4 |
    |*  2 |   COUNT STOPKEY                 |                            |      1 |        |     10 |00:00:00.03 |      49 |      4 |
    |   3 |    VIEW                         |                            |      1 |     12 |     10 |00:00:00.03 |      49 |      4 |
    |   4 |     TABLE ACCESS BY INDEX ROWID | testa                      |      1 |   4133K|     10 |00:00:00.03 |      49 |      4 |
    |*  5 |      INDEX RANGE SCAN DESCENDING| IDX_testa_CREATETIME       |      1 |     12 |     10 |00:00:00.03 |      36 |      2 |
    ---------------------------------------------------------------------------------------------------------------------------------
    
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("ROWNUM_">=:C)
       2 - filter(ROWNUM<=:B)
       5 - access("O"."CREATE_TIME"<TIMESTAMP' 2015-07-23 23:59:59' AND "O"."CREATE_TIME">=TIMESTAMP' 2015-01-01 12:00:00')
    
    
    29 rows selected.
  • 相关阅读:
    HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)
    POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)
    HDU 1754 I Hate It(线段树之单点更新 区间最值查询)
    CSU 2056 a simple game (正反进行KMP)超级好题!!!
    HihoCoder
    HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)
    51 Nod 1107 斜率小于0的连线数量 (转换为归并求逆序数或者直接树状数组,超级详细题解!!!)
    POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
    C# DBHelper类 参考
    C#学习之接口
  • 原文地址:https://www.cnblogs.com/yiyuf/p/4680758.html
Copyright © 2011-2022 走看看