Antes de dizer que uma ferramenta não é adequada a determinada tarefa, aprenda o máximo que puder sobre a mesma.
Os testes a seguir foram executados usando-se o snapshot da versão 1.8.1 de Groovy, que inclui a melhoria GROOVY-4798. Ao analisar os resultados, tenha em mente que:
- Microbenchmarks são apenas microbenchmarks;
- Os valores devem ser comparados entre si e não considerados em termos absolutos;
- Os respectivos servidores de diferentes RDMBS estavam em localidades geográficas distintas, inviabilizando a comparação entre valores que não pertençam ao mesmo grupo/RDBMS: Enquanto o servidor de PostgreSQL estava no mesmo computador em que os testes foram executados, o servidor de Oracle estava a dezenas de quilômetros de distância.
OS: x86 Windows XP 5.1
JVM: Sun Microsystems Inc. 1.6.0_23
=============================
== Testing oracle10g ==
=============================
== without batch (Statement)
1385 of 1385 rows inserted in 24375 (ms)
== without batch (PreparedStatement)
1385 of 1385 rows inserted in 22141 (ms)
== with batch (Statement)
1385 of 1385 rows inserted in 22312 (ms)
== with batch (PreparedStatement)
1385 of 1385 rows inserted in 140 (ms)
=============================
== Testing postgresql84 ==
=============================
== without batch (Statement)
1385 of 1385 rows inserted in 1156 (ms)
== without batch (PreparedStatement)
1385 of 1385 rows inserted in 1203 (ms)
== with batch (Statement)
1385 of 1385 rows inserted in 234 (ms)
== with batch (PreparedStatement)
1385 of 1385 rows inserted in 141 (ms)
=============================
== Testing hsqldb21 ==
=============================
== without batch (Statement)
1385 of 1385 rows inserted in 250 (ms)
== without batch (PreparedStatement)
1385 of 1385 rows inserted in 250 (ms)
== with batch (Statement)
1385 of 1385 rows inserted in 93 (ms)
== with batch (PreparedStatement)
1385 of 1385 rows inserted in 32 (ms)
As partes de destaque do código são:
def updateActions = [ 'without batch (Statement)': {sql, reader -> // (...) while ((r = reader.readNext()) != null && r.length > 0) { /* (...) */ sql.executeUpdate("insert into city(geo_world_map_id, code, short_code, latitude, longitude) values (" + "${r[0]}, '${code}', '${r[8]}', ${r[4]}, ${r[5]})") /* (...) */ } /* (...) */ }, 'without batch (PreparedStatement)': {sql, reader -> // (...) while ((r = reader.readNext()) != null && r.length > 0) { /* (...) */ sql.executeUpdate('insert into city(geo_world_map_id, code, short_code, latitude, longitude) values (' + '?, ?, ?, ?, ?)', r1) /* (...) */ } /* (...) */ }, 'with batch (Statement)': {sql, reader -> sql.withBatch(batchSize) {stmt -> // (...) while ((r = reader.readNext()) != null && r.length > 0) { /* (...) */ stmt.addBatch("insert into city(geo_world_map_id, code, short_code, latitude, longitude) values (" + "${r[0]}, '${code}', '${r[8]}', ${r[4]}, ${r[5]})") /* (...) */ } } }, 'with batch (PreparedStatement)': {sql, reader -> sql.withBatch(batchSize, 'insert into city(geo_world_map_id, code, short_code, latitude, longitude) values (' + '?, ?, ?, ?, ?)') {stmt -> // (...) while ((r = reader.readNext()) != null && r.length > 0) { /* (...) */ stmt.addBatch(r1) /* (...) */ } } } ]
Mais alguns resultados
OS: x86 Windows XP 5.1
JVM: Sun Microsystems Inc. 1.6.0_23
=============================
== Testing hsqldb21 ==
=============================
== without batch (Statement)
37230 of 37230 rows inserted in 2610 (ms)
== without batch (PreparedStatement)
37230 of 37230 rows inserted in 2750 (ms)
== with batch (Statement)
37230 of 37230 rows inserted in 2047 (ms)
== with batch (PreparedStatement)
37230 of 37230 rows inserted in 953 (ms)
=============================
== Testing postgresql84 ==
=============================
== without batch (Statement)
37230 of 37230 rows inserted in 26672 (ms)
== without batch (PreparedStatement)
37230 of 37230 rows inserted in 28500 (ms)
== with batch (Statement)
37230 of 37230 rows inserted in 6031 (ms)
== with batch (PreparedStatement)
37230 of 37230 rows inserted in 4406 (ms)
Projeto Groovy usado nos testes
Referências adicionais
2 Responses to Groovy SQL e batch updates (microbenchmark)