insert语句执行速度慢应该怎么解决
展开全部
通常大数据插入的话都不会一条一条插入,使用bulk insert等方法。 如果是程序限制的话,一批一批的提交 BEGIN TRANSACTION INSERT … INSERT … INSERT … COMMIT TRANSACTION 另外去掉索引,把日志设为简单都能提高一点效率
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Oracle 的主键 是 sequence 生成的吗?若是,以下是解决方法:
1,用Oracle 的returning 语句。
PreparedStatement sta = conn.prepareStatement("INSERT INTO LOGGING VALUES (TESTSEQ.NEXTVAL,SYSDATE)returning id into ? ");
sta.execute();
ResultSet rset = sta.getResultSet();
while(rset.next())
{
int id = rset.getInt(1);
}
如果是既有传入值,又有返回值的话,应该:
PreparedStatement sta = conn.prepareStatement("BEGIN INSERT INTO LOGGING VALUES
(TESTSEQ.NEXTVAL,? )returning id into ?; END; ");
sta = conn.prepareCall(sqlText);
sta .setString(1, srqID);
sta .registerOutParameter(2, Types.NUMERIC);
sta .execute();
return sta .getLong(2);
或者用oracle的 OraclePreparedStatement
String sql = "insert into test values(1,?) returning id into ?";
OraclePreparedStatement pstt = (OraclePreparedStatement) conn.prepareStatement(sql);
pstt.setString(1, "xxx");
pstt.registerReturnParameter(2, OracleTypes.INTEGER);
pstt.executeUpdate();
ResultSet rset = pstt.getReturnResultSet(); // rest is not null
while (rset.next()) {
int id = rset.getInt(1);
System.out.println("Insert returnning: " + id);
}
1,用Oracle 的returning 语句。
PreparedStatement sta = conn.prepareStatement("INSERT INTO LOGGING VALUES (TESTSEQ.NEXTVAL,SYSDATE)returning id into ? ");
sta.execute();
ResultSet rset = sta.getResultSet();
while(rset.next())
{
int id = rset.getInt(1);
}
如果是既有传入值,又有返回值的话,应该:
PreparedStatement sta = conn.prepareStatement("BEGIN INSERT INTO LOGGING VALUES
(TESTSEQ.NEXTVAL,? )returning id into ?; END; ");
sta = conn.prepareCall(sqlText);
sta .setString(1, srqID);
sta .registerOutParameter(2, Types.NUMERIC);
sta .execute();
return sta .getLong(2);
或者用oracle的 OraclePreparedStatement
String sql = "insert into test values(1,?) returning id into ?";
OraclePreparedStatement pstt = (OraclePreparedStatement) conn.prepareStatement(sql);
pstt.setString(1, "xxx");
pstt.registerReturnParameter(2, OracleTypes.INTEGER);
pstt.executeUpdate();
ResultSet rset = pstt.getReturnResultSet(); // rest is not null
while (rset.next()) {
int id = rset.getInt(1);
System.out.println("Insert returnning: " + id);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询