The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zsj1029

golang mongodb mgo 封装 query 查询慢,请教

  •  
  •   zsj1029 · Nov 7, 2022 · 1654 views
    This topic created in 1292 days ago, the information mentioned may be changed or developed.

    db:statistics collection:uart_logs 32w 条数据,每条多层文档结构 serial_server 字段 asc 索引

    手写 query db.uart_logs.find({serial_server:"10.30.40.233"}).limit(1) 查询时间 0.75s mgo 封装后 query db.uart_logs.find({ find: "uart_logs", filter: { serial_server: "10.30.40.233" }, limit: 1 , $db: "statistics" }) 查询时间 23s 这样子手写 db.collection.find({ find: "uart_logs", filter: { serial_server: "10.30.40.233" }, limit: 1 , $db: "statistics" }) 也是 1s 内

    mongodb log

    2022-11-07T14:39:15.592+0800 I COMMAND [conn6] command statistics.uart_logs command: find { find: "uart_logs", filter: { serial_server: "10.30.37.233", time: new Date(1667800800000) }, limit: 1, singleBatch: true, lsid: { id: UUID("934fd6f4-3e07-4697-b381-5e329799fbf2") }, $db: "statistics" } planSummary: COLLSCAN keysExamined:0 docsExamined:326818 cursorExhausted:1 numYields:2588 nreturned:1 queryHash:589B9D08 planCacheKey:589B9D08 reslen:61527 locks:{ ReplicationStateTransition: { acquireCount: { w: 2589 } }, Global: { acquireCount: { r: 2589 } }, Database: { acquireCount: { r: 2589 } }, Collection: { acquireCount: { r: 2589 } }, Mutex: { acquireCount: { r: 1 } } } storage:{ data: { bytesRead: 916177929, timeReadingMicros: 572959 }, timeWaitingMicros: { cache: 20 } } protocol:op_msg 12574ms

    请问各位大佬什么原因

    6 replies    2022-11-14 11:21:07 +08:00
    zsj1029
        1
    zsj1029  
    OP
       Nov 7, 2022
    远程 mongodump 三个小时 这 32w 条数据还没 dump 下来
    zsj1029
        2
    zsj1029  
    OP
       Nov 7, 2022
    sorry 搞错了,用的官方推荐的 go.mongodb.org/mongo-driver/mongo
    wumoumou
        3
    wumoumou  
       Nov 8, 2022
    向大佬学习
    tikazyq
        4
    tikazyq  
       Nov 14, 2022
    大概率是没加索引

    db.uart_logs.getIndexes()
    zsj1029
        5
    zsj1029  
    OP
       Nov 14, 2022
    @tikazyq 找到原因了,测试了下,用的 go 连接器,默认连接上了 col 集合,然后用集合的连接池,再去查其他集合就会出现无索引,几十万全部扫描。
    后面改为直连接库,然后根据实际使用再连接 col 集合查询就没这个问题了,感谢提醒
    tikazyq
        6
    tikazyq  
       Nov 14, 2022
    @zsj1029 一般出现这种 slow query 的问题,首先考虑是不是索引没加,如果加了没使用,要么是 query 写法问题,要么是代码错误。主流数据库发展这么多年了,大小问题都优化过了,所以出问题首先怀疑自己代码通常是正解😂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   915 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    ♥ Do have faith in what you're doing.