Mongodb使用小结
以下讨论的问题,有一个前提,就是要保存的数据大于内存容量。否则你可无视之。。。 索引占用的空间有时候超出你的想象。 即使集合中只有一个默认的索引 _id, 1 亿条记录,索引占用超过 5G 。如果内存不足以保存索引,赶紧加内存吧 ~~~ 查询或者更新的速度,
以下讨论的问题,有一个前提,就是要保存的数据大于内存容量。否则你可无视之。。。
索引占用的空间有时候超出你的想象。即使集合中只有一个默认的索引_id, 1亿条记录,索引占用超过5G。如果内存不足以保存索引,赶紧加内存吧~~~
查询或者更新的速度,有时候与文档数量的关系并不大。我在网上看到的最多的是,mongodb的速度与文档数量有关,与文档所占用空间的关系谈的人不多。我尝试在一台16G的机器上,添加100w文档,索引占用的空间大概100M不到。每个文档大小大概为50k, 100w的数据量大概占用的磁盘空间是50G,然后随机的update或者find_one这100w个文档,update操作不会改变原有记录大小。原本以为才这点数据,mongodb应该像火箭一样飞起来,结果出乎意料,速度慢的要死,每秒大概只能更新50条记录左右,通过iostat或者mongostat查询,你会发现磁盘像疯了一样的转,好像吃了*,停也停不下来。想想我在sqlite中,100w记录量,查询的速度都比这要快。为什么会这么慢?mongodb也不过如此,可能还不如自己写的程序来的快?真的吗?
了解一些b-tree的知识对使用mongodb或者其他关系数据库有好处。但索引不是万能,别以为充分利用了索引就以为mongodb会像火箭一样飞起来,有时候他会比蜗牛爬的还慢。应该根据业务需求,充分考虑数据在磁盘上保存的顺序和索引的关系,合理的设计索引。以前在学sqlserver的时候,书上说主键很重要,因为数据在磁盘上保存的顺序就是按主键的顺序来的,好像一本新华字典,书上的字按拼音的顺序保存,虽然我们也可以按部首去查询某个字,但要像获取所有”a”开头的汉字,总比获取所有“亻”的汉字要快的多。
如果热点数据在内存中,查询与更新操作非常快,亿级数据,单实例不分片,每妙能处理上千次查询或者更新操作。否则,你的磁盘会转个不停,而且非常慢。即使充分使用了索引,因为数据不在内存中,操作系统需要先卸载部分数据腾出内存空间(如果内存不够的话)去映射磁盘上的数据。这个过程磁盘会疯一样的转。
Mongodb的内存管理是交给操作系统的,即使mongodb重启,系统可能并不会立即释放系统cache,这个时候,如果热点数据还没有被系统卸载掉,查询的速度还是非常快的。这常常会给人以假象,mongodb很快。。。
总之,索引与热点数据有多大,就给mongodb分配多大的内存。如果主要是保存数据,那么只要磁盘足够大,mongodb都可以把数据塞进去。
Last: good luck…
原文地址:Mongodb使用小结, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

The main tools for connecting to MongoDB are: 1. MongoDB Shell, suitable for quickly viewing data and performing simple operations; 2. Programming language drivers (such as PyMongo, MongoDB Java Driver, MongoDB Node.js Driver), suitable for application development, but you need to master the usage methods; 3. GUI tools (such as Robo 3T, Compass) provide a graphical interface for beginners and quick data viewing. When selecting tools, you need to consider application scenarios and technology stacks, and pay attention to connection string configuration, permission management and performance optimization, such as using connection pools and indexes.
