Miniseminario fetch size

16
Fetch Size Ganhe desempenho, pergunte-me como

Transcript of Miniseminario fetch size

Page 1: Miniseminario fetch size

Fetch Size

Ganhe desempenho, pergunte-me como

Page 2: Miniseminario fetch size

Fetch Size

É a quantidade esperada de resultados. Quando informada, muitos SGBD's enviam as informações de forma diferente.

JDBC esconde os detalhes complexos do protocolo de comunicação com o banco (TNS do oracle, por exemplo), mas contempla os problemas comuns a todos eles.

Page 3: Miniseminario fetch size

Exemplo(by nulaya)

var b1 varchar2(4000); select /*+ CHOOSE */

null into :b1 from dual where 1e1 = 1e1connect by rownum <= 1e3;

Page 4: Miniseminario fetch size

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 = 1e1connect by rownum <= 1e3;

Page 5: Miniseminario fetch size

Resultado

Decorrido: 00:00:04.12Plano 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 |-------------------------------------------------------------------------------

Page 6: Miniseminario fetch size

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

Page 7: Miniseminario fetch size

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 = 1e1connect by rownum <= 1e3;

Page 8: Miniseminario fetch size

Resultado 2

Decorrido: 00:00:03.81Plano 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 |-------------------------------------------------------------------------------

Page 9: Miniseminario fetch size

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

Page 10: Miniseminario fetch size

Roundtrip e Payload

(...) 91309 bytes sent via SQL*Net to client 11334 bytes received via SQL*Net from client 1002 SQL*Net roundtrips to/from client(...)

(...)5393 bytes sent via SQL*Net to client 345 bytes received via SQL*Net 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 paratransportar 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.

Page 11: Miniseminario fetch size

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(); (...)

Page 12: Miniseminario fetch size

JPA/Hibernate tem também, :)

<property name="hibernate.jdbc.fetch_size">20</property> ou <property name="eclipselink.jdbc.fetch-size" value="20"/>

Page 13: Miniseminario fetch size

Observações

● Quando não informamos o fetch o banco de dados e o driver jdbc negociam (e nem sempre acertam) uma quantidade esperada de “linhas a retornar”.

● Experimentalmente, observou-se que o valor ideal de fetch deve ser um pouco superior à média retornada pelas consultas da aplicação.

Page 14: Miniseminario fetch size

?

Page 15: Miniseminario fetch size

FIM

Page 16: Miniseminario fetch size

Referências

● http://en.wikipedia.org/w/index.php?title=Round-trip_delay_time&oldid=408460586● http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html#setFetchSize(int)● http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Fetch_Size● http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-jdbc-properties