I'm performing a simple 'select' query in a Java loop as what is shown below. The size of the List can grow up to 10000+. How can I improve the query speed? Any example or advice is appreciated. Thanks.
Do note that I need to retrieve all data in every column of that table, so that's why the asterisk (*) is used.
List<String> valueList = .... Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); conn = DriverManager.getConnection(dbURL, dbUsername, dbPassword); for (int m = 0; m < valueList.size() ; m++) { String sql = "SELECT * FROM WORKSHEET WHERE " + sheetId + " = '" + valueList.get(m) + "'"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); // retreive data.... } } Edit: At the end, there are a few ways to speed this query up. I'm using the second way as it prevent ORA-04031 error in future.
- Use parameterized 'SELECT' query with 'IN' clause.
- Create a Nested table and cast array/list of items that comes from JDBC to the created Nested table.
- Create a temporary table and insert the list of items. Then perform a JOIN to the main table (1 query) and get the results.
WHERE pair1 or pair2 or pair3 etc...) would reduce some of the overhead.