-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate insert performance
PostPosted: Wed Aug 25, 2004 3:10 pm 
Beginner
Beginner

Joined: Fri Apr 16, 2004 10:25 am
Posts: 44
Location: Brazil
I have to insert 750000 registers in the DB, but when I start inserting the data, it gets more and more slow, what can I do to optimize inserts in hibernate?

Hibernate version: 2.1.6

Mapping documents:
Code:
   <class name="com.br.resource.csu.lembranca.dao.hibernate.persisted.Cliente" table="SSC_CLIENTE">
      <id name="codCli" column="COD_CLI" type="integer">
         <generator class="increment"/>
      </id>
      <bag name="documentos" table="SSC_documento" inverse="true" lazy="true" cascade="all">
          <key column="COD_CLI"/>
          <one-to-many class="com.br.resource.csu.lembranca.dao.hibernate.persisted.Documento"/>
      </bag>
      <bag name="enderecos" table="SSC_endereco_cli" inverse="true" lazy="true" cascade="all">
          <key column="COD_CLI"/>
          <one-to-many class="com.br.resource.csu.lembranca.dao.hibernate.persisted.EnderecoCliente"/>
      </bag>
      <bag name="telefones" table="SSC_fone_cli" inverse="true" lazy="true" cascade="all">
          <key column="COD_CLI"/>
          <one-to-many class="com.br.resource.csu.lembranca.dao.hibernate.persisted.TelefoneCliente"/>
      </bag>
      <bag name="doctoPessoa" table="SSC_REL_CLI_DOCTO" inverse="true" lazy="true" cascade="all">
          <key column="COD_CLI"/>
          <one-to-many class="com.br.resource.csu.lembranca.dao.hibernate.persisted.DocumentoCliente"/>
      </bag>

      <bag name="campanhas" table="SSC_REL_CLIENTE_CAMPANHA" inverse="true" lazy="true" cascade="all">
          <key column="COD_CLI"/>
          <one-to-many class="com.br.resource.csu.lembranca.dao.hibernate.persisted.ClienteCampanha"/>
      </bag>
        <property name="codCliExt"       column="COD_CLI_EXT"/>
        <many-to-one name="codOrg"      column="COD_ORG"/>
        <property name="datInclusao"    column="DAT_INCLUSAO"/>
        <property name="dscEmail"       column="DSC_EMAIL"/>
        <property name="indSituaFornec" column="IND_SITUA_FORNEC"/>
        <property name="nomCli"       column="NOM_CLI"/>
        <property name="vlrSldDev"       column="VLR_SLD_DEV"/>


Code between sessionFactory.openSession() and session.close():
Code:
      try {
         Cliente cliente;
         cliente = clienteDao.findByCodigoClienteExt(fileDataTemp.getDadosCliente().getCodigoCliente());
         Organizacao org = orgDao.findByPrimaryKey(new Integer(dadosSessao.getOrganizacao()));
         if (cliente != null) {
            // soma ao saldo devedor
            cliente.setVlrSldDev(new Float(cliente.getVlrSldDev().floatValue() + fileDataTemp.getFaturaCliente().getValorFatura().floatValue()));
            cliente.setCodOrg(org);
//            clienteDao.update(cliente);
         } else {
            cliente = new Cliente();
            cliente.setCodOrg(org);
            cliente.setCodCliExt(fileDataTemp.getDadosCliente().getCodigoCliente());
            cliente.setNomCli(fileDataTemp.getDadosCliente().getNomeCliente());
            cliente.setVlrSldDev(fileDataTemp.getFaturaCliente().getValorFatura());
            cliente.setDatInclusao(new Date());
//            clienteDao.create(cliente);
         }
         
         Documento doc = new Documento();
         doc.setCodCli(cliente);
         doc.setVlrFatura(fileDataTemp.getFaturaCliente().getValorFatura());
         doc.setDatIniCob(fileDataTemp.getDadosCliente().getInicioCobranca());
         doc.setDatFimCob(fileDataTemp.getDadosCliente().getFimCobranca());
         doc.setFlgDebAut(fileDataTemp.getDadosCliente().getDebitoAutomatico());
         doc.setCodClasse(fileDataTemp.getDadosCliente().getClasseCliente());
         doc.setNumFatura(fileDataTemp.getFaturaCliente().getNumeroFatura());
         doc.setMesRefFatura(fileDataTemp.getFaturaCliente().getMesReferenciaFatura());
         doc.setDatVenctoFatura(fileDataTemp.getFaturaCliente().getDataVencimento());
         List documentos = new ArrayList();
         documentos.add(doc);
         cliente.setDocumentos(documentos);

//         DocumentoDao documentoDao = hibernateDaoFactory.getDocumentoDao();
//         documentoDao.create(doc);
         
         List telefones = new ArrayList();
         telefones.add(getTel(fileDataTemp.getDadosCliente().getTelefone1(), cliente));
         telefones.add(getTel(fileDataTemp.getDadosCliente().getTelefone2(), cliente));
         telefones.add(getTel(fileDataTemp.getDadosCliente().getTelefone3(), cliente));
         cliente.setTelefones(telefones);
         telefones = null;

         List enderecos = new ArrayList();
         EnderecoCliente endCliente = new EnderecoCliente();
         EnderecoClienteDao endClienteDao = hibernateDaoFactory.getEnderecoClienteDao();
         if (fileDataTemp.getDadosCliente().getEnderecoComercial() != null &&
               !fileDataTemp.getDadosCliente().getEnderecoComercial().trim().equals("")){
            endCliente.setCodCli(cliente);
            endCliente.setDscEnd(fileDataTemp.getDadosCliente().getEnderecoComercial());
            endCliente.setCodCep(fileDataTemp.getDadosCliente().getCepComercial());
            endCliente.setNomBairro(fileDataTemp.getDadosCliente().getBairroComercial());
            endCliente.setNomMunicipio(fileDataTemp.getDadosCliente().getMunicipioComercial());
            endCliente.setCodTipoEnd(EnderecoCliente.TIPO_COMERCIAL);

            enderecos.add(endCliente);
//            endClienteDao.create(endCliente);
         }
         if (fileDataTemp.getDadosCliente().getEnderecoEntrega() != null &&
               !fileDataTemp.getDadosCliente().getEnderecoEntrega().trim().equals("")){
            endCliente = new EnderecoCliente();
            endCliente.setCodCli(cliente);
            endCliente.setDscEnd(fileDataTemp.getDadosCliente().getEnderecoEntrega());
            endCliente.setCodCep(fileDataTemp.getDadosCliente().getCepEntrega());
            endCliente.setNomBairro(fileDataTemp.getDadosCliente().getBairroEntrega());
            endCliente.setNomMunicipio(fileDataTemp.getDadosCliente().getMunicipioEntrega());
            endCliente.setCodTipoEnd(EnderecoCliente.TIPO_RESIDENCIAL);

            enderecos.add(endCliente);
//            endClienteDao.create(endCliente);
         }
         
         cliente.setEnderecos(enderecos);

         List documentosCliente = new ArrayList();
         DocumentoClienteDao docClienteDao = hibernateDaoFactory.getDocumentoClienteDao();
         if (fileDataTemp.getDocumentoCliente() != null){
            Iterator it = fileDataTemp.getDocumentoCliente().iterator();
            while (it.hasNext()) {
               DocumentoClienteFase1TO docClienteFase1TO = (DocumentoClienteFase1TO) it.next();
               if (docClienteFase1TO.getTipoDocumento().trim().toUpperCase().equals(DocumentoCliente.TIPO_CPF) ||
                  docClienteFase1TO.getTipoDocumento().trim().toUpperCase().equals(DocumentoCliente.TIPO_CI) ||
                  docClienteFase1TO.getTipoDocumento().trim().toUpperCase().equals(DocumentoCliente.TIPO_CGC)) {

                  DocumentoCliente docCliente = docClienteDao.findByCodCliCodDoc(cliente.getCodCli() , docClienteFase1TO.getTipoDocumento().trim().toUpperCase());
                  if (docCliente == null) {
                     docCliente = new DocumentoCliente();
                     docCliente.setVlrDoc(docClienteFase1TO.getNumeroDocumento().trim());
                     docCliente.setCodDoc(docClienteFase1TO.getTipoDocumento().trim().toUpperCase());
                     docCliente.setCodCli(cliente);
                     
                     documentosCliente.add(docCliente);
//                     docClienteDao.create(docCliente);
                  }
               }
            }
            cliente.setDoctoPessoa(documentosCliente);
         }

         clienteDao.update(cliente);

      } catch (Exception e) {
         e.printStackTrace();
         throw new Fase1FileProcessException(e.getMessage());
      }
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 3:13 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ORM isn't a performant solution for massive delete, update, insert
JDBC or pl sql do this better.

If you really want to do this with hibernate, try to clear session every 100 inserts for example... just try and bench

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:13 pm 
Beginner
Beginner

Joined: Fri Apr 16, 2004 10:25 am
Posts: 44
Location: Brazil
I made a test

processing 23224 registers from file into DB with processing of file's rules

using flush() and evict() for every save() = 00:01:07,536

using session.clear() per 150 processes = 00:01:15,422


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 5:33 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Thats what JDBC is for. Or better, use the data import/export tools that come with your database.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 26, 2004 5:15 pm 
Beginner
Beginner

Joined: Fri Apr 16, 2004 10:25 am
Posts: 44
Location: Brazil
is it possible to do something like this or set hibernate to act as a batch preparedStatement

Code:
      try {
         PreparedStatement psLogRejeicao = conn.prepareStatement("insert into ssc_log_rejeicao (COD_ARQUIVO, COD_LOTE,DSC_REJEICAO,NUM_LINHA,INX_REJEICAO,DSC_MOTIVO)" +
         " values (?,?,?,?,?,?)");
         Statement st = conn.createStatement();

         for (int inx=0;inx<rejeicao.size();inx++){
               LogsRejeicao log = (LogsRejeicao)rejeicao.get(inx);
               
               psLogRejeicao.setInt(1,intArquivo.intValue());
               psLogRejeicao.setInt(2,intLote.intValue());
               psLogRejeicao.setString(3,log.linha);
               psLogRejeicao.setInt(4,log.numLinha);
               psLogRejeicao.setInt(5,idLogRejeicao);
               psLogRejeicao.setString(6,log.motivo);
               
               psLogRejeicao.addBatch();
            }
            // Processa o batch a cada 2000 itens
            if (inx%2000 == 0){
               psLogRejeicao.clearBatch();
            }
         }
         psLogRejeicao.clearBatch();
         psLogRejeicao.close();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 26, 2004 5:18 pm 
Beginner
Beginner

Joined: Fri Apr 16, 2004 10:25 am
Posts: 44
Location: Brazil
Code:
PreparedStatement psLogRejeicao = conn.prepareStatement("insert into ssc_log_rejeicao (COD_ARQUIVO, COD_LOTE,DSC_REJEICAO,NUM_LINHA,INX_REJEICAO,DSC_MOTIVO)" +
" values (?,?,?,?,?,?)");

for (int inx=0;inx<rejeicao.size();inx++){
   LogsRejeicao log = (LogsRejeicao)rejeicao.get(inx);

   psLogRejeicao.setInt(1,intArquivo.intValue());
   psLogRejeicao.setInt(2,intLote.intValue());
   psLogRejeicao.setString(3,log.linha);
   psLogRejeicao.setInt(4,log.numLinha);
   psLogRejeicao.setInt(5,idLogRejeicao);
   psLogRejeicao.setString(6,log.motivo);

   psLogRejeicao.addBatch();

   // Processa o batch a cada 2000 itens
   if (inx%2000 == 0){
      psLogRejeicao.executeBatch();
      psLogRejeicao.clearBatch();
   }
}
psLogRejeicao.executeBatch();
psLogRejeicao.clearBatch();
psLogRejeicao.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.