python - Mongodb 上的高全局锁定百分比

标签 python mongodb pymongo

<分区>

我刚开始使用 Mongodb v2.4.8,全局锁定百分比平均为 80%,这对我来说似乎相当高。在运行 Ubuntu 12.04 64 位的 2 核、2GB RAM、SSD VPS 上,CPU 使用率约为 120%。 iotop 显示偶尔的磁盘写入速度约为 10KB/s。 htop 显示 2GB 中仅使用了 358 MB 内存。

2 Python 进程不断地在 mongo 上执行查找/插入/更新操作。查找操作中使用的字段被索引。

为什么全局锁%这么高?我们如何解决这个问题?

彩信

enter image description here

db.serverStatus()

    "myCollection" : {
        "timeLockedMicros" : {
            "r" : NumberLong(161149047),
            "w" : NumberLong(38511071963)
        },
        "timeAcquiringMicros" : {
            "r" : NumberLong(11738433),
            "w" : NumberLong(6056873726)
        }
    },

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     73     *0       0    75|0       0  1.61g  3.42g   283m      0  testCollection:95.7%          0       0|0     0|0    13k    10k    13   15:56:06
    *0     *0     52     *0       0    54|0       0  1.61g  3.42g   283m      0  testCollection:83.6%          0       0|0     0|1     9k     8k    13   15:56:07
    *0     *0     67     *0       0    68|0       0  1.61g  3.42g   283m      0  testCollection:89.4%          0       0|0     0|0    12k     9k    13   15:56:08
     1      1     17      1       1   173|0       0  1.61g  3.42g   283m      0  testCollection:34.3%          0       0|0     0|1    18k    40k    13   15:56:09
    *0     *0     45     *0       0    46|0       0  1.61g  3.42g   283m      0  testCollection:69.2%          0       0|0     0|1     8k     7k    13   15:56:10
    *0     *0     46     *0       0    48|0       0  1.61g  3.42g   283m      0 testCollection:101.2%          0       0|0     0|1     8k     7k    13   15:56:11
    *0     *0     48     *0       0    50|0       0  1.61g  3.42g   283m      0 testCollection:100.5%          0       0|0     0|0     8k     8k    13   15:56:12
    *0     *0     62     *0       0    63|0       0  1.61g  3.42g   283m      0  testCollection:91.5%          0       0|0     0|0    11k     9k    13   15:56:13
    *0     *0     52     *0       0    53|0       0  1.61g  3.42g   283m      0  testCollection:94.4%          0       0|0     0|1     9k     8k    13   15:56:14
    *0     *0     34     *0       0    36|0       0  1.61g  3.42g   283m      0  testCollection:94.8%          0       0|0     0|1     6k     6k    13   15:56:15
insert  query update delete getmore command flushes mapped  vsize    res faults   locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time
     4      1      8      2       1   167|0       0  1.61g  3.42g   283m      0  testCollection:15.3%          0       0|0     0|1    17k    39k    13   15:56:16
    *0     *0     41     *0       0    43|0       0  1.61g  3.42g   283m      0  testCollection:97.4%          0       0|0     0|1     7k     7k    13   15:56:17
    *0     *0     45     *0       0    46|0       0  1.61g  3.42g   283m      0  testCollection:95.3%          0       0|0     0|1     8k     7k    13   15:56:18
    *0     *0     50     *0       0    52|0       0  1.61g  3.42g   283m      0  testCollection:90.0%          0       0|0     0|1     9k     8k    13   15:56:19
    *0     *0     57     *0       0    58|0       0  1.61g  3.42g   283m      0  testCollection:93.2%          0       0|0     0|1    10k     8k    13   15:56:20
    *0     *0     46     *0       0    48|0       0  1.61g  3.42g   283m      0 testCollection:105.6%          0       0|0     0|1     8k     7k    13   15:56:21
    *0     *0     58     *0       0    60|0       0  1.61g  3.42g   283m      0  testCollection:95.9%          0       0|0     0|0    10k     9k    12   15:56:22
     1      1     12      1       1   167|0       0  1.61g  3.42g   283m      0  testCollection:14.5%          0       0|0     0|1    16k    39k    13   15:56:23
    *0      1     49     *0       0    52|0       0  1.61g  3.42g   283m      0  testCollection:98.8%          0       0|0     0|1     9k    11k    13   15:56:24
    *0     *0     49     *0       0    51|0       0  1.61g  3.42g   283m      0 testCollection:101.9%          0       0|0     0|0     9k     8k    13   15:56:25
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     49     *0       0    50|0       0  1.61g  3.42g   283m      0  testCollection:95.0%          0       0|0     0|1     8k     8k    13   15:56:26
    *0     *0     60     *0       0    62|0       0  1.61g  3.42g   283m      0  testCollection:94.2%          0       0|0     0|1    10k     9k    13   15:56:27
    *0     *0     46     *0       0    47|0       0  1.61g  3.42g   283m      0  testCollection:94.2%          0       0|0     0|1     8k     7k    13   15:56:28
    *0     *0     35     *0       0    38|0       0  1.61g  3.42g   283m      0  testCollection:90.6%          0       0|0     0|0     6k     6k    12   15:56:29
     1      1      1     *0       1   155|0       0  1.61g  3.42g   283m      0   testCollection:0.9%          0       0|0     0|0    15k    38k    13   15:56:30
     1     *0     42      1       0    45|0       0  1.61g  3.42g   283m      0  testCollection:93.3%          0       0|0     0|1     7k     7k    13   15:56:31
    *0     *0     57     *0       0    68|0       0  1.61g  3.42g   283m      0  testCollection:89.6%          0       0|0     0|1    10k    14k    13   15:56:32
    *0     *0     46     *0       0    48|0       0  1.61g  3.42g   283m      0  testCollection:91.9%          0       0|0     0|1     8k     7k    13   15:56:33
    *0     *0     53     *0       0    54|0       0  1.61g  3.42g   283m      0  testCollection:92.2%          0       0|0     0|1     9k     8k    13   15:56:34
    *0     *0     61     *0       0    63|0       0  1.61g  3.42g   283m      0  testCollection:89.3%          0       0|0     0|1    11k     9k    13   15:56:35
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     40     *0       0    61|0       0  1.61g  3.42g   283m      0  testCollection:53.7%          0       0|0     0|0     9k     8k    13  

mongotop

                    ns       total        read       write              2014-01-01T15:59:33
             testCollection.oooc       868ms         0ms       868ms
                 testCollection.         5ms         5ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.oook         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

                    ns       total        read       write              2014-01-01T15:59:34
             testCollection.oooc       891ms         0ms       891ms
                 testCollection.         0ms         0ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.oook         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

                    ns       total        read       write              2014-01-01T15:59:35
             testCollection.oooc       838ms         0ms       838ms
                 testCollection.         0ms         0ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.oook         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

                    ns       total        read       write              2014-01-01T15:59:36
             testCollection.oooc       889ms         0ms       889ms
                 testCollection.         0ms         0ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.oook         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

                    ns       total        read       write              2014-01-01T15:59:37
             testCollection.oooc       831ms         0ms       831ms
                 testCollection.         0ms         0ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.oook         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

                    ns       total        read       write              2014-01-01T15:59:38
             testCollection.oooc       249ms         0ms       249ms
            testCollection.oook        62ms        62ms         0ms
                 testCollection.         0ms         0ms         0ms
            testCollection.depth         0ms         0ms         0ms
   testCollection.system.indexes         0ms         0ms         0ms
testCollection.system.namespaces         0ms         0ms         0ms
     testCollection.system.users         0ms         0ms         0ms
            testCollection.users         0ms         0ms         0ms

Python 代码

这是导致 Adam C 指出的减速的代码。

for date, row in oooc.T.iterkv():

    docExist = db.oooc.find({'timestamp': row['timestamp']})
    if docExist.count() == 0:
        data = json.loads(pd.concat([row, id]).to_json())
        db.oooc.insert(data)
    else:
        data = json.loads(row.to_json())
        db.oooc.update({'timestamp': data['timestamp']}, {'$set': data})

最佳答案

mongotop 的输出是您需要的线索。 1000 毫秒中有 800 多毫秒,它每秒运行一次,用于在 testCollection.oooc 命名空间中写入,因此这基本上是您的罪魁祸首 (800/1000 = ~80%)。

从您的 mongostat 输出来看,这些看起来像是更新,对于相对较低级别的更新,看到如此高的锁定百分比,我会假设您在该集合上有很多索引或者您正在增加文档并导致它们移动(或两者)。它们可能是批量更新,因此真实速率可能远高于 mongostat 建议的值。无论哪种方式,弄清楚您对该集合做了什么,然后修复它。

根据提供的代码更新:

如果不查看代码段中 data 中的内容,很难准确判断发生了什么,但请注意 docExist.count 的任何值都大于 1可以成倍增加我即将描述的效果。

当您最初写入 MongoDB 中的文档时,会为该文档保留特定数量的空间。如果您随后添加字段、扩展数组或以超出原始空间分配的其他方式增加文档,则 MongoDB 必须将文档移动到新位置(并分配更多空间,这是使用 padding factor 所做的)。

因此,与无需移动即可完成的就地更新相比,增加文档的操作非常昂贵。每个这样的操作实际上变成了删除和插入。对于每次此类移动,还会有与索引更新相关的惩罚。

我怀疑更新中的 else 子句会触发文档增长,因此会导致您看到锁定。我会建议改变它,这样它就不会通过手动填充初始插入(以便更新可以就地发生)或使用其他 allocation strategies 之一来触发移动。 .

关于python - Mongodb 上的高全局锁定百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20870485/

相关文章:

python - 使用 Beautiful Soup 在 python 中解析网页

javascript - mongodb 驱动程序不释放资源

python - 无法更新 PyMongo 中的文档

python - mongoDB:PyMongo 中包含单个字母的字符串的全文查询

python - 分组数据框并获得总和和计数?

python - 在Python中,指数对数和中包含负数

python - 根据列排列/排序 Pandas 数据框数据中的数据

javascript - 我们如何在 JavaScript 中为键动态变化的 JSON 形成 mongoDB 模式对象?

mongodb - 如何在解码 MongoDB 文档时忽略空值?

python - 时间数据 '2019-06-02T16:19:27.000-04:00' 与格式 '%Y-%m-%dT%H:%M:%S.%fZ' 不匹配