java - Speeding up sql inserts on postgresql with JDBC? -
i have 2 methods below checking if match in database , if not if call insert method. program has go through thousands of rows , takes long time. doing incorrectly? can make faster?
public boolean ismatchidindatabase(string matchid) throws sqlexception { connection conn = null; preparedstatement pst = null; resultset rs = null; boolean exists = false; try { class.forname("org.postgresql.driver"); conn = drivermanager.getconnection(url, props); pst = conn.preparestatement("select count(*) match match_id = ?"); pst.setstring(1, matchid); rs = pst.executequery(); while (rs.next()) { exists = rs.getboolean(1); } } catch (exception e) { e.printstacktrace(); } { pst.close(); rs.close(); conn.close(); } return exists; } public boolean insertmatchid(string matchid, string name, timestamp birthdate, string bio, string accountid) throws sqlexception, classnotfoundexception { connection conn = null; preparedstatement pst = null; boolean exists = false; try { class.forname("org.postgresql.driver"); conn = drivermanager.getconnection(url, props); pst = conn.preparestatement("insert match (match_id, name, birthdate, bio, account_id) values(?, ? , ?, ?, ?)"); pst.setstring(1, matchid); pst.setstring(2, name); pst.settimestamp(3, birthdate); pst.setstring(4, bio); pst.setstring(5, accountid); pst.executeupdate(); } { pst.close(); conn.close(); } return exists; }
are calling first ismatchidindatabase
insertmatchid
many records? possible duplicate: efficient way batch inserts jdbc
it expensive operation open connection , query single record. if thousands of times, gets slow. should try restructure query use 1 select
. can collect records have insert , doing batch insert.
Comments
Post a Comment