MongoDB内部实现索引编制吗? [英] MongoDB Internal implementation of indexing?

查看:66
本文介绍了MongoDB内部实现索引编制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学到了很多有关建立索引并从中找到一些东西的知识 此处.

索引支持在MongoDB中高效执行查询.没有索引,MongoDB必须执行集合扫描,即扫描集合中的每个文档,以选择与 查询语句.如果存在用于查询的适当索引,则MongoDB可以使用该索引来限制它必须检查的文档数.

但是我仍然有一些问题:

  1. 使用(createIndex)创建索引时,记录始终存储在 内存?
  2. 是否每次我的应用程序都需要创建索引 要重启吗?
  3. 在默认ID(_id)的情况下会发生什么.始终存储在RAM中.
  4. _id是默认索引,这意味着对于特定集合,所有记录始终存储在RAM中吗?

如果我错了,请帮助我. 谢谢.

解决方案

我认为,您的想法是将索引存储在RAM中.如果我说他们不是,那该怎么办.

首先,我们需要了解什么是索引,索引基本上是一个指针,用于指示该文件在磁盘上的位置.就像我们在书中建立索引一样,为了更快地访问,我们可以查看哪个主题在哪个页码上.

因此,当创建索引时,它们也存储在磁盘中,但是当应用程序运行时,基于频繁使用甚至更快的访问,它们会被加载到RAM中,但是加载和创建之间是有区别的.

加载索引也不同于将集合或记录加载到RAM.如果加载了索引,我们就知道要从磁盘上提取所有文档,这与加载所有文档并验证每个文档不同.因此索引避免了集合扫描.

创建索引是一个一次性的过程,但是文档中的每次写入都可能会更改索引,因此某些部分可能需要重新计算,因为记录可能会根据数据的变化而被改组.这就是为什么索引会使写入变慢而读取快.

再次将其视为一本书,如果您在这本书之间添加了一个新的主题,例如2页,则该主题编号之后的所有索引都需要重新计算.因此.

使用(createIndex)创建索引时,记录始终存储在RAM中 ?.

  • 否,记录没有存储在RAM中,在创建记录时会处理集合中的所有文档并创建索引表,如果文档太多,这会很费时,这是可以理解的,这就是为什么在后台创建索引的选项.

每当我的应用程序要创建索引时,是否每次都需要创建索引 重新启动?

  • 索引创建一次,您可以将其删除并再次创建,但是不会在应用程序上重新创建或数据库重新启动.对于分片环境中的大量收集而言,这将是疯狂的.

在默认ID(_id)的情况下会发生什么.始终存储在 RAM.

  • 同样,这是不正确的. _id作为索引字段出现,因此已经为空集合创建了索引,就像执行write一样,它将重新计算索引.由于它是唯一索引,因此处理会更快.

_id是默认索引,这意味着对于特定集合,所有记录始终存储在RAM中吗?

  • 所有记录仅在使用MongoDB内存引擎(我认为是企业版)时才会存储在RAM中.由于建立索引,它不会自动将记录加载到RAM.

I've learned a lot of things about indexing and finding some stuff from here.

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

But i still have some questions:

  1. While Creating index using (createIndex), is the Record always stored in RAM?
  2. Is every time need to create Index Whenever My application is going to restart ?
  3. What will Happen in the case of default id (_id). Is always Store in RAM.
  4. _id Is Default Index, That means All Records is always Store in RAM for particular collections?

Please help me If I am wrong. Thanks.

解决方案

I think, you are having an idea that indexes are stored in RAM. What if I say they are not.

First of all we need to understand what are indexes, indexes are basically a pointer to tell where on disk that document is. Just like we have indexing in book, for faster access we can see what topic is on which page number.

So when indexes are created, they also are stored in the disk, But when an application is running, based on the frequent use and even faster access they get loaded into RAM but there is a difference between loaded and created.

Also loading an index is not same as loading a collection or records into RAM. If we have index loaded we know what all documents to pick up from disk, unlike loading all document and verifying each one of them. So indexes avoid collection scan.

Creation of indexes is one time process, but each write on the document can potentially alter the indexing, so some part might need to be recalculating because records might get shuffled based on the change in data. that's why indexing makes write slow and read fast.

Again think of as a book, if you add a new topic of say 2 pages in between the book, all the indexes after that topic number needs to be recalculated. accordingly.

While Creating index Using (createIndex),Is Record always store in RAM ?.

  • No, records are not stored in RAM, while creating it sort of processes all the document in the collection and create an index sheet, this would be time consuming understandably if there are too many documents, that's why there is an option to create index in background.

Is every time need to create Index Whenever My application is going to restart ?

  • Index are created one time, you can delete it and create again, but it won't recreated on the application or DB restart. that would be insane for huge collection in sharded environment.

What will Happen in the case of default id (_id). Is always Store in RAM.

  • Again that's not true. _id comes as indexed field, so index is already created for empty collection, as when you do a write , it would recalculate the index. Since it's a unique index, the processing would be faster.

_id Is Default Index, That means All Records is always Store in RAM for particular collections ?

  • all records would only be stored in RAM when you are using in-memory engine of MongoDB, which I think comes as enterprise edition. Due to indexing it would not automatically load the record into RAM.

这篇关于MongoDB内部实现索引编制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆