From: Luis Vilar Flores <lflores@evolute.pt>
To: pgsql-jdbc@postgresql.org
Subject: Re: outOfMemoryError
Date: Tue, 29 Aug 2006 23:02:34 +0100
Message-ID: <0D2330AE-008F-47DD-858F-491CB18652F1@evolute.pt> (raw)
In-Reply-To: <DE16CA8B-FB61-429B-92B3-D859DA009E3D@fastcrypt.com>
References: <ed0ra2$5ct$1@news.hub.org>
<6E13F8E6-8D37-4D2D-833D-AFFD87BF22B8@fastcrypt.com>
<ed1evf$1lqb$1@news.hub.org>
<DE16CA8B-FB61-429B-92B3-D859DA009E3D@fastcrypt.com>
I wrote a program for moving full databases between different db
engines, the main loop for inserting is:
The main difference is that I use batch insert on prepared statements.
public class Move
{
public static final int MAX_BATCH_ROWS = 4096;
public static move( Connection CON_SRC, Connection CON_DEST, String
table )
throws Exception
{
Statement stm = CON_SRC.createStatement( ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY );
ResultSet rs = statement.executeQuery( "SELECT * FROM " + table );
StringBuffer buff = new StringBuffer( "INSERT INTO " );
StringBuffer args = new StringBuffer();
buff.append( table );
buff.append( " ( " );
ResultSetMetaData rsmd = rs.getMetaData();
for( int j = 1; j <= rsmd.getColumnCount(); ++j )
{
if( j != 1 )
{
buff.append( ", " );
args.append( ", " );
}
buff.append( rsmd.getColumnName( j ) );
args.append( "?" );
}
buff.append( " ) VALUES ( " );
buff.append( args );
buff.append( " )" );
String insert = buff.toString();
PreparedStatement pstm = CON_DEST.prepareStatement( insert );
System.out.println( "I: " + i + " " + TABLES[ i ] );
int typesCache[] = new int[ rsmd.getColumnCount() + 1 ];
for( int j = 1; j <= rsmd.getColumnCount(); ++j )
{
typesCache[ j ] = rsmd.getColumnType( j );
}
int rows = 0;
while( rs.next() )
{
for( int j = 1; j < typesCache.length; ++j )
{
Object data = rs.getObject( j );
pstm.setObject( j, rs.getObject( j ), typesCache[ j ] );
}
++rows;
if( ( rows % MAX_BATCH_ROWS ) == 0 )
{
pstm.executeBatch();
}
}
rs.close();
pstm.close();
}
}
Please have attention, this code was copy/paste from 2 classes (my
program is multi-threaded for performance - async read/write and
multiple tables at once)
This method works on at least 5 million rows.
Luis Flores
Analista de Sistemas
Evolute - Consultoria Informática
Email: lflores@evolute.pt
Tel: (+351) 212949689
AVISO DE CONFIDENCIALIDADE
Esta mensagem de correio electrónico e eventuais ficheiros anexos são
confidenciais e destinados apenas à(s) pessoa(s) ou entidade(s) acima
referida(s), podendo conter informação privilegiada e confidencial, a
qual não poderá ser divulgada, copiada, gravada ou distribuída nos
termos da lei vigente. Caso não seja o destinatário da mensagem, ou
se ela lhe foi enviada por engano, agradecemos que não faça uso ou
divulgação da mesma. A distribuição ou utilização da informação nela
contida é interdita. Se recebeu esta mensagem por engano, por favor
notifique o remetente e apague este e-mail do seu sistema. Obrigado.
CONFIDENTIALITY NOTICE
This e-mail transmission and eventual attached files are intended
only for the use of the individual(s) or entity(ies) named above and
may contain information that is both privileged and confidential and
is exempt from disclosure under applicable law. If you are not the
intended recipient, you are hereby notified that any disclosure,
copying, distribution or use of any of the information contained in
this transmission is strictly restricted. If by any means you have
received this transmission in error, please immediately notify the
sender and delete this e-mail from your system. Thank you.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: lflores@evolute.pt
Subject: Re: outOfMemoryError
In-Reply-To: <0D2330AE-008F-47DD-858F-491CB18652F1@evolute.pt>
References: <ed0ra2$5ct$1@news.hub.org> <6E13F8E6-8D37-4D2D-833D-AFFD87BF22B8@fastcrypt.com> <ed1evf$1lqb$1@news.hub.org> <DE16CA8B-FB61-429B-92B3-D859DA009E3D@fastcrypt.com> <0D2330AE-008F-47DD-858F-491CB18652F1@evolute.pt>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox