简体   繁体   中英

Get fast resultset with mysql jdbc

I have a lineitem table with 6001215 rows of data and I wanted to get the result of this query SELECT * FROM LINEITEM quickly, to then treat the data from the resultset in memory. I have this code, but it's still very slow anyway.

           String Query = "SELECT * FROM LINEITEM";
           Properties p = new Properties();
           p.setProperty("user", "root");
           p.setProperty("password", "root");
           p.setProperty("MaxPooledStatements", "10000");
           p.setProperty("cachePrepStmts", "true");
           p.setProperty("useServerPrepStmts", "true");
           Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/tpch",p);
           conn.setAutoCommit(false);
           PreparedStatement preparedStatement = conn.prepareStatement(Query);
           resultSet = preparedStatement.executeQuery(Query);

       } catch (Exception e) {
           System.err.println("Got an exception! ");
           System.err.println(e.getMessage());
       }

Please try to make the database connection before the query run, making the connection in a method can reduce the performance when you will call that method again and again it will create db connection each time and use preparedStatement.executeQuery(); instead of preparedStatement.executeQuery(Query);

Use a POJO class implementation in order to save the Resulset fetched from the database table into a list of entities and then retrieve the column values whenever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM