本条语句如何优化

[复制链接]
查看11 | 回复2 | 2005-10-30 17:05:33 | 显示全部楼层 |阅读模式
mysql5+linux(centos)
PreparedStatement prepared = null;



Connection conn = null;

ResultSet result = null;

try {

conn = DBUtil.getConnection();

conn.setAutoCommit(false);



prepared = conn.prepareStatement(INSERT_TOKEN_SQL);



int size = tokenLst.size();



for(Token t: tokenLst){

Long disciplinedId = t.getDisciplineId() == null? 0L: t.getDisciplineId();

Integer typeId = t.getTypeId() == null? 0:t.getTypeId();

Long participantId = t.getParticipantId() == null? 0:t.getParticipantId();



if(t.getText().length() >= 100){

System.out.println(t.getText());

continue;

}



prepared.setInt(1, storyId);

prepared.setLong(2, disciplinedId);

prepared.setInt(3, typeId);

prepared.setString(4, t.getText());

prepared.setBoolean(5, t.getIsReservedKeyWord());

prepared.setDouble(6, t.getFrequency());

prepared.setString(7, t.getHashCode()+"");

prepared.setBoolean(8, t.isInHeadLine());

prepared.setBoolean(9, t.getIsReservedKeyWord());

prepared.setLong(10, participantId);

prepared.setString(11, t.getStandardName());

prepared.setBoolean(12, t.isTearmParticipant());

prepared.setBoolean(13, t.isHomeOrAway());

prepared.addBatch();

}

prepared.executeBatch();



conn.commit();

} catch (Exception e) {

e.printStackTrace();

} finally {

DBUtil.JDBCUtil(conn, prepared, result);

}
本表有130万记录 ,现在发现批处理插入1万条记录发现需要 3-5秒。
经过观察该表有主键(id,storyId)其中id是系统自动增长。
问如何调节性能, 可以动索引
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
我不懂写的代码,若是批处理,你可以考虑写成INSERT INTO tablename VALUES(),()....方式,也许是1000条记录一次,然后commit下(Innodb引擎的话),试试是否快点
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
right, Innodb引擎
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行