V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
vlike
V2EX  ›  MongoDB

db.xxx.find({}).limit(10) 非常慢,正确姿势是啥样的?

  •  
  •   vlike · 2015-10-23 11:06:31 +08:00 · 4167 次点击
    这是一个创建于 3107 天前的主题,其中的信息可能已经有所发展或是发生改变。
    数据量也就 300W 左右。

    开始发现这个问题因为 RockMongo 用不了,

    打开 collection 时每次都是超时,估计他要列出数据时执行了上述的命令导致。

    在 CLI 里试过执行 db.xxx.find({}).limit(10),确实非常慢

    但是数据有加索引, db.xxx.find({type:123}).limit(10) 这样是非常快的


    那么

    db.xxx.find({}).limit(10) 非常慢是因为我的哪个姿势不正确?

    如果姿势没问题的话,那 RockMongo 管理大的集合时是不是都有这个问题?
    3 条回复    2015-10-23 17:51:09 +08:00
    nullcc
        1
    nullcc  
       2015-10-23 11:47:04 +08:00
    find({})是全集合扫描了, explain()一下就能看到 nscanned 字段是整个集合的总文档数,慢是正常的,有索引,一般 B/B+ tree 搜索,时间复杂度肯定要大大小于全文档扫描
    nullcc
        2
    nullcc  
       2015-10-23 11:54:29 +08:00
    上面说的有点问题,我没加 limit , shell 上跑貌似没 LZ 说的问题, 200W 左右数据量
    vlike
        3
    vlike  
    OP
       2015-10-23 17:51:09 +08:00
    @nullcc

    应该是数据库有问题,但我却查不出什么问题。

    我测试时开了两个分片 A , B 。他们的数据量相当各 150W 左右,分别在两个配置一样的虚拟机内分别安装的 A 跟 B 。

    B 执行上述命令很快返回,但 A 要 30 秒左右,并且 CPU 很高。

    试过 db.repairDatabase(),不过没有效果。

    开启了慢查询记录,但似乎我看不出什么有用的信息:



    {
    "op": "query",
    "ns": "Book.test",
    "query": {

    },
    "ntoreturn": 1,
    "ntoskip": 0,
    "nscanned": 0,
    "nscannedObjects": 1,
    "keyUpdates": 0,
    "writeConflicts": 0,
    "numYield": 1,
    "locks": {
    "Global": {
    "acquireCount": {
    "r": NumberLong(4)
    }
    },
    "Database": {
    "acquireCount": {
    "r": NumberLong(2)
    }
    },
    "Collection": {
    "acquireCount": {
    "r": NumberLong(2)
    }
    }
    },
    "nreturned": 1,
    "responseLength": 67,
    "millis": 35616,
    "execStats": {
    "stage": "PROJECTION",
    "nReturned": 1,
    "executionTimeMillisEstimate": 35340,
    "works": 2,
    "advanced": 1,
    "needTime": 1,
    "needFetch": 0,
    "saveState": 1,
    "restoreState": 1,
    "isEOF": 0,
    "invalidates": 0,
    "transformBy": {
    "text": 0
    },
    "inputStage": {
    "stage": "COLLSCAN",
    "filter": {
    "$and": [

    ]
    },
    "nReturned": 1,
    "executionTimeMillisEstimate": 35340,
    "works": 2,
    "advanced": 1,
    "needTime": 1,
    "needFetch": 0,
    "saveState": 1,
    "restoreState": 1,
    "isEOF": 0,
    "invalidates": 0,
    "direction": "forward",
    "docsExamined": 1
    }
    },
    "ts": ISODate("2015-10-23T06:06:39.458Z"),
    "client": "127.0.0.1",
    "allUsers": [

    ],
    "user": ""
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3237 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:51 · PVG 21:51 · LAX 06:51 · JFK 09:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.