mongodb
数据库 系统性能、负载和容量
Cursor Isolation
Because the cursor is not isolated during its lifetime, intervening write operations on a document may result in a cursor that returns a document more than once if that document has changed. To handle this situation, see the information on snapshot mode
为了实现低延迟的数据库操作MongoDB中广泛使用了RAM。在MongoDB中,所有的数据都是通过内存映射文件读取和操作的。从内存中读取数据是使用纳秒来度量的,而从磁盘中读取数据则是使用毫秒度量的,所以从内存中读取数据几乎比从磁盘中读取要快了十万倍。
MongoDB能够使用本地附加的存储和固态硬盘(SSD)
Sharding可以让MongoDB的部署解决单个服务器的硬件限制而不需要增加应用程序的复杂性,解决的硬件限制包括RAM和磁盘I/O的瓶颈。
The maximum BSON document size is 16 megabytes.
索引优化查询
All indexes in MongoDB are B-tree indexes
A single collection can have no more than 64 indexes;There can be no more than 31 fields in a compound index;
The total size of an index entry, which can include structural overhead depending on the BSON type, must be less than 1024 bytes.
By default, all collections have an index on the _id field
By default, creating an index blocks all other operations on a database. When building an index on a collection, the database that holds the collection is unavailable for read or write operations until the index build completes. Any operation that requires a read or write lock on all databases (e.g. listDatabases) will wait for the foreground index build to complete.
可以考虑background模式执行建立索引
MongoDB allows you to specify a unique constraint on an index. These constraints prevent applications from inserting documents that have duplicate values for the inserted fields. Additionally, if you want to create an index on a collection that has existing data that might have duplicate values for the indexed field, you may choose to combine unique enforcement with duplicate dropping.
原子操作是记录级别的:In MongoDB, write operations are atomic at the document level, and no single write operation can atomically affect more than one document or more than one collection。 If the application can tolerate non-atomic updates for two pieces of data, you can store these data in separate documents.
数据存储结构
MongoDB 默认的数据目录是data/db,它负责存储所有mongodb的数据文件,在mongoDB中每个数据库都包含一个.ns和一些数据文件,而且这些数据文件会随着数据的增多越来越多,则: 如果系统中有一个叫foo的数据库,那么构成foo这个数据库的文件就会有foo.ns ,foo.0,foo1,foo.2等。
Mongodb内部有预分配空间的机制,每个预分配的文件都用0填充,由于有了这个机制,
mongoDB始终保存额外的空间和空闲的文件,这对系统数据突然暴增时减缓磁盘压力有很大好处.
由于数据量的不断增加,mongoDB每新分配一次,大小都会是上一个文件大小的2倍,最大2G.这种机制保证系统数据较小时 不会浪费太多空间,系统数据较多时 也有相应预留空间。
mongoDB命名空间
每张表都有命名空间,每个索引也有对应的命名空间,这些命令空间的元数据都存在.ns文件中
Capped collections are fixed-size collections that support high-throughput operations that insert, retrieve, and delete documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection. 这个东西保持数据插入的顺序FIFO,且集合大小固定,会覆盖老的。
Expire Data from Collections by Setting TTL
new in 2.2
When using TTL indexes on replica sets, the TTL background thread only deletes documents on primarymembers. However, the TTL background thread does run on secondaries. Secondary members replicate deletion operations from the primar
The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection after they expire but before the background task runs or completes.
Bulk Write Operations
bulk 支持批量的写操作,分串行和并行两种模式。