本文采用Wireshark 分析mysql IO,抓包分析是否批量插入,接下来在工作中尽量去尝试采用Wireshark解决一些问题,也算一种解决问题的新思路
1. 准备好mysql,自家的腾讯云服务器,最近自有基建由阿里云迁移到腾讯云了,阿里太贵了
create table user
(
id int auto_increment comment '自增id'
primary key,
nick_name varchar(66) null,
age int null comment '年龄',
user_name varchar(64) not null comment '用户名 保证唯一',
create_time datetime default CURRENT_TIMESTAMP null comment '创建时间'
)
charset = utf8;
2. 测试用力执行并抓包
@Test
public void testInsert() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://1.117.109.40:3306/test?useSSL=false";
Connection connection = DriverManager.getConnection(url, "root", "******");
PreparedStatement statement = connection.prepareStatement("insert into user (nick_name,age, sex, user_name) value (?, ?, ?, ?)");
for (int i = 0; i < 10; i++) {
statement.setString(1, "name#" + System.currentTimeMillis() + "#" + i);
statement.setInt(2, 23 + i);
statement.setInt(3, 2 + i);
statement.setString(4, "jackson");
statement.addBatch();
}
statement.executeBatch();
}
抓到的包: 应用显示过滤器 ip.src_host == 192.168.1.100 and mysql
好办了,说明这不是批量插入,那咋改呢
改了以后会变成一次插入了
String url = "jdbc:mysql://1.117.109.40:3306/test?useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true";