测试环境:
可惜的是硬盘是5400;
Mongodb启动
Mysql启动
测试代码:
public static void mysql() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sants", "sants", "123456");
String sql = "insert into demo(name,birth) values(?,?)";
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement(sql);
for (int n = 0; n < max; n++) {
pst.setString(1, "Name" + Math.random());
pst.setDate(2, new java.sql.Date(System.currentTimeMillis()));
pst.addBatch();
}
pst.executeBatch();
conn.commit();
conn.setAutoCommit(true);
System.out.println("Insert " + max + " record cost :" + ((System.currentTimeMillis() - start) / 1000) + " ; Insert Speed:" + (max / ((System.currentTimeMillis() - start) / 1000)));
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void mongodb() {
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("demo");
boolean auth = db.authenticate("root", "root".toCharArray());
System.out.println("Connect to database successfully");
System.out.println("Authentication: " + auth);
Set<String> tables = db.getCollectionNames();
for (String name : tables) {
System.out.println("Table : " + name);
}
DBCollection demo = db.getCollection("demo");
long start = System.currentTimeMillis();
for (int n = 0; n < max; n++) {
DBObject dbo = new BasicDBObject();
dbo.put("name", "Name" + Math.random());
dbo.put("date", new Date());
demo.insert(dbo);
}
System.out.println("Insert " + max + " record cost :" + ((System.currentTimeMillis() - start) / 1000) + " ; Insert Speed:" + (max / ((System.currentTimeMillis() - start) / 1000)));
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
测试输出结果:
Insert 100000 record cost :22 ; Insert Speed:4545
Connect to database successfully
Authentication: true
Table : demo
Table : system.indexes
Table : system.users
Insert 100000 record cost :12 ; Insert Speed:8333
当然这个比排除磁盘性能的影响。MySQL是采用默认的InnoDB模式,所以在存储上可能也会有点压力;