快速通道
测试内容
测试MySQL数据库字段采用默认方式存储值和非默认方式(通过MyBatis插入数据)的效率比较。
表结构
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,
`address` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT 0,
`sex` int(1) NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 252001 CHARACTER SET = gbk COLLATE = gbk_chinese_ci ROW_FORMAT = Compact;
采用id自增的方式,防止插入过快造成的主键冲突。
测试代码
@Test
public void testInsert() {
long utc1 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++){
Student student = new Student("姓名","地址",i,0);
customerDao.insertCustomer(student);
}
long utc2 = System.currentTimeMillis();
System.out.println("==>"+(utc2-utc1));
}
采用时间戳的方式来判断运行时间的差别
POJO
public class Student {
private Integer id;
private String name;
private String address;
private Integer age;
private Integer sex;
public Student() {
}
public Student(String name, String address, Integer age, Integer sex) {
this.name = name;
this.address = address;
this.age = age;
this.sex = sex;
}
}
对比【1】(除主键外所有字段均自定义插入)
插入100条数据(1713ms)
插入1000条数据(3964ms)
插入10000条数据(19153ms)
插入50000条数据(84008ms)
插入100000条数据(277664ms)
对比【2】('age’和’sex’字段使用数据库默认的方式)
插入100条数据(1731ms)
插入1000条数据(3768ms)
插入10000条数据(19713ms)
插入50000条数据(86576ms)
插入100000条数据(298735ms)
结果
数量/条 | 所用时间/ms(对比【1】)(对比【2】) |
---|---|
100 | 1713 <==> 1731 |
1000 | 3964<==> 3768 |
10000 | 19153<==> 19713 |
50000 | 84008<==> 86576 |
100000 | 277664<==> 298735 |
测试结果可能有误,有较多的不确定因素,建议亲自测试,以求达到数据更加真实。