[ORALCE] order by 있으면 없을 때 보다 buffers 가 적은 이유



    DBMS는 Array 단위로 데이터를 Fetch 합니다. 
    한 블록에 저장된 레코드 수가 Array Size보다 크면, 한 블록을 여러 번의 Fetch에 걸쳐 읽게 됩니다. 
    한 블록에 100개 레코드가 담겨 있는데 Array Size가 10이면, 10번의 Fetch Call로 나눠서 읽으므로 한 블록을 총 10번 읽습니다.
    Array Size가 100이면 1번의 Fetch Call로 읽으므로 한 블록을 1번만 읽습니다.

    SQL에 order by가 있으면, 첫 번째 Fetch Call에서 전체 데이터를 읽어서 Sort Area 및 Temp Tablespace에 담습니다
    이 과정에 같은 블록을 여러 번에 걸쳐 읽는 현상은 발생하지 않습니다. 
    모든 정렬 작업을 마친 후에 Sort Area 및 Temp Tablespace에 담긴 데이터를 Array Size 단위로 전송합니다. 이미 읽어서 가공한 데이터를 보내므로 추가적인 블록 I/O는 발생하지 않습니다. 

    order by를 위한 Sort 처리를 하면 블록 I/O가 더 적게 발생하는 이유입니다.

    SELECT * FROM sk.tab1 테스트

    
    --# SELECT * FROM sk.tab1 ;
    -- arraysize 15
    --------------------------------------------------------------------------------------------------------------------
    | Id  | Operation	  | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |	A-Time	 | Buffers |
    --------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |	 |	1 |	   |	   |   397 (100)|	   |  73194 |00:00:00.03 |    6209 |
    |   1 |  TABLE ACCESS FULL| TAB1 |	1 |  73194 |  9435K|   397   (1)| 00:00:01 |  73194 |00:00:00.03 |    6209 |
    --------------------------------------------------------------------------------------------------------------------
    
    -- 10046
    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch     4881      0.02       0.12          0       6209          0       73194
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total     4883      0.02       0.12          0       6209          0       73194
    
    Rows (1st) Rows (avg) Rows (max)  Row Source Operation
    ---------- ---------- ----------  ---------------------------------------------------
         73194      73194      73194  TABLE ACCESS FULL TAB1 (cr=6209 pr=0 pw=0 time=43887 us starts=1 cost=397 size=9661608 card=73194)
    
    
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      PGA memory operation                            1        0.00          0.00
      Disk file operations I/O                        1        0.00          0.00
      SQL*Net message to client                    4881        0.00          0.00
      SQL*Net message from client                  4881        2.82         28.98
    ********************************************************************************
    


    SELECT * FROM sk.tab1 ORDER BY 1 테스트

    
    --# SELECT * FROM sk.tab1 ORDER BY 1  ;
    --------------------------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation	   | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time   | A-Rows |	 A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
    --------------------------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |	  |	 1 |	    |	    |	    |  2556 (100)|	    |  73194 |00:00:00.08 |    1424 |	    |	    |	       |
    |   1 |  SORT ORDER BY	   |	  |	 1 |  73194 |  9435K|	 13M|  2556   (1)| 00:00:01 |  73194 |00:00:00.08 |    1424 |	 14M|  1431K|	12M (0)|
    |   2 |   TABLE ACCESS FULL| TAB1 |	 1 |  73194 |  9435K|	    |	397   (1)| 00:00:01 |  73194 |00:00:00.02 |    1424 |	    |	    |	       |
    --------------------------------------------------------------------------------------------------------------------------------------------------------
    
    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch     4881      0.06       0.18          0       1424          0       73194
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total     4883      0.06       0.18          0       1424          0       73194
    
    Rows (1st) Rows (avg) Rows (max)  Row Source Operation
    ---------- ---------- ----------  ---------------------------------------------------
         73194      73194      73194  SORT ORDER BY (cr=1424 pr=0 pw=0 time=84143 us starts=1 cost=2556 size=9661608 card=73194)
         73194      73194      73194   TABLE ACCESS FULL TAB1 (cr=1424 pr=0 pw=0 time=13664 us starts=1 cost=397 size=9661608 card=73194)
    
    
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      PGA memory operation                          155        0.00          0.00
      Disk file operations I/O                        1        0.00          0.00
      SQL*Net message to client                    4881        0.00          0.00
      SQL*Net message from client                  4881        0.06          5.99
    ********************************************************************************
    


    SELECT * FROM sk.tab1, arraysize 5000 테스트

    
    --# SELECT * FROM sk.tab1, arraysize 5000 테스트
    --------------------------------------------------------------------------------------------------------------------
    | Id  | Operation	  | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |	A-Time	 | Buffers |
    --------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |	 |	1 |	   |	   |   397 (100)|	   |  73194 |00:00:00.02 |    1438 |
    |   1 |  TABLE ACCESS FULL| TAB1 |	1 |  73194 |  9435K|   397   (1)| 00:00:01 |  73194 |00:00:00.02 |    1438 |
    --------------------------------------------------------------------------------------------------------------------
    
    -- 10046
    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch       16      0.03       0.13          0       1438          0       73194
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total       18      0.03       0.13          0       1438          0       73194
    
    Rows (1st) Rows (avg) Rows (max)  Row Source Operation
    ---------- ---------- ----------  ---------------------------------------------------
         73194      73194      73194  TABLE ACCESS FULL TAB1 (cr=1438 pr=0 pw=0 time=19795 us starts=1 cost=397 size=9661608 card=73194)
    
    
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      Disk file operations I/O                        1        0.00          0.00
      SQL*Net message to client                      16        0.00          0.00
      SQL*Net message from client                    16        1.53          7.42
      SQL*Net more data to client                   483        0.00          0.10
    ********************************************************************************
    

    댓글