JDBC连接mysql 批处理,每秒插入10条左右,求问原因.谢老师们.

画江湖之不良人 2016-11-29 11:45:52
1.在看方立勋老师的视频,其JDBC 每秒插入1000条左右,而我的只有每秒10几条,想问一下原因? 是电脑配置的问题?还是其他?
下面贴出建表语句,代码,笔记本配置 处理器是 i3-3217u- 1.80HZ.. .谢谢.
create table testbatch(
id int primary key ,
name varchar(30)
)

代码: @Test
public void test2() {
Connection conn = null;
PreparedStatement sta = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into testbatch (id,name) values (?,?)";
sta = conn.prepareStatement(sql);
for (int i = 3; i < 1000008; i++) {
sta.setInt(1, i);
sta.setString(2, "aa" + i);
sta.addBatch();
if (i % 1000 == 0) {
sta.executeBatch();
sta.clearBatch();
}
}
sta.executeBatch();
sta.clearBatch();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(rs, sta, conn);
}

}


<!----------------------------------,mysql init 文件:--------------------------------------------->
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]

[mysql]

default-character-set = utf8
[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir =D:\Program Files\mysql5716
datadir = D:\Program Files\mysql5716\data

character-set-server = utf8
collation-server = utf8_general_ci
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'



# server_id = .....


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
...全文
157 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
魔都 2016-11-30
  • 打赏
  • 举报
回复
插入数据库的时候,老师是批量插入,你是一次一个吧. 打开关闭数据库连接特别影响性能的
brooklyng60 2016-11-30
  • 打赏
  • 举报
回复
插入数据库的时候,老师是批量插入,你是一次一个。默认是一次一个,要显式的改成连接不提交,一次提交 conn = getConnection(); conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql); int[] result = pstmt.executeBatch(); conn.commit(); return result;
爱睡觉的阿狸 2016-11-29
  • 打赏
  • 举报
回复
你的电脑配置确实太low。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧