使用 JDBC 加速 postgresql 上的 sql 插入?
Speeding up sql inserts on postgresql with JDBC?
我有以下两种方法来检查数据库中是否存在匹配项,如果不存在则调用插入方法。我的程序必须经过数千行并且需要很长时间。我做错了吗?我能做些什么来显着加快速度?
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(*) FROM match where match_id = ?");
pst.setString(1, matchId);
rs = pst.executeQuery();
while (rs.next())
{
exists = rs.getBoolean(1);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
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 INTO 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();
}
finally
{
pst.close();
conn.close();
}
return exists;
}
您可以尝试使用 WHERE NOT EXISTS
更改您的 SQL 查询,该查询仅在该行不在数据库中时才插入要插入的行。
这个 post 似乎是相关的 - 我知道它适用于 MySQL 而不是 PostgreSQL 但原则应该是相同的。
MySQL Conditional Insert
您是否先调用 isMatchIdInDatabase
然后调用 insertMatchId
以获得许多记录?
可能重复:Efficient way to do batch INSERTS with JDBC
打开连接并查询单个记录是一项开销很大的操作。如果你这样做数千次,它会变得非常慢。您应该尝试重组您的查询,以便只使用一个 SELECT
。然后你可以收集你必须插入的记录并使用批量插入来完成。
我有以下两种方法来检查数据库中是否存在匹配项,如果不存在则调用插入方法。我的程序必须经过数千行并且需要很长时间。我做错了吗?我能做些什么来显着加快速度?
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(*) FROM match where match_id = ?");
pst.setString(1, matchId);
rs = pst.executeQuery();
while (rs.next())
{
exists = rs.getBoolean(1);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
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 INTO 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();
}
finally
{
pst.close();
conn.close();
}
return exists;
}
您可以尝试使用 WHERE NOT EXISTS
更改您的 SQL 查询,该查询仅在该行不在数据库中时才插入要插入的行。
这个 post 似乎是相关的 - 我知道它适用于 MySQL 而不是 PostgreSQL 但原则应该是相同的。
MySQL Conditional Insert
您是否先调用 isMatchIdInDatabase
然后调用 insertMatchId
以获得许多记录?
可能重复:Efficient way to do batch INSERTS with JDBC
打开连接并查询单个记录是一项开销很大的操作。如果你这样做数千次,它会变得非常慢。您应该尝试重组您的查询,以便只使用一个 SELECT
。然后你可以收集你必须插入的记录并使用批量插入来完成。