MongoDB vs MySQL,哪个更快?

3 篇文章 0 订阅
3 篇文章 0 订阅

一、MongoDB批量操作

MongoDB对数据的操作分为Read Operations和Write Operations,Read Operations包含查询操作,Write Operations包含删除、插入、替换、更新几种操作。MongoDB提供客户端用bulk方式执行Write Operations,也就是批量写操作。在java driver中,对应MongoCollection的bulkWrite()方法,先来看下这个方法签名:

BulkWriteResult&nbsp;&nbsp;com.mongodb.client.MongoCollection.bulkWrite(List<?&nbsp;extends&nbsp;WriteModel<?&nbsp;extends&nbsp;Document>>&nbsp;requests)

这个方法要求传入一个List集合,集合中的元素类型为WriteModel,它表示一个可用于批量写操作的基类模型,它有以下几个子类DeleteManyModel、DeleteOneModel、 InsertOneModel、ReplaceOneModel、 UpdateManyModel、UpdateOneModel,从名字可以看出来它对应了删除、插入、替换、更新几种操作。该方法返回一个BulkWriteResult对象,代表一个成功的批量写操作结果,封装了操作结果的状态信息,如插入、更新、删除记录数等。

1、插入操作

(1)、批量插入

代码如下,该方法接收一个包含要进行插入的Document对象的集合参数,遍历集合,使用Document构造InsertOneModel对象,每个InsertOneModel实例代表一个插入单个Document的操作,然后将该实例添加List集合中,调用bulkWrite()方法,传入存储所有插入操作的List集合完成批量插入。

public&nbsp;void&nbsp;bulkWriteInsert(List<Document>&nbsp;documents){
&nbsp;List<WriteModel<Document>>&nbsp;requests&nbsp;=&nbsp;new&nbsp;ArrayList<WriteModel<Document>>();
&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents)&nbsp;{
&nbsp;&nbsp;//构造插入单个文档的操作模型
&nbsp;&nbsp;InsertOneModel<Document>&nbsp;&nbsp;iom&nbsp;=&nbsp;new&nbsp;InsertOneModel<Document>(document);
&nbsp;&nbsp;requests.add(iom);
&nbsp;}
&nbsp;BulkWriteResult&nbsp;&nbsp;bulkWriteResult&nbsp;=&nbsp;collection.bulkWrite(requests);
&nbsp;System.out.println(bulkWriteResult.toString());
}

测试:下面通过一个main函数测试下。首先构造10万个Product实体对象,使用一个工具类将其转换成json字符串,然后解析成Document对象,保存到一个list集合中,然后调用上面编写的方法测试10万个对象插入时间。

TestMongoDB&nbsp;instance&nbsp;=&nbsp;TestMongoDB.getInstance();
ArrayList<Document>&nbsp;documents&nbsp;=&nbsp;new&nbsp;ArrayList<Document>();
for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;100000;&nbsp;i++)&nbsp;{
&nbsp;Product&nbsp;product&nbsp;=&nbsp;new&nbsp;Product(i,"书籍","追风筝的人",22.5);
&nbsp;//将java对象转换成json字符串
&nbsp;String&nbsp;jsonProduct&nbsp;=&nbsp;JsonParseUtil.getJsonString4JavaPOJO(product);
&nbsp;//将json字符串解析成Document对象
&nbsp;Document&nbsp;docProduct&nbsp;=&nbsp;Document.parse(jsonProduct);
&nbsp;documents.add(docProduct);
}

System.out.println("开始插入数据。。。");
long&nbsp;startInsert&nbsp;=&nbsp;System.currentTimeMillis();
instance.bulkWriteInsert(documents);
System.out.println("插入数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startInsert)+"毫秒");

结果:1560毫秒,多次测试基本在1.5秒左右

cbc02a30fedf596fbee26b6fca4ab947.jpeg

(2)、逐条插入

下面再通过非批量插入10万个数据对比下,方法如下:

&nbsp;public&nbsp;void&nbsp;insertOneByOne(List<Document>&nbsp;documents)&nbsp;throws&nbsp;ParseException{
&nbsp;&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents){
&nbsp;&nbsp;&nbsp;collection.insertOne(document);
&nbsp;&nbsp;}
&nbsp;}

测试:10万条数据

System.out.println("开始插入数据。。。");
long&nbsp;startInsert&nbsp;=&nbsp;System.currentTimeMillis();
instance.insertOneByOne(documents);
System.out.println("插入数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startInsert)+"毫秒");

结果:12068毫秒,差距非常大。由此可见,MongoDB批量插入比逐条数据插入效率提高了非常多。

729c03c05fcbc663c232fa65b5827fcb.jpeg

补充:

MongoCollection的insertMany()方法和bulkWrite()方法是等价的,测试时间差不多,不再贴图。

&nbsp;public&nbsp;void&nbsp;insertMany(List<Document>&nbsp;documents)&nbsp;throws&nbsp;ParseException{
&nbsp;&nbsp;//和bulkWrite()方法等价
&nbsp;&nbsp;collection.insertMany(documents);
&nbsp;}

2、删除操作

(1)、批量删除

掌握了批量插入,批量删除就是依葫芦画瓢了。构造DeleteOneModel需要一个Bson类型参数,代表一个删除操作,这里使用了Bson类的子类Document。重点来了,这里的删除条件使用文档的_id字段,该字段在文档插入数据库后自动生成,没插入数据库前document.get("_id")为null,如果使用其他条件比如productId,那么要在文档插入到collection后在productId字段上添加索引

collection.createIndex(new&nbsp;Document("productId",&nbsp;1));

因为随着collection数据量的增大,查找将越耗时,添加索引是为了提高查找效率,进而加快删除效率。另外,值得一提的是DeleteOneModel表示至多删除一条匹配条件的记录,DeleteManyModel表示删除匹配条件的所有记录。为了防止一次删除多条记录,这里使用DeleteOneModel,保证一个操作只删除一条记录。当然这里不可能匹配多条记录,因为_id是唯一的。

最新 MySQL&nbsp;面试题整理好了,点击Java面试库小程序在线刷题。

public&nbsp;void&nbsp;bulkWriteDelete(List<Document>&nbsp;documents){
&nbsp;List<WriteModel<Document>>&nbsp;requests&nbsp;=&nbsp;new&nbsp;ArrayList<WriteModel<Document>>();
&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents)&nbsp;{
&nbsp;&nbsp;//删除条件
&nbsp;&nbsp;Document&nbsp;queryDocument&nbsp;=&nbsp;new&nbsp;Document("_id",document.get("_id"));
&nbsp;&nbsp;//构造删除单个文档的操作模型,
&nbsp;&nbsp;DeleteOneModel<Document>&nbsp;&nbsp;dom&nbsp;=&nbsp;new&nbsp;DeleteOneModel<Document>(queryDocument);
&nbsp;&nbsp;requests.add(dom);
&nbsp;}
&nbsp;BulkWriteResult&nbsp;bulkWriteResult&nbsp;=&nbsp;collection.bulkWrite(requests);
&nbsp;System.out.println(bulkWriteResult.toString());
}

测试:10万条数据

System.out.println("开始删除数据。。。");
long&nbsp;startDelete&nbsp;=&nbsp;System.currentTimeMillis();
instance.bulkWriteDelete(documents);
System.out.println("删除数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startDelete)+"毫秒");

结果:2251毫秒

212d1c2366e049f9ae1b8afd15843697.jpeg

(2)、逐条删除

来看看在非批量下的删除

&nbsp;public&nbsp;void&nbsp;deleteOneByOne(List<Document>&nbsp;documents){
&nbsp;&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents)&nbsp;{
&nbsp;&nbsp;&nbsp;Document&nbsp;queryDocument&nbsp;=&nbsp;new&nbsp;Document("_id",document.get("_id"));
&nbsp;&nbsp;&nbsp;DeleteResult&nbsp;deleteResult&nbsp;=&nbsp;collection.deleteOne(queryDocument);
&nbsp;&nbsp;}
&nbsp;}

测试:10万条数据

System.out.println("开始删除数据。。。");
long&nbsp;startDelete&nbsp;=&nbsp;System.currentTimeMillis();
instance.deleteOneByOne(documents);
System.out.println("删除数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startDelete)+"毫秒");

结果:12765毫秒,比批量删除效率低很多

fdb179aca949c717dd726c393c129d90.jpeg

3、更新操作

(1)、批量更新

再来看看批量更新,分UpdateOneModel和UpdateManyModel两种,区别是前者更新匹配条件的一条记录,后者更新匹配条件的所有记录。对于ReplaceOneModel,表示替换操作,这里也归为更新,现在以UpdateOneModel为例进行讲解。UpdateOneModel构造方法接收3个参数,第一个是查询条件,第二个参数是要更新的内容,第三个参数是可选的UpdateOptions,不填也会自动帮你new一个,代表批量更新操作未匹配到查询条件时的动作,它的upser属性值默认false,什么都不干,true时表示将一个新的Document插入数据库,这个新的Document是查询Document和更新Document的结合,但如果是替换操作,这个新的Document就是这个替换Document。

这里会有个疑惑:这和匹配到查询条件后执行替换操作结果不一样吗?区别在于_id字段,未匹配查询条件时插入的新的Document的_id是新的,而成功执行替换操作,_id是原先旧的。

&nbsp;public&nbsp;void&nbsp;bulkWriteUpdate(List<Document>&nbsp;documents){
&nbsp;&nbsp;List<WriteModel<Document>>&nbsp;requests&nbsp;=&nbsp;new&nbsp;ArrayList<WriteModel<Document>>();
&nbsp;&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents)&nbsp;{
&nbsp;&nbsp;&nbsp;//更新条件
&nbsp;&nbsp;&nbsp;Document&nbsp;queryDocument&nbsp;=&nbsp;new&nbsp;Document("_id",document.get("_id"));
&nbsp;&nbsp;&nbsp;//更新内容,改下书的价格
&nbsp;&nbsp;&nbsp;Document&nbsp;updateDocument&nbsp;=&nbsp;new&nbsp;Document("$set",new&nbsp;Document("price","30.6"));
&nbsp;&nbsp;&nbsp;//构造更新单个文档的操作模型
&nbsp;&nbsp;&nbsp;UpdateOneModel<Document>&nbsp;uom&nbsp;=&nbsp;new&nbsp;UpdateOneModel<Document>(queryDocument,updateDocument,new&nbsp;UpdateOptions().upsert(false));
&nbsp;&nbsp;&nbsp;//UpdateOptions代表批量更新操作未匹配到查询条件时的动作,默认false,什么都不干,true时表示将一个新的Document插入数据库,他是查询部分和更新部分的结合
&nbsp;&nbsp;&nbsp;requests.add(uom);
&nbsp;&nbsp;}
&nbsp;&nbsp;BulkWriteResult&nbsp;bulkWriteResult&nbsp;=&nbsp;collection.bulkWrite(requests);
&nbsp;&nbsp;System.out.println(bulkWriteResult.toString());
&nbsp;}

测试:10万条数据

System.out.println("开始更新数据。。。");
long&nbsp;startUpdate&nbsp;=&nbsp;System.currentTimeMillis();
instance.bulkWriteUpdate(documents);
System.out.println("更新数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startUpdate)+"毫秒");

结果:3198毫秒

d21be4d16a062e84f4446903dd6a204c.jpeg

(2)、逐条更新

对比非批量下的更新

&nbsp;public&nbsp;void&nbsp;updateOneByOne(List<Document>&nbsp;documents){
&nbsp;&nbsp;for&nbsp;(Document&nbsp;document&nbsp;:&nbsp;documents)&nbsp;{
&nbsp;&nbsp;&nbsp;Document&nbsp;queryDocument&nbsp;=&nbsp;new&nbsp;Document("_id",document.get("_id"));
&nbsp;&nbsp;&nbsp;Document&nbsp;updateDocument&nbsp;=&nbsp;new&nbsp;Document("$set",new&nbsp;Document("price","30.6"));
&nbsp;&nbsp;&nbsp;UpdateResult&nbsp;UpdateResult&nbsp;=&nbsp;collection.updateOne(queryDocument,&nbsp;updateDocument);
&nbsp;&nbsp;}
&nbsp;}

测试:10万条数据

System.out.println("开始更新数据。。。");
long&nbsp;startUpdate&nbsp;=&nbsp;System.currentTimeMillis();
instance.updateOneByOne(documents);
System.out.println("更新数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;startUpdate)+"毫秒");

结果:13979毫秒,比批量更新效率低很多

b488ba36b91b5bf4e87cdc365def1980.jpeg

4、混合批量操作

bulkWrite()方法可以对不同类型的写操作进行批量处理,代码如下:

&nbsp;public&nbsp;void&nbsp;bulkWriteMix(){
&nbsp;&nbsp;List<WriteModel<Document>>&nbsp;requests&nbsp;=&nbsp;new&nbsp;ArrayList<WriteModel<Document>>();
&nbsp;&nbsp;&nbsp;InsertOneModel<Document>&nbsp;&nbsp;iom&nbsp;=&nbsp;new&nbsp;InsertOneModel<Document>(new&nbsp;Document("name","kobe"));
&nbsp;&nbsp;&nbsp;UpdateManyModel<Document>&nbsp;umm&nbsp;=&nbsp;new&nbsp;UpdateManyModel<Document>(new&nbsp;Document("name","kobe"),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Document("$set",new&nbsp;Document("name","James")),new&nbsp;UpdateOptions().upsert(true));
&nbsp;&nbsp;&nbsp;DeleteManyModel<Document>&nbsp;&nbsp;dmm&nbsp;=&nbsp;new&nbsp;DeleteManyModel<Document>(new&nbsp;Document("name","James"));
&nbsp;&nbsp;&nbsp;requests.add(iom);
&nbsp;&nbsp;&nbsp;requests.add(umm);
&nbsp;&nbsp;&nbsp;requests.add(dmm);
&nbsp;&nbsp;&nbsp;BulkWriteResult&nbsp;bulkWriteResult&nbsp;=&nbsp;collection.bulkWrite(requests);
&nbsp;&nbsp;&nbsp;System.out.println(bulkWriteResult.toString());
&nbsp;}

注意:updateMany()、deleteMany()两个方法和insertMany()不同,它俩不是批量操作,而是代表更新(删除)匹配条件的所有数据。

最新 MySQL 面试题整理好了,点击Java面试库小程序在线刷题。

二、与MySQL性能对比

1、插入操作

(1)、批处理插入

与MongoDB一样,也是插入Product实体对象,代码如下

&nbsp;public&nbsp;void&nbsp;insertBatch(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("insert&nbsp;into&nbsp;t_product&nbsp;value(?,?,?,?)");
&nbsp;&nbsp;&nbsp;int&nbsp;count&nbsp;=&nbsp;1;
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(2,&nbsp;product.getCategory());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(3,&nbsp;product.getName());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setDouble(4,&nbsp;product.getPrice());
&nbsp;&nbsp;&nbsp;&nbsp;pst.addBatch();
&nbsp;&nbsp;&nbsp;&nbsp;if(count&nbsp;%&nbsp;1000&nbsp;==&nbsp;0){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.executeBatch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.clearBatch();//每1000条sql批处理一次,然后置空PreparedStatement中的参数,这样也能提高效率,防止参数积累过多事务超时,但实际测试效果不明显
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;count++;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

JDBC默认自动提交事务,切记在获取连接后添加下面一行代码,关闭事务自动提交。

connection.setAutoCommit(false);

测试:10万条数据

public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestMysql&nbsp;test&nbsp;=&nbsp;new&nbsp;TestMysql();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList<Product>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList<Product>();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;1000;&nbsp;i++)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product&nbsp;product&nbsp;=&nbsp;new&nbsp;Product(i,&nbsp;"书籍",&nbsp;"追风筝的人",&nbsp;20.5);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(product);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("MYSQL开始插入数据。。。");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;insertStart&nbsp;=&nbsp;System.currentTimeMillis();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test.insertBatch(list);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("MYSQL插入数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;insertStart)+"毫秒");
}

结果:7389毫秒,多次测试基本7秒左右

(2)、逐条插入

再来看看mysql逐条插入,代码如下:

&nbsp;public&nbsp;void&nbsp;insertOneByOne(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("insert&nbsp;into&nbsp;t_product&nbsp;value(?,?,?,?)");
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(2,&nbsp;product.getCategory());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(3,&nbsp;product.getName());
&nbsp;&nbsp;&nbsp;&nbsp;pst.setDouble(4,&nbsp;product.getPrice());
&nbsp;&nbsp;&nbsp;&nbsp;pst.executeUpdate();
&nbsp;&nbsp;&nbsp;&nbsp;//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

测试:10万条记录

System.out.println("MYSQL开始插入数据。。。");
long&nbsp;insertStart&nbsp;=&nbsp;System.currentTimeMillis();
test.insertOneByOne(list);
System.out.println("MYSQL插入数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;insertStart)+"毫秒");

结果:8921毫秒,基本比批量慢1秒多。

2、删除操作

(1)、批处理删除

删除的where条件是productId,这里在建表的时候没有添加主键,删除异常的慢,查了半天不知道什么原因。切记添加主键,主键默认有索引,所有能更快匹配到记录。

插播一条:如果你近期准备面试跳槽,建议在Java面试库小程序在线刷题,涵盖 2000+&nbsp;道 Java 面试题,几乎覆盖了所有主流技术面试题。


&nbsp;public&nbsp;void&nbsp;deleteBatch(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("delete&nbsp;from&nbsp;t_product&nbsp;where&nbsp;id&nbsp;=&nbsp;?");//按主键查,否则全表遍历很慢
&nbsp;&nbsp;&nbsp;int&nbsp;count&nbsp;=&nbsp;1;
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.addBatch();
&nbsp;&nbsp;&nbsp;&nbsp;if(count&nbsp;%&nbsp;1000&nbsp;==&nbsp;0){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.executeBatch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.clearBatch();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;count++;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

测试:10万条数据。

System.out.println("MYSQL开始删除数据。。。");
long&nbsp;deleteStart&nbsp;=&nbsp;System.currentTimeMillis();
test.deleteBatch(list);
System.out.println("MYSQL删除数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;deleteStart)+"毫秒");

结果:7936毫秒

(2)、逐条删除

代码如下

&nbsp;public&nbsp;void&nbsp;deleteOneByOne(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;null;
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("delete&nbsp;from&nbsp;t_product&nbsp;where&nbsp;id&nbsp;=&nbsp;?");
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.executeUpdate();
&nbsp;&nbsp;&nbsp;&nbsp;//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

测试:10万条数据

System.out.println("MYSQL开始删除数据。。。");
long&nbsp;deleteStart&nbsp;=&nbsp;System.currentTimeMillis();
test.deleteOneByOne(list);
System.out.println("MYSQL删除数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;deleteStart)+"毫秒");

结果:8752毫秒,比批处理删除慢一秒左右。

3、更新操作

(1)、批处理更新

代码如下

&nbsp;public&nbsp;void&nbsp;updateBatch(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("update&nbsp;t_product&nbsp;set&nbsp;price=31.5&nbsp;where&nbsp;id=?");
&nbsp;&nbsp;&nbsp;int&nbsp;count&nbsp;=&nbsp;1;
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.addBatch();
&nbsp;&nbsp;&nbsp;&nbsp;if(count&nbsp;%&nbsp;1000&nbsp;==&nbsp;0){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.executeBatch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pst.clearBatch();//每1000条sql批处理一次,然后置空PreparedStatement中的参数,这样也能提高效率,防止参数积累过多事务超时,但实际测试效果不明显
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;count++;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

测试:10万条数据

System.out.println("MYSQL开始更新数据。。。");
long&nbsp;updateStart&nbsp;=&nbsp;System.currentTimeMillis();
test.updateBatch(list);
System.out.println("MYSQL更新数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;updateStart)+"毫秒");

结果:8611毫秒

(2)、逐条更新

代码如下

public&nbsp;void&nbsp;updateOneByOne(ArrayList<Product>&nbsp;list)&nbsp;throws&nbsp;Exception{
&nbsp;&nbsp;Connection&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection();
&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;for&nbsp;(Product&nbsp;product&nbsp;:&nbsp;list)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pst&nbsp;=&nbsp;conn.prepareStatement("update&nbsp;t_product&nbsp;set&nbsp;price=30.5&nbsp;where&nbsp;id=?");
&nbsp;&nbsp;&nbsp;&nbsp;pst.setInt(1,&nbsp;product.getProductId());
&nbsp;&nbsp;&nbsp;&nbsp;pst.executeUpdate();
&nbsp;&nbsp;&nbsp;&nbsp;//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;conn.commit();
&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}
&nbsp;&nbsp;DBUtil.closeConnection(conn);
&nbsp;}

测试:10万条数据

System.out.println("MYSQL开始更新数据。。。");
long&nbsp;updateStart&nbsp;=&nbsp;System.currentTimeMillis();
test.updateOneByOne(list);
System.out.println("MYSQL更新数据完成,共耗时:"+(System.currentTimeMillis()&nbsp;-&nbsp;updateStart)+"毫秒");

结果:9430毫秒,比批处理更新慢了1秒左右

三、总结

本文主要是为了介绍bulkWrite()方法的使用,也就是MongoDB的批量写操作,通过实验可以看出MongoDB使用bulkWrite()方法进行大量数据的写操作比使用常规的方法进行写操作效率要高很多。文章也介绍了mysql几种写操作下批量和非批量的对比,可以看出他们批处理方式比非批处理快点,但没有MongoDB那么明显。

对于MongoDB与mysql的比较,批量操作下,MongoDB插入、删除、更新都比mysql快,非批量操作下,MongoDB插入、删除、更新都比mysql慢。当然只是一个初略的结论,文中并没有进行100条、1000条、10000条或更大的这样不同的数据对比,以及CPU内存使用情况进行监测,有兴趣的可以尝试下。

  • 37
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值