微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在 mongocxxc++17中“find_one”比“find”需要更长的时间?

如何解决为什么在 mongocxxc++17中“find_one”比“find”需要更长的时间?

我有一个mongodb(大约5M,1.3W文件)。

我将它与 mongocxx(Visual Studio 2019,C++17)一起使用,我像这样编译 diver:

//mongoc release
cmake -G"Visual Studio 16 2019" -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_INSTALL_PREFIX=F:\mongo-windows-x64-cpp17\mongo-c-driver -DCMAKE_PREFIX_PATH=F:\mongo-windows-x64-cpp17\mongo-c-driver -DCMAKE_BUILD_TYPE=Release ..

cmake --bulid . --config Release --target install

//mongocxx release
cmake -G"Visual Studio 16 2019" -DBSONCXX_poly_USE_STD=ON -DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC=ON -DBUILD_VERSION=1.17.4 -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_FLAGS="/Zc:__cplusplus /EHsc" -DCMAKE_PREFIX_PATH=F:\mongo-windows-x64-cpp17\mongo-c-driver -DCMAKE_INSTALL_PREFIX=F:\mongo-windows-x64-cpp17\mongo-cxx-driver -DCMAKE_BUILD_TYPE=Release -DBSONCXX_OUTPUT_BASENAME=bsoncxx -DMONGOCXX_OUTPUT_BASENAME=mongocxx ..

cmake --build . --config Release --target install

然后我测试程序使用的时间如下:

#define KVP_CAST_DOUBLE(str,data) kvp((str),static_cast<double>(data))

mongocxx::instance mginstance{};

U32 _initial_mongodb()
{
    //client pool
    mongocxx::pool *p = new mongocxx::pool{ mongocxx::uri{ "mongodb://127.0.0.1:27017" } };

    //client
    std::unique_ptr<mongocxx::pool::entry> en = std::make_unique<mongocxx::pool::entry>(p->acquire());
    mongocxx::client& cli = *(*(en));

    //database
    auto mongo_db = cli["mongodb"];

    mongocxx::collection col = mongo_db["collection_kNowledgedata"];


    //create index
    mongocxx::options::index unique_opts;
    unique_opts.unique(true);
    //SE_KNowledge
    col.create_index(make_document(kvp("id",1)),unique_opts);
    col.create_index(make_document(kvp("id0",1),kvp("id1",{});

    auto t1 = std::chrono::high_resolution_clock::Now();
    auto result = col.find({});
    auto t2 = std::chrono::high_resolution_clock::Now();
    auto delt1 = (t2 - t1).count();
    std::cout << std::format("find all time = {} ns.\n",delt1);

    auto t3 = std::chrono::high_resolution_clock::Now();
    int i = 0;
    for (auto it = result.begin(); it != result.end(); ++it)
    {
        ++i;
    }

    auto t4 = std::chrono::high_resolution_clock::Now();
    auto delt2 = (t4 - t3).count();
    std::cout << std::format("total documents = {},loop time = {} ns.\n",i,delt2);

    auto t5 = std::chrono::high_resolution_clock::Now();
    bsoncxx::builder::basic::document doc;
    doc.append(KVP_CAST_DOUBLE("id",40889));
    auto t6 = std::chrono::high_resolution_clock::Now();
    std::cout << std::format("make document time = {} ns.\n",(t6 - t5).count());

    auto t7 = std::chrono::high_resolution_clock::Now();
    auto result2 = col.find_one(doc.view());
    auto t8 = std::chrono::high_resolution_clock::Now();
    std::cout << std::format("find one time = {} ns.\n",(t8 - t7).count());

    return EC_Success;
}

结果是(发布):

find all time = 13400 ns.
total documents = 13346,loop time = 13740400 ns.
make document time = 3200 ns.
find one time = 434400 ns.

result

我发现“find_one”比“find”花费更多的时间...
我不知道为什么以及如何优化?
谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。