This presentation explains the importance of set up the transaction fetch size. by doing this the underlying database protocol can easily calculate the correct tcp window and avoid to waste bandwidth and time.
2. Fetch Size
É a quantidade esperada de resultados. quando informada,
muito o Oracle (e outros bancos!) envia as informações de
forma diferente.
JDBC esconde os detalhes ccomplexos do protocolo de
comunicação com o banco (TNS do oracle, por exemplo), mas
contempla os problemas comuns a todos eles.
3. Exemplo(by nulaya)
var b1 varchar2(4000);
select /*+ CHOOSE */
null
into :b1
from
dual
where
1e1 = 1e1
connect by
rownum <= 1e3;
4. Exemplo complementado (by nulaya)
set echo off define off scan off feedback off heading off;
set timing on termout on autotrace on;
set pagesize 0 linesize 1024 arraysize 1;
var b1 varchar2(4000);
select /*+ CHOOSE */
null
into :b1
from
dual
where
1e1 = 1e1
connect by
rownum <= 1e3;
5. Resultado
Decorrido: 00:00:04.12
Plano de Execução
----------------------------------------------------------
Plan hash value: 634278704
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | COUNT | | | | |
|* 2 | FILTER | | | | |
|* 3 | CONNECT BY WITHOUT FILTERING| | | | |
| 4 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------
6. Resultado (cont.)
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(1e1=1e1)
3 - filter(ROWNUM<=1e3)
Estatística
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
91309 bytes sent via SQL*Net to client
11334 bytes received via SQL*Net from client
1002 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1000 rows processed
7. Exemplo complementado 2 (by nulaya)
set echo off define off scan off feedback off heading off;
set timing on termout on autotrace on;
set pagesize 0 linesize 1024 arraysize 1000;
var b1 varchar2(4000);
select /*+ CHOOSE */
null
into :b1
from
dual
where
1e1 = 1e1
connect by
rownum <= 1e3;
8. Resultado 2
Decorrido: 00:00:03.81
Plano de Execução
----------------------------------------------------------
Plan hash value: 634278704
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | COUNT | | | | |
|* 2 | FILTER | | | | |
|* 3 | CONNECT BY WITHOUT FILTERING| | | | |
| 4 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------
9. Resultado 2 (cont.)
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(1e1=1e1)
3 - filter(ROWNUM<=1e3)
Estatística
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
5393 bytes sent via SQL*Net to client
345 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1000 rows processed
10. Roundtrip e Payload
(...) (...)
91309 bytes sent via SQL*Net to client 5393 bytes sent via SQL*Net to client
11334 bytes received via SQL*Net from client 345 bytes received via SQL*Net from client
1002 SQL*Net roundtrips to/from client 3 SQL*Net roundtrips to/from client
(...) (...)
The round-trip delay time (RTD) or round-trip time (RTT) is the length of time it
takes for a signal to be sent plus the length of time it takes for an acknowledgment
of that signal to be received. (wikipedia)
Payload ou carga. Refere-se à quantidade de dados
sendo trafegada.
Um payload baixo implica em mais pacotes para
transportar uma mesma quantidade de dados, implicando
em mais roundtrip.
É como transportar 100 litros de água em um balde
de 10 litros, mas carregar apenas 1 litro por vez.
11. Use o "fetch size" adequado!
Com JDBC é possível informar quantos resultados são
esperados. Não informar quantos resultados são esperados
faz o JDBC utilizar o padrão de 1 resultado.
Se vamos alimentar uma grid com tamaanho de exibição
padrão de 10 linhas:
(...)
PreparedStatement pst = con.prepareStatement(sql);
pst.setParameter(1,vo.getNome());
pst.setFetchSize(10);// @since 1.2
ResultSet rs = pst.executeQuery();
(...)
12. JPA/Hibernate tem também, :)
<property name="hibernate.jdbc.fetch_size">20</property>
ou
<property name="eclipselink.jdbc.fetch-size" value="20"/>