mongo upsert performance(C ++)

时间:2014-03-12 14:13:13

标签: c++ performance mongodb upsert

我正在编写一个C ++客户端,它处理一个包含1M行的文件并将它们插入到mongodb中。数据被插入6个不同的表中,这些表遵循不同的累积标准。我在C ++地图中累积了5000行(可配置)的数据,然后插入(upsert)到mongodb中,冲洗并重复直到文件末尾。

其中一个upsert的代码是:

void Upsert(mongo::DBClientConnection & cmon)
    {
        map<KEY, VALUE>::const_iterator it;
        cmon.ensureIndex("test.WebNavigationUri", BSON("ClientId" << 1 << "Uri" << 1 << "Date" << 1), true);

        for (it=detail.begin(); it!=detail.end(); ++it)
        {
            cmon.update("test.WebNavigationUri",
                BSON("ClientId" << it->first.clientid << "Uri" << it->first.uri << "Date" << it->second.dHora),
                BSON("$inc" << BSON("Time" << it->second.time)),
                true);
        }
    }

除了存储的信息之外,其他upsert是相同的。 我的问题是,如果我只使用2个表,时间相当快(比MySql快4-8倍),但如果我使用全部6个,性能会下降。

有两张桌子:

WebAccumulationCategory:6877ms 261749 项目; 1465ms (1465 + 0 + 0 + 0)。

WebAccumulationClientId:5005ms 0项; 0ms(0 + 0 + 0 + 0)。

WebAccumulationPolicy:5115ms 0项; 0ms(0 + 0 + 0 + 0)。

WebAccumulationVirusName:0ms 0项; 0ms(0 + 0 + 0 + 0)。

WebNavigationCategory:5442ms 0项; 0ms(0 + 0 + 0 + 0)。

WebNavigationUri:9493ms 414610 项目; 3666ms (3666 + 0 + 0 + 0)。

有6个表(相同的文件,表格已删除):

WebAccumulationCategory:6605ms 261749 项目; 1421ms (1421 + 0 + 0 + 0)

WebAccumulationClientId:5367ms 172873项; 1753ms(1753 + 0 + 0 + 0)

WebAccumulationPolicy:6187ms 174941项; 2086ms(2086 + 0 + 0 + 0)

WebAccumulationVirusName:12ms 52项; 0ms(0 + 0 + 0 + 0)

WebNavigationCategory:18745ms 258072项; 14510ms(14510 + 0 + 0 + 0)

WebNavigationUri:33046ms 414610 项目; 27317ms (27317 + 0 + 0 + 0)

使用

在计划中衡量时间
boost::posix_time::microsec_clock::local_time();

我在Oracle Server 6.5计算机中使用mongodb 2.4.9。该程序使用C ++驱动程序并在同一台机器上运行。

正如您所看到的,当我在其他4个表中插入时,WebNavigationUri从3666ms变为27317ms,但WebAccumulationCategory的时间保持不变。表格和/或我制作upsert的顺序之间似乎存在某种相互作用。 任何想法为什么会出现这种性能损失?我可以在数据库中查找/监控什么?

mongostat说:

insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0     *0     *0       0     1|0       0   544m  1.42g   247m      0  test:0.0%          0       0|0     0|0    62b     3k     6   12:09:59  
     3     *0   2696     *0       0     1|0       0   544m  1.42g   254m      4 test:23.5%          0       0|0     0|0   583k     3k     6   12:10:00  
     3     *0  10635     *0       0     2|0       0   544m  1.42g   248m      3 test:80.3%          0       0|0     0|1     2m     3k     6   12:10:01  
    *0     *0  13151     *0       0     1|0       0   544m  1.42g   252m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:10:02  
    *0     *0  13089     *0       0     1|0       0   544m  1.42g   253m      0 test:81.7%          0       0|0     0|1     2m     3k     6   12:10:03  
    *0     *0  13567     *0       0     1|0       0   544m  1.42g   250m      0 test:81.5%          0       0|0     0|1     2m     3k     6   12:10:04  
    *0     *0  13636     *0       0     1|0       0   544m  1.42g   248m      0 test:81.2%          0       0|0     0|1     2m     3k     6   12:10:05  
    *0     *0  13417     *0       0     2|0       0   544m  1.42g   255m      0 test:78.9%          0       0|0     0|1     2m     3k     6   12:10:06  
    *0     *0  13759     *0       0     1|0       0   544m  1.42g   256m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:07  
    *0     *0  13556     *0       0     1|0       0   544m  1.42g   257m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:10:08  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  13821     *0       0     1|0       0   544m  1.42g   257m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:09  
    *0     *0  13932     *0       0     1|0       0   544m  1.42g   258m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:10  
    *0     *0  13460     *0       0     2|0       0   544m  1.42g   258m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:11  
    *0     *0  13823     *0       0     1|0       0   544m  1.42g   256m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:10:12  
    *0     *0  13811     *0       0     1|0       0   544m  1.42g   253m      0 test:80.3%          0       0|0     0|1     2m     3k     6   12:10:13  
    *0     *0  13794     *0       0     1|0       0   544m  1.42g   251m      0 test:81.2%          0       0|0     0|1     2m     3k     6   12:10:14  
    *0     *0  13605     *0       0     1|0       0   544m  1.42g   247m      0 test:79.6%          0       0|1     0|1     2m     3k     6   12:10:15  
    *0     *0  13403     *0       0     2|0       0   544m  1.42g   261m      0 test:79.0%          0       0|0     0|1     2m     3k     6   12:10:16  
    *0     *0  13781     *0       0     1|0       0   544m  1.42g   261m      0 test:81.2%          0       0|0     0|0     2m     3k     6   12:10:17  
    *0     *0  13824     *0       0     1|0       0   544m  1.42g   262m      0 test:80.6%          0       0|0     0|1     2m     3k     6   12:10:18  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  13954     *0       0     1|0       0   544m  1.42g   262m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:19  
    *0     *0  13873     *0       0     2|0       0   544m  1.42g   262m      0 test:81.3%          0       0|0     0|1     2m     3k     6   12:10:20  
    *0     *0  13416     *0       0     2|0       0   544m  1.42g   261m      0 test:79.4%          0       0|0     0|1     2m     3k     6   12:10:21  
    *0     *0  13840     *0       0     1|0       0   544m  1.42g   261m      0 test:81.2%          0       0|0     0|1     2m     3k     6   12:10:22  
    *0      1  13657     *0       0    13|0       0   544m  1.42g   259m      0 test:80.4%          0       0|0     0|1     2m    11k     6   12:10:23  
    *0     *0  12566     *0       0     1|0       0   544m  1.42g   259m      0 test:75.5%          0       0|0     0|1     2m     3k     6   12:10:24  
    *0     *0  13777     *0       0     1|0       0   544m  1.42g   251m      0 test:80.4%          0       0|0     0|1     2m     3k     6   12:10:25  
    *0     *0  13148     *0       0     2|0       0   544m  1.42g   265m      0 test:77.6%          0       0|0     0|1     2m     3k     6   12:10:26  
    *0     *0  13691     *0       0     1|0       0   544m  1.42g   265m      0 test:80.7%          0       0|0     0|1     2m     3k     6   12:10:27  
    *0     *0  13633     *0       0     1|0       0   544m  1.42g   265m      0 test:80.2%          0       0|0     0|0     2m     3k     6   12:10:28  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  13538     *0       0     1|0       0   544m  1.42g   265m      0 test:80.7%          0       0|0     0|1     2m     3k     6   12:10:29  
    *0     *0  13773     *0       0     1|0       0   544m  1.42g   265m      0 test:80.4%          0       0|0     0|1     2m     3k     6   12:10:30  
    *0     *0   9118     *0       0     2|0       0   544m  1.42g   263m      0 test:52.9%          0       0|0     0|1     1m     3k     6   12:10:31  
    *0     *0  11894     *0       0     1|0       0   544m  1.42g   268m      0 test:68.6%          0       0|0     0|1     2m     3k     6   12:10:32  
    *0     *0  13742     *0       0     1|0       0   544m  1.42g   266m      0 test:80.6%          0       0|0     0|1     2m     3k     6   12:10:33  
    *0     *0  13583     *0       0     1|0       0   544m  1.42g   266m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:10:34  
    *0     *0  13769     *0       0     1|0       0   544m  1.42g   266m      0 test:81.4%          0       0|0     0|1     2m     3k     6   12:10:35  
    *0     *0  13921     *0       0     2|0       0   544m  1.42g   268m      0 test:81.1%          0       0|0     0|1     2m     3k     6   12:10:36  
    *0     *0  13414     *0       0     1|0       0   544m  1.42g   265m      0 test:78.4%          0       0|0     0|1     2m     3k     6   12:10:37  
    *0     *0  13489     *0       0     1|0       0   544m  1.42g   263m      0 test:80.7%          0       0|0     0|1     2m     3k     6   12:10:38  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  13847     *0       0     1|0       0   544m  1.42g   262m      0 test:81.3%          0       0|0     0|1     2m     3k     6   12:10:39  
    *0      3  13776     *0       0     1|0       0   544m  1.42g   266m      0 test:80.6%          0       0|0     0|1     2m     3k     6   12:10:40  
    *0     *0  14000     *0       0     2|0       0   544m  1.42g   251m      0 test:81.1%          0       0|0     0|1     2m     3k     6   12:10:41  
    *0     *0  13416     *0       0     1|0       0   544m  1.42g   270m      0 test:77.8%          0       0|0     0|1     2m     3k     6   12:10:42  
    *0     *0  13583     *0       0     1|0       0   544m  1.42g   270m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:10:43  
    *0     *0  13854     *0       0     1|0       0   544m  1.42g   272m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:10:44  
    *0     *0  13742     *0       0     1|0       0   544m  1.42g   270m      0 test:80.6%          0       0|0     0|1     2m     3k     6   12:10:45  
    *0     *0  13708     *0       0     2|0       0   544m  1.42g   269m      0 test:80.7%          0       0|0     0|1     2m     3k     6   12:10:46  
    *0     *0  13675     *0       0     1|0       0   544m  1.42g   269m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:10:47  
    *0     *0  11748     *0       0     1|0       0   544m  1.42g   271m      0 test:68.9%          0       0|0     0|1     2m     3k     6   12:10:48  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0   9845     *0       0     1|0       1   544m  1.42g   274m      0 test:56.4%          0       0|0     0|1     1m     3k     6   12:10:49  
    *0     *0  13623     *0       0     2|0       0   544m  1.42g   271m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:10:50  
    *0     *0  13530     *0       0     2|0       0   544m  1.42g   271m      0 test:80.4%          0       0|0     0|1     2m     3k     6   12:10:51  
    *0     *0  14127     *0       0     1|0       0   544m  1.42g   274m      0 test:81.2%          0       0|0     0|1     2m     3k     6   12:10:52  
    *0     *0  13734     *0       0     1|0       0   544m  1.42g   270m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:10:53  
    *0     *0  13305     *0       0     1|0       0   544m  1.42g   268m      0 test:81.3%          0       0|0     0|1     2m     3k     6   12:10:54  
    *0     *0  13575     *0       0     1|0       0   544m  1.42g   269m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:10:55  
    *0     *0  13735     *0       0     2|0       0   544m  1.42g   272m      0 test:80.2%          0       0|0     0|1     2m     3k     6   12:10:56  
    *0     *0  13809     *0       0     1|0       0   544m  1.42g   257m      0 test:80.5%          0       0|0     0|1     2m     3k     6   12:10:57  
    *0     *0  13896     *0       0     1|0       0   544m  1.42g   252m      0 test:81.3%          0       0|0     0|1     2m     3k     6   12:10:58  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  13207     *0       0     1|0       0   544m  1.42g   274m      0 test:78.0%          0       0|0     0|0     2m     3k     6   12:10:59  
    *0     *0  14075     *0       0     1|0       0   544m  1.42g   276m      0 test:81.1%          0       0|0     0|1     2m     3k     6   12:11:00  
    *0     *0  11833     *0       0     2|0       0   544m  1.42g   273m      0 test:70.9%          0       0|0     0|1     2m     3k     6   12:11:01  
    *0     *0  13540     *0       0     1|0       0   544m  1.42g   271m      0 test:81.0%          0       0|0     0|1     2m     3k     6   12:11:02  
    *0     *0  13762     *0       0     1|0       0   544m  1.42g   272m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:11:03  
    *0     *0  13535     *0       0     1|0       0   544m  1.42g   275m      0 test:79.3%          0       0|0     0|1     2m     3k     6   12:11:04  
    *0     *0  13584     *0       0     1|0       0   544m  1.42g   265m      0 test:80.1%          0       0|0     0|1     2m     3k     6   12:11:05  
    *0     *0   9272     *0       0     2|0       0   544m  1.42g   275m      0 test:54.3%          0       0|0     0|1     1m     3k     6   12:11:06  
    *0     *0   8524     *0       0     1|0       0   544m  1.42g   271m      0 test:52.8%          0       0|0     0|1     1m     3k     6   12:11:07  
    *0     *0  13772     *0       0     1|0       0   544m  1.42g   272m      0 test:81.6%          0       0|0     0|1     2m     3k     6   12:11:08  
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time  
    *0     *0  11479     *0       0     1|0       0   544m  1.42g   271m      0 test:65.8%          0       1|0     0|1     2m     3k     6   12:11:09  
    *0     *0  13835     *0       0     1|0       0   544m  1.42g   252m      0 test:80.8%          0       0|0     0|1     2m     3k     6   12:11:10  
    *0     *0  13597     *0       0     2|0       0   544m  1.42g   276m      0 test:80.2%          0       0|0     0|1     2m     3k     6   12:11:11  
    *0     *0  13704     *0       0     1|0       0   544m  1.42g   275m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:11:12  
    *0     *0  14127     *0       0     1|0       0   544m  1.42g   279m      0 test:81.2%          0       0|0     0|1     2m     3k     6   12:11:13  
    *0     *0  13144     *0       0     1|0       0   544m  1.42g   275m      0 test:78.1%          0       0|0     0|1     2m     3k     6   12:11:14  
    *0     *0  13680     *0       0     1|0       0   544m  1.42g   273m      0 test:80.9%          0       0|0     0|1     2m     3k     6   12:11:15  
    *0     *0  13491     *0       0     2|0       0   544m  1.42g   273m      0 test:78.9%          0       0|0     0|0     2m     3k     6   12:11:16  
    *0     *0     *0     *0       0     1|0       0   544m  1.42g   265m      0     .:0.1%          0       0|0     0|0    62b     3k     5   12:11:17  
    *0      1     *0     *0       0    13|0       0   544m  1.42g   258m      0 config:0.1%          0       0|0     0|0   892b    11k     5   12:11:18 

阻止是80%,但我想这是正常的,因为我每秒都在发送13K记录。

2 个答案:

答案 0 :(得分:1)

  

Blockquote任何想法为什么会出现这种性能损失?我可以在数据库中查找/监控什么?

您可能想要运行mongostat(http://docs.mongodb.org/manual/reference/program/mongostat/):

 mongostat --username <user> --password <pass>

并注意故障的数量,如果不是零,你的工作集不适合内存,因此数据访问变得非常慢。解决方案是使用索引,提供更多RAM和/或开始分片数据库

答案 1 :(得分:1)

使用不同的数据库。 MongoDB的当前版本具有数据库级别锁定,因此不使用test所有版本,只需使用test1test2testN ...

您也可以尝试在旧版驱动程序中使用批量api(定于下一个版本)以避免网络往返。