Mongo查询在大数据库上抛出超时

时间:2019-04-07 13:41:59

标签: javascript angular mongodb mongoose

我是mongo和angular的新手,我刚刚解决了一个本地mongo数据库的问题,并且能够在一个小数据库上执行查询;现在,我正在尝试在210GB的大数据库上进行查询,但是每次执行查询时,即使按1条记录进行过滤,也会出现超时。

有什么我想念的吗?

这是我正在使用的查询

let q = 'ClientID : 18481';

await Activities.find({q}, function(err, users){
            if (err) throw err;
            // object of all the users
            console.log(users);
        }, 20000);

这也是我与茉莉相关的量角器

  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 300000,
    allScriptsTimeout: 300000,
    isVerbose: true
  },

这是我得到的输出

Failures:
1) dashboard_links update time frame filter to Today
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at listOnTimeout (timers.js:327:15)
        at processTimers (timers.js:271:5)
  Message:
    MongoNetworkError: connection 0 to server.qa.test.online:27017 timed out
  Stack:
    MongoNetworkError: connection 0 to server.qa.test.online:27017 timed out
        at Socket.<anonymous> (/Users/moisessiles/Automation/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:259:7)
        at Object.onceWrapper (events.js:285:13)
        at Socket.emit (events.js:197:13)
        at Socket._onTimeout (net.js:447:8)
        at listOnTimeout (timers.js:327:15)
        at processTimers (timers.js:271:5)
  Message:
    Failed: connection 1 to server.qa.test.online:27017 timed out
  Stack:
    MongoNetworkError: connection 1 to server.qa.test.online:27017 timed out
        at Socket.<anonymous> (/Users/moisessiles/Automation/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:259:7)
        at Object.onceWrapper (events.js:285:13)
        at Socket.emit (events.js:197:13)
        at Socket._onTimeout (net.js:447:8)
        at listOnTimeout (timers.js:327:15)
        at processTimers (timers.js:271:5)
    From: Task: Run it("update time frame filter to Today") in control flow
        at UserContext.<anonymous> (/Users/moisessiles/Automation/node_modules/jasminewd2/index.js:94:19)
        at /Users/moisessiles/Automation/node_modules/jasminewd2/index.js:64:48
        at ControlFlow.emit (/Users/moisessiles/Automation/node_modules/selenium-webdriver/lib/events.js:62:21)
        at ControlFlow.shutdown_ (/Users/moisessiles/Automation/node_modules/selenium-webdriver/lib/promise.js:2674:10)
        at shutdownTask_.MicroTask (/Users/moisessiles/Automation/node_modules/selenium-webdriver/lib/promise.js:2599:53)
        at MicroTask.asyncRun (/Users/moisessiles/Automation/node_modules/selenium-webdriver/lib/promise.js:2728:9)
        at /Users/moisessiles/Automation/node_modules/selenium-webdriver/lib/promise.js:668:7
        at processTicksAndRejections (internal/process/next_tick.js:81:5)
    From asynchronous test: ```

1 个答案:

答案 0 :(得分:1)

您的实际问题是查询花费了这么长时间。查看您的查询:

 { q:  'ClientID : 18481' }

您真的需要在字段q中包含一个名称为字符串的字符串吗?您不能只是将对象构造为:

 { clientID: 18481 }

然后,您查询的是数字,这是更快的方法,它可以使数据库保持干净。这样,您还可以轻松地为该字段创建index

Activities.index({ clientID: 1 })

这会将您的搜索转换为查找

  

210GB

您如何获得如此大量的数据?尝试对数据进行结构化(如上所示),以使mongodb更加优雅地存储数据,从而减小数据库的大小。

还请记住,数据库必须在RAM中存储大量数据才能快速运行,如果您的服务器无法处理此数量,则可能需要在多个服务器上shard进行数据存储。

如果所有这些都无助于减少超时后的加载时间,则可以increase / disable the timeout