Mysql5 与 Mongodb2.4 简单性能对比


测试环境:



可惜的是硬盘是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模式,所以在存储上可能也会有点压力;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值