mongodb中高效的集合查找和索引创建

2024-05-26 04:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大量的数据进入我的数据库,高达每秒1000个文档。当我收到每一个文档时,我会根据文档中的某个字段有效地检查该类型文档是否已经有一个集合,如果没有,我想在该集合上创建几个索引。基本上,我想知道在空集合上创建索引需要多长时间,以及是否有更快的方法来检查是否存在具有指定名称的集合。在

def insert_new_doc(json_doc):
  collection_name = json_doc["collection_name"]
  coll = tlm_db_connection[collection_name]

  # create indexes in background if the collection doesn't exist
  if tlm_db_connection.system.namespaces.find( { name : collection_name } ) == None:
      coll.ensure_index([('time_stamp', pymongo.DESCENDING)], background = True)
      coll.ensure_index([('raw_value', pymongo.DESCENDING)], background = True)
      coll.ensure_index([('time_stamp', pymongo.DESCENDING), ('raw_value', pymongo.DESCENDING)], background = True)

  coll.insert(json_doc)

这就是我的函数。如果我已经确保把'u index设置为background=True,你知道它阻止这个线程调用那个函数多长时间吗?在


Tags: name文档jsontrueindexdoccollectioninsert
1条回答
网友
1楼 · 发布于 2024-05-26 04:23:59

在空集合上创建新索引非常快,可以通过运行以下测试来度量它

function howLong(){

    var t0 = new Date().valueOf();
    db.myCollection.ensureIndex({name: 1});
    var t1 = new Date().valueOf();

    return t1 - t0;
}

在创建索引之前,EnsureIndex将阻塞。我的旧笔记本上写着0:)

同样的技术也可以用于获取mongoshell中索引的大约“背景”创建时间。在

Background indexing operations run in the background so that other database operations can run while creating the index. However, the mongo shell session or connection where you are creating the index will block until the index build is complete.

http://docs.mongodb.org/manual/core/index-creation/#behavior

如果您尽早调用ensureIndex,它将很快,即在我的机器上索引100000个项目(按用户集合的名称索引)需要大约350毫秒

随后对ensureIndex的调用(在创建之后)将立即退出(带有适当的消息),但如果可以的话,我不会这样做。(即数据库由我控制,不与其他人共享)我会做索引创建的专用线程。在

由于您的集合增长很快,而且您将创建一个索引,因此请确保它适合RAM see here,因此在插入时预聚合数据可能是值得的。在

关于检查集合是否存在,假设您的应用程序是唯一一个写入数据库的应用程序,您可以在启动时列出所有集合,并将这些信息保存在内存中。在

10gen实验室有一个有趣的项目,似乎解决了类似的问题(尽管是java代码),可能值得一看High Volume Data Feed

相关问题 更多 >