一次分頁SQL優化

開發說業務有個統計跑不出結果,我讓他把sql給我,他說不清楚,那我就直接才消耗時間最長了sql了,查找sql如下

SELECT inst_id,
       sql_id,
       sql_fulltext,
       round(exec_time / 1000000, 0) / 60 exec_time
  FROM (SELECT inst_id,
               sql_id,
               sql_fulltext,
               exec_time,
               rank() over(ORDER BY exec_time DESC) exec_rank
          FROM (SELECT sql_id,
                       sql_fulltext,
                       inst_id,
                       cpu_time,
                       elapsed_time,
                       executions,
                       round(elapsed_time / executions, 0) exec_time
                  FROM gv$sql
                 WHERE executions > 1))
 WHERE exec_rank <= 10;
 
因爲我對業務比較瞭解,因此直接就找出到了這個sql
 
 從sql_fulltext得到sql
select 
id,
agent_account,
operatorAccount,
aisleName,
interface_id,
customer_number,
serialnumber,
customer_serialnumber,
type,
money,
create_date,
status,
remark,
processed,
calls,
upNum,
area,
  reversal_type,
  brokerage_type,
  payment_type,
  refund_type,
  activities
from (
select 
temp.id,
temp.agent_account,
temp.operatorAccount,
      temp.aisleName,
      temp.interface_id,
      temp.customer_number,
      temp.serialnumber,
      temp.customer_serialnumber,
      temp.type,
      temp.money,
      temp.create_date,
      temp.status,
      temp.remark,
      temp.processed,
      temp.calls,
      temp.upNum,
      temp.area,
        temp.reversal_type,
        temp.brokerage_type,
        temp.payment_type,
        temp.refund_type,
        temp.activities,
      rownum row_id
    from (
    select 
      a.id,
      a.agent_account,
      b.login_name as operatorAccount,
      c.aislename as aisleName,
      a.interface_id,
      a.customer_number,
      a.serialnumber,
      a.customer_serialnumber,
      a.type,
      a.money,
      a.create_date,
      a.status,
      a.remark,
      a.processed,
      a.calls,
      a.upNum,
      a.area,
      a.reversal_type,
        a.brokerage_type,
        a.payment_type,
        a.refund_type,
        a.activities
    from 
      tb_recharge a left join tb_operator b on a.operator_id = b.id 
      left join tb_aisle c on a.aisle_id = c.id
     WHERE a.agent_id in (select id from tb_agent where sales_id =  :1)
      and a.status = :2
    order by a.id desc )temp
    where  rownum <= :3 ) where  row_id > :4

    

 

 查詢綁定變量的值


 select inst_id, value_string from gv$sql_bind_capture where sql_id = '0rhttycv0upqc' and inst_id = 1


 得到的值爲 103585 1 10 0

 

sql已經成功找出來了,是一個分頁語句,這種sql我們首先想到的是order by 列一定要有index,而且執行計劃應該走nestloop join 才比較快。


下面來看看執行計劃

 

Plan hash value: 3945838093
----------------------------------------------------------------------------------------------------------------------------
| Id  | Operation  | Name   | Rows  | Bytes | Cost (%CPU)| Time   | Pstart| Pstop |
----------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |   | 4 |  4052 | 92723   (1)| 00:18:33 |   |   |
|*  1 |  VIEW  |   | 4 |  4052 | 92723   (1)| 00:18:33 |   |   |
|*  2 |   COUNT STOPKEY   |   |   |   ||   |   |   |
|   3 |    VIEW   |   | 4 |  4000 | 92723   (1)| 00:18:33 |   |   |
|*  4 |     SORT ORDER BY STOPKEY  |   | 4 |   900 | 92723   (1)| 00:18:33 |   |   |
|   5 |      NESTED LOOPS OUTER   |   | 4 |   900 | 92722   (1)| 00:18:33 |   |   |
|*  6 |       HASH JOIN OUTER  |   | 4 |   832 | 92718   (1)| 00:18:33 |   |   |
|*  7 |        HASH JOIN  |   | 4 |   720 | 92715   (1)| 00:18:33 |   |   |
|*  8 | TABLE ACCESS FULL  | TB_AGENT   | 3 |30 |   206   (0)| 00:00:03 |   |   |
|   9 | PARTITION RANGE ALL  |   |  1194 |   198K| 92509   (1)| 00:18:31 | 1 |19 |
|  10 |  TABLE ACCESS BY LOCAL INDEX ROWID| TB_RECHARGE    |  1194 |   198K| 92509   (1)| 00:18:31 | 1 |19 |
|* 11 |   INDEX SKIP SCAN  | TB_RECHARGE_I3 |  1194 |   | 91316   (1)| 00:18:16 | 1 |19 |
|  12 |        TABLE ACCESS FULL  | TB_AISLE   |16 |   448 | 3   (0)| 00:00:01 |   |   |
|  13 |       TABLE ACCESS BY INDEX ROWID  | TB_OPERATOR    | 1 |17 | 1   (0)| 00:00:01 |   |   |
|* 14 |        INDEX UNIQUE SCAN  | TB_OPERATOR_PK | 1 |   | 0   (0)| 00:00:01 |   |   |
----------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("ROW_ID">0)
   2 - filter(ROWNUM<=10)
   4 - filter(ROWNUM<=10)
   6 - access("A"."AISLE_ID"="C"."ID"(+))
   7 - access("A"."AGENT_ID"="ID")
   8 - filter("SALES_ID"=103585)
  11 - access("A"."STATUS"=1)
       filter("A"."STATUS"=1)
  14 - access("A"."OPERATOR_ID"="B"."ID"(+))

 

分析:執行計劃id 爲 7 8  9三行,8和9 hashjoin後返回4行數據,這個顯然不準,至少也應該有1000多行(我查了下實際數據有70萬行),而且id爲9 使用index skip scan,

然後hash join 的結果集作爲驅動表與TB_OPERATOR nestloops join 這尼瑪能出結果纔怪,解決思路如下


先看tb_recharge的索引,status在TB_RECHARGE_I3組合索引的第五個字段

SELECT index_name, column_name, table_name, COLUMN_POSITION
  FROM user_ind_columns
 WHERE table_name = 'TB_RECHARGE'
 
INDEX_NAME       COLUMN_NAME TABLE_NAMECOLUMN_POSITION
------------------------------ ------------------------- ------------------------------ ---------------
TB_RECHARGEBK_PK       ID TB_RECHARGE      1
TB_RECHARGE_UNIQUE2       CUSTOMER_SERIALNUMBER TB_RECHARGE      2
TB_RECHARGE_UNIQUE2       AGENT_ACCOUNT TB_RECHARGE      1
TB_RECHARGE_I3       CUSTOMER_NUMBER TB_RECHARGE      1
TB_RECHARGE_I3       CREATE_DATE TB_RECHARGE      2
TB_RECHARGE_I3       AGENT_ACCOUNT TB_RECHARGE      3
TB_RECHARGE_I3       MONEY TB_RECHARGE      4
TB_RECHARGE_I3       STATUS TB_RECHARGE      5
TB_RECHARGE_I5       CREATE_DATE TB_RECHARGE      1
TB_RECHARGE_UNIQUE1       SERIALNUMBER TB_RECHARGE      1


sql中tb_recharge用到了status 和id字段因此我創建了這個索引

create index idx_tb_recharge_status_id(status,id)


創建索引後執行計劃如下

SQL>  select  *from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3852339816
---------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation  | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     4 |  4052 |  2037(1)| 00:00:25 |       |       |
|*  1 |  VIEW  |      |     4 |  4052 |  2037(1)| 00:00:25 |       |       |
|*  2 |   COUNT STOPKEY   |      |       |       |    |      |       |       |
|   3 |    VIEW   |      |     4 |  4000 |  2037(1)| 00:00:25 |       |       |
|*  4 |     SORT ORDER BY STOPKEY  |      |     4 |   900 |  2037(1)| 00:00:25 |       |       |
|   5 |      NESTED LOOPS OUTER   |      |     4 |   900 |  2036(1)| 00:00:25 |       |       |
|*  6 |       HASH JOIN OUTER  |      |     4 |   832 |  2032(1)| 00:00:25 |       |       |
|*  7 |        HASH JOIN  |      |     4 |   720 |  2029(1)| 00:00:25 |       |       |
|*  8 | TABLE ACCESS FULL  | TB_AGENT      |     3 |    30 |   206(0)| 00:00:03 |       |       |
|   9 | TABLE ACCESS BY GLOBAL INDEX ROWID| TB_RECHARGE       |  1194 |   198K|  1822(0)| 00:00:22 | ROWID | ROWID |
|* 10 |  INDEX RANGE SCAN  | IDX_TB_RECHARGE_STATUS_ID |  1194 |       |    13(0)| 00:00:01 |       |       |
|  11 |        TABLE ACCESS FULL  | TB_AISLE      |    16 |   448 |     3(0)| 00:00:01 |       |       |
|  12 |       TABLE ACCESS BY INDEX ROWID  | TB_OPERATOR       |     1 |    17 |     1(0)| 00:00:01 |       |       |
|* 13 |        INDEX UNIQUE SCAN  | TB_OPERATOR_PK      |     1 |       |     0(0)| 00:00:01 |       |       |
---------------------------------------------------------------------------------------------------------------------------------------



上面這個sql跑10s出結果了,其實這裏還是沒有走分頁的特性,tb_agent.id 是pk,因此可以直接把in改成join,最後又加了hint然其走nl,最後結果如下

SELECT id,
       status,
       agent_account,
       operatorAccount,
       aisleName,
       interface_id,
       customer_number,
       serialnumber,
       customer_serialnumber,
       TYPE,
       money,
       create_date,
       remark,
       processed,
       calls,
       upNum,
       area,
       reversal_type,
       brokerage_type,
       payment_type,
       refund_type,
       activities
  FROM (SELECT temp.id,
               temp.agent_account,
               temp.operatorAccount,
               temp.aisleName,
               temp.interface_id,
               temp.customer_number,
               temp.serialnumber,
               temp.customer_serialnumber,
               temp.type,
               temp.money,
               temp.create_date,
               temp.status,
               temp.remark,
               temp.processed,
               temp.calls,
               temp.upNum,
               temp.area,
               temp.reversal_type,
               temp.brokerage_type,
               temp.payment_type,
               temp.refund_type,
               temp.activities,
               rownum row_id
          FROM (SELECT
                /*+ use_nl(a,ta) use_nl(a,b) use_nl(a,c) leading(a,b,c) */
                 a.id,
                 a.agent_account,
                 b.login_name            AS operatorAccount,
                 c.aislename             AS aisleName,
                 a.interface_id,
                 a.customer_number,
                 a.serialnumber,
                 a.customer_serialnumber,
                 a.type,
                 a.money,
                 a.create_date,
                 a.status,
                 a.remark,
                 a.processed,
                 a.calls,
                 a.upNum,
                 a.area,
                 a.reversal_type,
                 a.brokerage_type,
                 a.payment_type,
                 a.refund_type,
                 a.activities
                  FROM (SELECT id FROM tb_agent WHERE sales_id = 103585) ta
                  INNER JOIN tb_recharge a
                  ON ta.id = a.agent_id
                  LEFT JOIN tb_operator b
                    ON a.operator_id = b.id
                  LEFT JOIN tb_aisle c
                    ON a.aisle_id = c.id
                 WHERE a.status = 1
                 ORDER BY a.id DESC) temp
         WHERE rownum <= 10)
 WHERE row_id > 0
SQL>  select  *from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3946970243
---------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation  | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     4 |  4052 |  5405(1)| 00:01:05 |       |       |
|*  1 |  VIEW  |      |     4 |  4052 |  5405(1)| 00:01:05 |       |       |
|*  2 |   COUNT STOPKEY   |      |       |       |    |      |       |       |
|   3 |    VIEW   |      |     4 |  4000 |  5405(1)| 00:01:05 |       |       |
|   4 |     NESTED LOOPS  |      |     4 |   900 |  5405(1)| 00:01:05 |       |       |
|   5 |      NESTED LOOPS  |      |  1194 |   900 |  5405(1)| 00:01:05 |       |       |
|   6 |       NESTED LOOPS OUTER  |      |  1194 |   250K|  4211(1)| 00:00:51 |       |       |
|   7 |        NESTED LOOPS OUTER  |      |  1194 |   218K|  3017(1)| 00:00:37 |       |       |
|   8 | TABLE ACCESS BY GLOBAL INDEX ROWID| TB_RECHARGE       |  1194 |   198K|  1822(0)| 00:00:22 | ROWID | ROWID |
|*  9 |  INDEX RANGE SCAN DESCENDING  | IDX_TB_RECHARGE_STATUS_ID |  1194 |       |    13(0)| 00:00:01 |       |       |
|  10 | TABLE ACCESS BY INDEX ROWID  | TB_OPERATOR       |     1 |    17 |     1(0)| 00:00:01 |       |       |
|* 11 |  INDEX UNIQUE SCAN  | TB_OPERATOR_PK      |     1 |       |     0(0)| 00:00:01 |       |       |
|  12 |        TABLE ACCESS BY INDEX ROWID  | TB_AISLE      |     1 |    28 |     1(0)| 00:00:01 |       |       |
|* 13 | INDEX UNIQUE SCAN  | TB_AISLEV_PK      |     1 |       |     0(0)| 00:00:01 |       |       |
|* 14 |       INDEX UNIQUE SCAN   | TB_AGENT_PK       |     1 |       |     0(0)| 00:00:01 |       |       |
|* 15 |      TABLE ACCESS BY INDEX ROWID  | TB_AGENT      |     1 |    10 |     1(0)| 00:00:01 |       |       |
---------------------------------------------------------------------------------------------------------------------------------------

最後ms級別出結果了

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章