【发布时间】:2016-05-24 18:28:30
【问题描述】:
我使用 Sails.js 和 mongoDb(sails-mongo) 编写了一个应用程序。
首先,我决定将所有内容写入一个文档... 并且数据库在 5GB 的数据上变慢了.. “慢”表示基本查找查询在 30-50 秒内执行。
比我在多个集合中重写所有内容并添加索引.. 我的模型示例:
Markets.js
module.exports = {
attributes: {
name: {
type: 'string',
index: true
},
pairs: {
collection: 'Exchanges',
via: 'source',
}
}
};
和 Exchanges.js
module.exports = {
attributes: {
s1: {
type: "string"
},
source:{
model: "Maklers",
index: true
},
s2: {
type: "string"
},
p: {
type: 'float'
},
v1: {
type: 'float'
},
v2: {
type: 'float'
},
vb: {
type: 'float'
}
}
};
以及慢查询示例
Markets.findOne({
name: info,
sort: 'createdAt DESC',
limit: 1,
createdAt: {
'<=': aft
}
}).populateAll().exec(function(err, items) {
callback(err, items);
});
db.stats 的结果
> db.stats()
{
"db" : "stats222",
"collections" : 8,
"objects" : 36620661,
"avgObjSize" : 238.26556139988844,
"dataSize" : 8725442352,
"storageSize" : 10033258480,
"numExtents" : 63,
"indexes" : 13,
"indexSize" : 2940024192,
"fileSize" : 14958985216,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
}
你能给我什么建议? 每分钟大约有 2000 条记录..
如何提高性能? 更改数据库配置?更改索引?更改数据库?更改模型/集合配置?
我使用具有 2GB 虚拟内存的 2 核服务器.. 对不起英语不好..
【问题讨论】:
-
你到底想做什么?看起来您正在混淆查询类型;例如在 findOne 方法中使用限制不能做任何事情......
-
Actuly Limit .. 是旧查询的一部分,其中只是 Find.. 现在它被删除了..
-
部分原因可能是您的数据库所在服务器的限制;理想情况下,您需要足够的内存来保存整个数据库 + 内存中的索引,这样您就不会遇到页面错误等。其中一部分将调用 .populateAll() 方法;这真的很昂贵,尤其是当您从初始 .find() 步骤返回大量记录时。它可能会在您的查询中添加 数千个 额外的查询和迭代...
标签: javascript node.js mongodb sails.js sails-mongo