MongoDB的性能和并发问题

东方凡 发布于 2013/12/07 17:30
阅读 4K+
收藏 1

我在项目中使用MongoDB, 内存够大 数据文件全部放在/dev/shm中,数据文件每天零点定时重建,数据只有100万条,结构如下

{ "_id" : ObjectId("52a1f489ba36e5035eaf218b"), "a_id" : 120000, "b_id" : 101, "c_id" : 30101, "d_id" : "BUY", "e_id" : -1, "count" : 1, "g_id" : "869899074f76f0b7bbbe581f600b9edd", "ip" : "8.8.8.8", "i_id" : 100026, "l_time" : 1386345609, "p_time" : 1386349399, "counts" : 1, "p" : "27430", "r_id" : 360000, "s_id" : 1 }

查询量小的时候一切正常,响应速度还蛮快,可一旦查询量上去的话惨不忍睹,serverStatus如下

{ "host" : "localhost.localdomain",
  "version" : "2.4.6",
  "process" : "mongod",
  "pid" : 31387,
  "uptime" : 62645,
  "uptimeMillis" : 62644661,
  "uptimeEstimate" : 48967,
  "localTime" : { "$date" : "Sat Dec  7 17:24:13 2013" },
  "asserts" : { "regular" : 0,
    "warning" : 0,
    "msg" : 0,
    "user" : 0,
    "rollovers" : 0 },
  "backgroundFlushing" : { "flushes" : 1044,
    "total_ms" : 1498,
    "average_ms" : 1.434865900383142,
    "last_ms" : 2,
    "last_finished" : { "$date" : "Sat Dec  7 17:24:09 2013" } },
  "connections" : { "current" : 359,
    "available" : 9241,
    "totalCreated" : 9445656 },
  "cursors" : { "totalOpen" : 144013,
    "clientCursors_size" : 144013,
    "timedOut" : 3704352,
    "totalNoTimeout" : 96046 },
  "extra_info" : { "note" : "fields vary by platform",
    "heap_usage_bytes" : 1374121408,
    "page_faults" : 48 },
  "globalLock" : { "totalTime" : 62644674000,
    "lockTime" : 5809,
    "currentQueue" : { "total" : 73,
      "readers" : 73,
      "writers" : 0 },
    "activeClients" : { "total" : 2,
      "readers" : 2,
      "writers" : 0 } },
  "indexCounters" : { "accesses" : 139135197,
    "hits" : 139135181,
    "misses" : 0,
    "resets" : 0,
    "missRatio" : 0 },
  "locks" : { "." : { "timeLockedMicros" : { "R" : 297412490,
        "W" : 5809 },
      "timeAcquiringMicros" : { "R" : 200122,
        "W" : 119330 } },
    "admin" : { "timeLockedMicros" : { "r" : 168307,
        "w" : 0 },
      "timeAcquiringMicros" : { "r" : 771,
        "w" : 0 } },
    "local" : { "timeLockedMicros" : { "r" : 425866,
        "w" : 128629 },
      "timeAcquiringMicros" : { "r" : 4398,
        "w" : 21287 } },
    "pms" : { "timeLockedMicros" : { "r" : 742008234676,
        "w" : 3889626469 },
      "timeAcquiringMicros" : { "r" : 13712412775800,
        "w" : 16988400383185 } } },
  "network" : { "bytesIn" : 5074683167,
    "bytesOut" : 6127445691,
    "numRequests" : 42957226 },
  "opcounters" : { "insert" : 652773,
    "query" : 24069213,
    "update" : 18236668,
    "delete" : 0,
    "getmore" : 0,
    "command" : 1226 },
  "opcountersRepl" : { "insert" : 0,
    "query" : 0,
    "update" : 0,
    "delete" : 0,
    "getmore" : 0,
    "command" : 0 },
  "recordStats" : { "accessesNotInMemory" : 0,
    "pageFaultExceptionsThrown" : 0,
    "admin" : { "accessesNotInMemory" : 0,
      "pageFaultExceptionsThrown" : 0 },
    "local" : { "accessesNotInMemory" : 0,
      "pageFaultExceptionsThrown" : 0 },
    "pms" : { "accessesNotInMemory" : 0,
      "pageFaultExceptionsThrown" : 0 } },
  "writeBacksQueued" : false,
  "mem" : { "bits" : 64,
    "resident" : 2396,
    "virtual" : 3623,
    "supported" : true,
    "mapped" : 1087 },
  "metrics" : { "document" : { "deleted" : 0,
      "inserted" : 652773,
      "returned" : 61452382,
      "updated" : 18236653 },
    "getLastError" : { "wtime" : { "num" : 0,
        "totalMillis" : 0 },
      "wtimeouts" : 0 },
    "operation" : { "fastmod" : 1,
      "idhack" : 0,
      "scanAndOrder" : 0 },
    "queryExecutor" : { "scanned" : 65552322215 },
    "record" : { "moves" : 842660 },
    "repl" : { "apply" : { "batches" : { "num" : 0,
          "totalMillis" : 0 },
        "ops" : 0 },
      "buffer" : { "count" : 0,
        "maxSizeBytes" : 268435456,
        "sizeBytes" : 0 },
      "network" : { "bytes" : 0,
        "getmores" : { "num" : 0,
          "totalMillis" : 0 },
        "ops" : 0,
        "readersCreated" : 0 },
      "oplog" : { "insert" : { "num" : 0,
          "totalMillis" : 0 },
        "insertBytes" : 0 },
      "preload" : { "docs" : { "num" : 0,
          "totalMillis" : 0 },
        "indexes" : { "num" : 0,
          "totalMillis" : 0 } } },
    "ttl" : { "deletedDocuments" : 0,
      "passes" : 1043 } },
  "ok" : 1 }

mongostat截图如下

insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time
    14    728    709     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:15.2%          0       0|0     2|0   159k    78k   189   17:29:45
    13   1039   1001     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:18.7%          0       0|0    27|0   221k    99k   189   17:29:46
    18    774    773     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:14.2%          0       0|0    12|0   174k    80k   189   17:29:47
    13    730    704     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:15.6%          0       0|0    23|0   158k    80k   189   17:29:48
    18    773    773     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:18.8%          0       8|0     3|0   176k   102k   189   17:29:49
    16    804    743     *0       0     1|0       0  1.06g  3.37g  2.34g      0  pms:16.4%          0       0|0    55|0   173k   126k   189   17:29:50
    11    860    760     *0       0     1|0       0  1.06g   3.4g  2.34g      0  pms:11.0%          0     256|0     1|0   160k   122k   250   17:29:52
    17   1860   1127     *0       0     1|0       0  1.06g  3.73g  2.35g      0   pms:9.5%          0     535|0    43|0   321k   627k   569   17:29:54
    20   1238    617     *0       0     1|0       0  1.06g  3.64g  2.34g      0  pms:10.4%          0     355|0   143|0   253k   684k   490   17:29:56
    39   1626   1269     *0       0     1|0       0  1.06g  3.47g  2.34g      0  pms:16.4%          0       0|0    16|0   408k   616k   285   17:29:57

对查询的的关键字也做了索引,是我并发太高了吗?还是有什么需要优化的地方?

加载中
0
Joy1024
Joy1024

mongostat来看, 你的mongo查询和修改比较大. locked比较大(15%左右). 

应该操作同一个表(pms),或者同一条记录比较频繁.

所以瓶颈应该在写锁上. 建议优化代码, 可以提高处理速度.

"pms" : { "timeLockedMicros" : { "r" : 742008234676,
        "w" : 3889626469 },//写锁执行时间:(16988400383185/1000000/60)64分钟
      "timeAcquiringMicros" : { "r" : 13712412775800,
        "w" : 16988400383185 } } },//等待获取写的锁时间:     (16988400383185/1000000/60)283140分钟



返回顶部
顶部