使用java在mongodb中批量更新500个文档的最佳方法是什么
我尝试在一个集合中更新500条记录(每个线程5个线程100条记录),花了12.09(或)14分钟(使用批量更新完成)。但是,当我进行大容量插入时,只需要几毫秒(256)。我需要知道,为什么我的批量更新需要更多的时间。如何以更快的方式执行批量更新 这是我的测试课使用java在mongodb中批量更新500个文档的最佳方法是什么,java,multithreading,mongodb,Java,Multithreading,Mongodb,我尝试在一个集合中更新500条记录(每个线程5个线程100条记录),花了12.09(或)14分钟(使用批量更新完成)。但是,当我进行大容量插入时,只需要几毫秒(256)。我需要知道,为什么我的批量更新需要更多的时间。如何以更快的方式执行批量更新 这是我的测试课 public class threadaccess { public static void main(String s[]){ System.err.println("Thread starting:");
public class threadaccess {
public static void main(String s[]){
System.err.println("Thread starting:");
for(int i=5;i<10;i++){
new BulkUpdateThread(i).start();
}
}
}
公共类线程访问{
公共静态void main(字符串s[]{
System.err.println(“线程启动:”);
对于(int i=5;i新日期().getMinutes()){
}
长s=新日期().getTime();
while(no
为什么要在更新中设置id
?是否所有文档都更新为相同的数据?如果是这样的话,为什么不使用Mongo的批量更新操作,而不是逐个更新每个文档呢?请使用bulk.find.update()仔细查看谢谢。但我已经在使用Bulk.find.update()。请参考我的代码..@Darth Android。我正在设置id,但它是字段名之一。它不是唯一的(_id)。测试目的仅更新了同一文档,使用批量更新和功能I将在使用批量更新中更新不同的文档。您是否比较了更新和插入之间的性能?它们是不同类型的操作。为什么要在更新中设置id
?是否所有文档都更新为相同的数据?如果是这样的话,为什么不使用Mongo的批量更新操作,而不是逐个更新每个文档呢?请使用bulk.find.update()仔细查看谢谢。但我已经在使用Bulk.find.update()。请参考我的代码..@Darth Android。我正在设置id,但它是字段名之一。它不是唯一的(_id)。测试目的仅更新了同一文档,使用批量更新和功能I将在使用批量更新中更新不同的文档。您是否比较了更新和插入之间的性能?它们是不同类型的操作。
import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class BulkUpdateThread extends Thread {
int threads ;
public BulkUpdateThread(Integer threadid){
setName(""+threadid);
threads=threadid;
}
public void run(){
long con =(threads+1)*100;
long no = threads*100;
Mongo mongo = new Mongo("192.168.1.197",27017);
DB db = mongo.getDB("Thread");
DBCollection collection = db.getCollection("large");
collection.setWriteConcern(new WriteConcern(1));
BulkWriteOperation bulkop = collection.initializeOrderedBulkOperation();
long a =0;
while(a > new Date().getMinutes()){
}
long s = new Date().getTime();
while(no < con){
try{
BasicDBObject document = new BasicDBObject();
document.put("id", no);
BasicDBObject updatedoc = new BasicDBObject();
updatedoc.put("id", no);
updatedoc.put("name", "sathish003");
updatedoc.put("age",24);
updatedoc.put("DOB",new Date());
BasicDBObject up = new BasicDBObject();
up.put("$set", updatedoc);
long start = new Date().getTime();
bulkop.find(document).update(up);
long end = new Date().getTime();
System.out.println("Thread-"+getName()+" : "+(++no)+" : "+(end-start));
} catch (Exception iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
} bulkop.execute();
long end = new Date().getTime();
System.out.println("----Thread -"+threads+" StartTime: "+s+" End Time -"+end+" TimeTaken: "+(end-s));
}
}