MongoDB与MySQL效率对比
共 644字,需浏览 2分钟
·
2021-11-04 19:22
阅读本文大概需要 8.5 分钟。
来自:blog.csdn.net/u014513883/article/details/49365987
测试环境:win7旗舰版、16G内存、i3处理器、MongoDB3.0.2、mysql5.0
一、MongoDB批量操作
BulkWriteResult com.mongodb.client.MongoCollection.bulkWrite(List extends WriteModel extends Document>> requests)
1、插入操作
public void bulkWriteInsert(List
documents) {
List> requests = new ArrayList >();
for (Document document : documents) {
//构造插入单个文档的操作模型
InsertOneModeliom = new InsertOneModel (document);
requests.add(iom);
}
BulkWriteResult bulkWriteResult = collection.bulkWrite(requests);
System.out.println(bulkWriteResult.toString());
}
TestMongoDB instance = TestMongoDB.getInstance();
ArrayListdocuments = new ArrayList ();
for (int i = 0; i < 100000; i++) {
Product product = new Product(i,"书籍","追风筝的人",22.5);
//将java对象转换成json字符串
String jsonProduct = JsonParseUtil.getJsonString4JavaPOJO(product);
//将json字符串解析成Document对象
Document docProduct = Document.parse(jsonProduct);
documents.add(docProduct);
}
System.out.println("开始插入数据。。。");
long startInsert = System.currentTimeMillis();
instance.bulkWriteInsert(documents);
System.out.println("插入数据完成,共耗时:"+(System.currentTimeMillis() - startInsert)+"毫秒");
public void insertOneByOne(List
documents) throws ParseException{
for (Document document : documents){
collection.insertOne(document);
}
}
System.out.println("开始插入数据。。。");
long startInsert = System.currentTimeMillis();
instance.insertOneByOne(documents);
System.out.println("插入数据完成,共耗时:"+(System.currentTimeMillis() - startInsert)+"毫秒");
public void insertMany(List
documents) throws ParseException{
//和bulkWrite()方法等价
collection.insertMany(documents);
}
2、删除操作
_id
字段,该字段在文档插入数据库后自动生成,没插入数据库前document.get("_id")
为null,如果使用其他条件比如productId,那么要在文档插入到collection后在productId字段上添加索引collection.createIndex(new Document("productId", 1));
public void bulkWriteDelete(List
documents) {
List> requests = new ArrayList >();
for (Document document : documents) {
//删除条件
Document queryDocument = new Document("_id",document.get("_id"));
//构造删除单个文档的操作模型,
DeleteOneModeldom = new DeleteOneModel (queryDocument);
requests.add(dom);
}
BulkWriteResult bulkWriteResult = collection.bulkWrite(requests);
System.out.println(bulkWriteResult.toString());
}
System.out.println("开始删除数据。。。");
long startDelete = System.currentTimeMillis();
instance.bulkWriteDelete(documents);
System.out.println("删除数据完成,共耗时:"+(System.currentTimeMillis() - startDelete)+"毫秒");
public void deleteOneByOne(List
documents) {
for (Document document : documents) {
Document queryDocument = new Document("_id",document.get("_id"));
DeleteResult deleteResult = collection.deleteOne(queryDocument);
}
}
System.out.println("开始删除数据。。。");
long startDelete = System.currentTimeMillis();
instance.deleteOneByOne(documents);
System.out.println("删除数据完成,共耗时:"+(System.currentTimeMillis() - startDelete)+"毫秒");
3、更新操作
public void bulkWriteUpdate(List documents) {
List> requests = new ArrayList >();
for (Document document : documents) {
//更新条件
Document queryDocument = new Document("_id",document.get("_id"));
//更新内容,改下书的价格
Document updateDocument = new Document("$set",new Document("price","30.6"));
//构造更新单个文档的操作模型
UpdateOneModeluom = new UpdateOneModel (queryDocument,updateDocument,new UpdateOptions().upsert(false));
//UpdateOptions代表批量更新操作未匹配到查询条件时的动作,默认false,什么都不干,true时表示将一个新的Document插入数据库,他是查询部分和更新部分的结合
requests.add(uom);
}
BulkWriteResult bulkWriteResult = collection.bulkWrite(requests);
System.out.println(bulkWriteResult.toString());
}
System.out.println("开始更新数据。。。");
long startUpdate = System.currentTimeMillis();
instance.bulkWriteUpdate(documents);
System.out.println("更新数据完成,共耗时:"+(System.currentTimeMillis() - startUpdate)+"毫秒");
public void updateOneByOne(List
documents) {
for (Document document : documents) {
Document queryDocument = new Document("_id",document.get("_id"));
Document updateDocument = new Document("$set",new Document("price","30.6"));
UpdateResult UpdateResult = collection.updateOne(queryDocument, updateDocument);
}
}
System.out.println("开始更新数据。。。");
long startUpdate = System.currentTimeMillis();
instance.updateOneByOne(documents);
System.out.println("更新数据完成,共耗时:"+(System.currentTimeMillis() - startUpdate)+"毫秒");
4、混合批量操作
public void bulkWriteMix(){
List> requests = new ArrayList >();
InsertOneModeliom = new InsertOneModel (new Document("name","kobe"));
UpdateManyModelumm = new UpdateManyModel (new Document("name","kobe"),
new Document("$set",new Document("name","James")),new UpdateOptions().upsert(true));
DeleteManyModeldmm = new DeleteManyModel (new Document("name","James"));
requests.add(iom);
requests.add(umm);
requests.add(dmm);
BulkWriteResult bulkWriteResult = collection.bulkWrite(requests);
System.out.println(bulkWriteResult.toString());
}
二、与MySQL性能对比
1、插入操作
public void insertBatch(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
try {
PreparedStatement pst = conn.prepareStatement("insert into t_product value(?,?,?,?)");
int count = 1;
for (Product product : list) {
pst.setInt(1, product.getProductId());
pst.setString(2, product.getCategory());
pst.setString(3, product.getName());
pst.setDouble(4, product.getPrice());
pst.addBatch();
if(count % 1000 == 0){
pst.executeBatch();
pst.clearBatch();//每1000条sql批处理一次,然后置空PreparedStatement中的参数,这样也能提高效率,防止参数积累过多事务超时,但实际测试效果不明显
}
count++;
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
connection.setAutoCommit(false);
public static void main(String[] args) throws Exception {
TestMysql test = new TestMysql();
ArrayListlist = new ArrayList ();
for (int i = 0; i < 1000; i++) {
Product product = new Product(i, "书籍", "追风筝的人", 20.5);
list.add(product);
}
System.out.println("MYSQL开始插入数据。。。");
long insertStart = System.currentTimeMillis();
test.insertBatch(list);
System.out.println("MYSQL插入数据完成,共耗时:"+(System.currentTimeMillis() - insertStart)+"毫秒");
}
public void insertOneByOne(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
try {
for (Product product : list) {
PreparedStatement pst = conn.prepareStatement("insert into t_product value(?,?,?,?)");
pst.setInt(1, product.getProductId());
pst.setString(2, product.getCategory());
pst.setString(3, product.getName());
pst.setDouble(4, product.getPrice());
pst.executeUpdate();
//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
System.out.println("MYSQL开始插入数据。。。");
long insertStart = System.currentTimeMillis();
test.insertOneByOne(list);
System.out.println("MYSQL插入数据完成,共耗时:"+(System.currentTimeMillis() - insertStart)+"毫秒");
2、删除操作
public void deleteBatch(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
try {
PreparedStatement pst = conn.prepareStatement("delete from t_product where id = ?");//按主键查,否则全表遍历很慢
int count = 1;
for (Product product : list) {
pst.setInt(1, product.getProductId());
pst.addBatch();
if(count % 1000 == 0){
pst.executeBatch();
pst.clearBatch();
}
count++;
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
System.out.println("MYSQL开始删除数据。。。");
long deleteStart = System.currentTimeMillis();
test.deleteBatch(list);
System.out.println("MYSQL删除数据完成,共耗时:"+(System.currentTimeMillis() - deleteStart)+"毫秒");
public void deleteOneByOne(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
PreparedStatement pst = null;
try {
for (Product product : list) {
pst = conn.prepareStatement("delete from t_product where id = ?");
pst.setInt(1, product.getProductId());
pst.executeUpdate();
//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
System.out.println("MYSQL开始删除数据。。。");
long deleteStart = System.currentTimeMillis();
test.deleteOneByOne(list);
System.out.println("MYSQL删除数据完成,共耗时:"+(System.currentTimeMillis() - deleteStart)+"毫秒");
3、更新操作
public void updateBatch(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
try {
PreparedStatement pst = conn.prepareStatement("update t_product set price=31.5 where id=?");
int count = 1;
for (Product product : list) {
pst.setInt(1, product.getProductId());
pst.addBatch();
if(count % 1000 == 0){
pst.executeBatch();
pst.clearBatch();//每1000条sql批处理一次,然后置空PreparedStatement中的参数,这样也能提高效率,防止参数积累过多事务超时,但实际测试效果不明显
}
count++;
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
System.out.println("MYSQL开始更新数据。。。");
long updateStart = System.currentTimeMillis();
test.updateBatch(list);
System.out.println("MYSQL更新数据完成,共耗时:"+(System.currentTimeMillis() - updateStart)+"毫秒");
public void updateOneByOne(ArrayList
list) throws Exception{
Connection conn = DBUtil.getConnection();
try {
for (Product product : list) {
PreparedStatement pst = conn.prepareStatement("update t_product set price=30.5 where id=?");
pst.setInt(1, product.getProductId());
pst.executeUpdate();
//conn.commit();//加上这句每次插入都提交事务,结果将是非常耗时
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(conn);
}
System.out.println("MYSQL开始更新数据。。。");
long updateStart = System.currentTimeMillis();
test.updateOneByOne(list);
System.out.println("MYSQL更新数据完成,共耗时:"+(System.currentTimeMillis() - updateStart)+"毫秒");
三、总结
推荐阅读:
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅