V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
vlike
V2EX  ›  MongoDB

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

  •  
  •   vlike · Oct 23, 2015 · 4871 views
    This topic created in 3838 days ago, the information mentioned may be changed or developed.
    数据量也就 300W 左右。

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

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

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

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


    那么

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

    如果姿势没问题的话,那 RockMongo 管理大的集合时是不是都有这个问题?
    3 replies    2015-10-23 17:51:09 +08:00
    nullcc
        1
    nullcc  
       Oct 23, 2015
    find({})是全集合扫描了, explain()一下就能看到 nscanned 字段是整个集合的总文档数,慢是正常的,有索引,一般 B/B+ tree 搜索,时间复杂度肯定要大大小于全文档扫描
    nullcc
        2
    nullcc  
       Oct 23, 2015
    上面说的有点问题,我没加 limit , shell 上跑貌似没 LZ 说的问题, 200W 左右数据量
    vlike
        3
    vlike  
    OP
       Oct 23, 2015
    @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": ""
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6013 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 01:57 · PVG 09:57 · LAX 18:57 · JFK 21:57
    ♥ Do have faith in what you're doing.