我尝试更新(每个Thread 100记录中的5个线程)一个集合中的500条记录,花了12.09(或)14分钟。(使用批量更新完成)。但是,当我进行批量插入时,它只花了毫秒(256)。我需要知道,为什么我的批量更新需要更多时间。如何以更快的方式执行批量更新。?
这是我的测试类,
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();
}
}
}
BulckUpdateThread
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));
}
}