为什么使用批量upsert write的MongoDB Java 3.0驱动程序和执行单个upsert的C ++ 2.6驱动程序具有相同的性能?

时间:2015-11-02 02:51:17

标签: java c++ performance mongodb

下面的代码表示使用Java 3.0驱动程序进行批量写入 - 一次进行14 000次upsert操作,c ++ 2.6进行14 000次单独upsert。 Java和C ++代码的执行时间是相同的 - 大约53秒。为什么批量写入没有性能优势?

 // Java driver with bulk write operation (14 000 upsert operation  preapeared and executed in one bulk operation:


public void addBookInfo(String bookTitle, HashMap<String, Integer> bookInfo)
{
// insert information to the book collection
Document d = new Document();
d.append("book_title", bookTitle);
book.insertOne(d);
// insert information to the word collection
// prepare collection of word info and book_word info documents
List<Document> wordInfoToInsert = new ArrayList<Document>();
List<Document> book_wordInfoToInsert = new ArrayList<Document>();
for (String key : bookInfo.keySet())
{
    Document d1 = new Document();
    Document d2 = new Document();
    d1.append("word", key);
    d1.append("count", bookInfo.get(key));
    wordInfoToInsert.add(d1);
    d2.append("book_title", bookTitle);
    d2.append("word", key);
    d2.append("count", bookInfo.get(key));
    book_wordInfoToInsert.add(d2);
}
// this is collection of insert/update DB operations
List<WriteModel<Document>> updates = new ArrayList<WriteModel<Document>>();
// iterator for collection of words
ListIterator<Document> listIterator = wordInfoToInsert.listIterator();
// generate list of insert/update operations
while (listIterator.hasNext()) 
{
    d = listIterator.next();
    String wordToUpdate = d.getString("word");
    int countToAdd = d.getInteger("count").intValue();
    updates.add(
        new UpdateOneModel<Document>(
            new Document("word", wordToUpdate),

            new Document("$inc",new Document("count", countToAdd)),
            new UpdateOptions().upsert(true)
        )
    );
}
// perform bulk operation
// this is slowly
BulkWriteResult bulkWriteResult = word.bulkWrite(updates);


// C++ 2.6 driver - 14 000 single inserts executed one by one.

void DAO::addBookInfo(std::string bookTitle, std::map<std::string, int>   bookInfo)
{
// insert information to the book collection
BSONObjBuilder b;
b.append("book_title", bookTitle);
BSONObj d = b.obj();
connection.insert(book_collection, d);
// insert information to the word collection
// prepare collection of word info and book_word info documents
vector<BSONObj> wordInfoToInsert;
wordInfoToInsert.clear();
vector<BSONObj> book_wordInfoToInsert;
book_wordInfoToInsert.clear();
std::map<std::string, int>::iterator itr;
itr = bookInfo.begin();
while (itr != bookInfo.end())
{
    std::pair<string, int> wordInfo = *itr;
    BSONObjBuilder b1;
    b1.append("word", wordInfo.first);
    b1.append("count", wordInfo.second);
    BSONObj d1 = b1.obj();
    wordInfoToInsert.push_back(d1);
    BSONObjBuilder b2;
    b2.append("book_title", bookTitle);
    b2.append("word", wordInfo.first);
    b2.append("count", wordInfo.second);
    BSONObj d2 = b2.obj();
    book_wordInfoToInsert.push_back(d2);
    itr++;
}

cout << "wordInfoToInsert " << wordInfoToInsert.size() << endl;
cout << "book_wordInfoToInsert " << book_wordInfoToInsert.size() << endl;
// prepare Query document;
vector<BSONObj>::iterator witr;
// upsert info to word collection
witr = wordInfoToInsert.begin();
while (witr != wordInfoToInsert.end())
{
    BSONObj widoc = *witr;
    BSONObjBuilder bqwi;
    bqwi.append("word", widoc.getStringField("word"));
    BSONObj qwi = bqwi.obj();
    Query q(qwi);
    BSONObjBuilder buwidoc;
    buwidoc.append("$inc", BSON("count" << widoc.getIntField("count")));
    BSONObj uwidoc = buwidoc.obj();
    connection.update(word_collection, q, uwidoc, true);
    witr++;
}

0 个答案:

没有答案