Very slow Java SE application with remote Mysql BD, how to optimize?

Asked

Viewed 109 times

0

Hello, people. I am developing a desktop application with Java SE and testing the software with a remote database it gets very slow, while with the local database it works normally. The following is an example method of how I am programming CRUD (this method also takes the longest to return the response when the connection is remote):

public List<EscolaTecnicaModel> pesquisar(EscolaTecnicaModel escolaTecnicaModel) throws Exception {
    Connection conexao = Conexao.abrirConexao();
    List<EscolaTecnicaModel> escolas = new ArrayList<>();
    String sqlSelect = "SELECT " +
                           "et.*, ad.usuario, ad.senha, c.nome AS nome_cidade, c.id_estado, e.nome AS nome_estado, e.codigo_uf " +
                       "FROM escolas_tecnicas et " +
                           "INNER JOIN administradores ad ON (et.id_administrador = ad.id_administrador) " +
                           "INNER JOIN cidades c ON (et.id_cidade = c.id_cidade) " +
                           "INNER JOIN estados e ON (c.id_estado = e.id_estado) "; 
    String sqlWhere = "";
    String sqlOrder = "ORDER BY et.nome ASC"; 

    if (!escolaTecnicaModel.getNome().trim().equals("")) {
        sqlWhere += "WHERE et.nome LIKE '%" + escolaTecnicaModel.getNome() + "%' "; 
    }       

    PreparedStatement psBuscarEscolas = conexao.prepareStatement(sqlSelect + sqlWhere + sqlOrder);
    ResultSet rsBuscaEscolas = psBuscarEscolas.executeQuery();

    if (rsBuscaEscolas.next()) {
        do {
            EscolaTecnicaModel escolaTecnica = new EscolaTecnicaModel();
            escolaTecnica.setIdEscolaTecnica(rsBuscaEscolas.getInt("id_escola"));
            escolaTecnica.setNome(rsBuscaEscolas.getString("nome"));
            escolaTecnica.setEmail(rsBuscaEscolas.getString("email"));
            escolaTecnica.setIdAdministrador(rsBuscaEscolas.getInt("id_administrador"));
            escolaTecnica.setUsuario(rsBuscaEscolas.getString("usuario"));
            escolaTecnica.setSenha(rsBuscaEscolas.getString("senha"));
            escolaTecnica.setNivel(AdministradorModel.Nivel.ESCOLA);
            escolaTecnica.getCidade().setIdCidade(rsBuscaEscolas.getInt("id_cidade"));
            escolaTecnica.getCidade().setNome(rsBuscaEscolas.getString("nome_cidade"));
            escolaTecnica.getCidade().getEstado().setIdEstado(rsBuscaEscolas.getInt("id_estado"));
            escolaTecnica.getCidade().getEstado().setNome(rsBuscaEscolas.getString("nome_estado"));
            escolaTecnica.getCidade().getEstado().setCodigoUf(rsBuscaEscolas.getString("codigo_uf"));
            escolaTecnica.getEndereco().setRua(rsBuscaEscolas.getString("rua"));
            escolaTecnica.getEndereco().setNumero(rsBuscaEscolas.getString("numero"));
            escolaTecnica.getEndereco().setBairro(rsBuscaEscolas.getString("bairro"));
            escolaTecnica.getEndereco().setCep(rsBuscaEscolas.getString("cep") == null ? "" : rsBuscaEscolas.getString("cep"));

            PreparedStatement psBuscarTelefones = conexao.prepareStatement("SELECT * FROM telefones_escolas_tecnicas WHERE id_escola = ?");
            psBuscarTelefones.setInt(1, escolaTecnica.getIdEscolaTecnica());
            ResultSet rsBuscarTelefones = psBuscarTelefones.executeQuery();

            while (rsBuscarTelefones.next()) {
                TelefoneModel telefone = new TelefoneModel();
                telefone.setDdd(rsBuscarTelefones.getString("ddd"));
                telefone.setNumero(rsBuscarTelefones.getString("numero"));
                telefone.setTipo(rsBuscarTelefones.getString("tipo"));

                escolaTecnica.adicionarTelefone(telefone);
            }

            escolas.add(escolaTecnica);

            Conexao.fecharConexao(rsBuscarTelefones, psBuscarTelefones);
        } while (rsBuscaEscolas.next());
    } else {
        Conexao.fecharConexao(conexao, psBuscarEscolas, rsBuscaEscolas);

        throw new Exception("Nenhuma escola encontrada");
    }

    Conexao.fecharConexao(conexao, psBuscarEscolas, rsBuscaEscolas);

    return escolas;
}

What may be causing this slowness and how to solve?

  • I think the problem here and the fact that you’re making the connection in the same Thread where you’re processing the data, knowing that you have several loops built in maybe it would be a good idea to retrieve your Resultset and treat it separately asynchronously.

  • This method is just an example of how the connection is opening, manipulating and closing, but there are others with only one loop that is also taking a long time to return data (it takes less than the one in the example, obviously, but it takes a long time).

  • makes Indice for those querys

  • The problem was in calling these methods for displaying the data in the graphical interface. I had made some redundant calls inside loops, which turned into a snowball... The way the events are performed in Swing also hurt a little, but I’ve managed to improve the performance a lot. Thank you for your willingness to help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.