听说MongoDB性能很好,于是我们决定尝试使用MongoDB来解决我们的问题。我开始将我们在几个MySQL数据库中拥有的所有记录移动到一个单独的MongoDB集合中。这导致了一个包含2900万个文档(每个文档至少有20个字段)的集合,占用约100 GB的硬盘空间。我们决定将它们全部放在一个集合中,因为所有文档的结构相同,我们希望对所有这些文档进行查询和聚合结果。
我创建了一些索引来匹配我的查询,否则即使是简单的count()也需要很长时间。然而,像distinct()和group()这样的查询仍然需要太长时间。
例子:
// creation of a compound index
db.collection.ensureIndex({'metadata.system':1, 'metadata.company':1})
// query to get all the combinations companies and systems
db.collection.group({key: { 'metadata.system':true, 'metadata.company':true }, reduce: function(obj,prev) {}, initial: {} });
我查看了mongod日志,发现有很多类似这样的行(在执行上面的查询时):
Thu Apr 8 14:40:05 getmore database.collection cid:973023491046432059 ntoreturn:0 query: {} bytes:1048890 nreturned:417 154ms
Thu Apr 8 14:40:08 getmore database.collection cid:973023491046432059 ntoreturn:0 query: {} bytes:1050205 nreturned:414 430ms
Thu Apr 8 14:40:18 getmore database.collection cid:973023491046432059 ntoreturn:0 query: {} bytes:1049748 nreturned:201 130ms
Thu Apr 8 14:40:27 getmore database.collection cid:973023491046432059 ntoreturn:0 query: {} bytes:1051925 nreturned:221 118ms
Thu Apr 8 14:40:30 getmore database.collection cid:973023491046432059 ntoreturn:0 query: {} bytes:1053096 nreturned:250 164ms
...
Thu Apr 8 15:04:18 query database.$cmd ntoreturn:1 command reslen:4130 1475894ms
这个查询用了1475894毫秒,比我预期的要慢很多(结果列表大约有60条记录)。首先,考虑到我的集合中有大量文档,这种情况是否正常?通常在MongoDB中聚合查询是否会如此缓慢?您对如何提高性能有什么想法吗?
我在一台双核10GB内存的单机上运行mongod。
谢谢。