search
HomeDatabaseMysql TutorialMongodb使用小结

Mongodb使用小结

Jun 07, 2016 pm 04:34 PM
mongodbusediscussquestion

以下讨论的问题,有一个前提,就是要保存的数据大于内存容量。否则你可无视之。。。 索引占用的空间有时候超出你的想象。 即使集合中只有一个默认的索引 _id, 1 亿条记录,索引占用超过 5G 。如果内存不足以保存索引,赶紧加内存吧 ~~~ 查询或者更新的速度,

        以下讨论的问题,有一个前提,就是要保存的数据大于内存容量。否则你可无视之。。。

        索引占用的空间有时候超出你的想象。即使集合中只有一个默认的索引_id, 1亿条记录,索引占用超过5G。如果内存不足以保存索引,赶紧加内存吧~~~

Mongodb使用小结

         查询或者更新的速度,有时候与文档数量的关系并不大。我在网上看到的最多的是,mongodb的速度与文档数量有关,与文档所占用空间的关系谈的人不多。我尝试在一台16G的机器上,添加100w文档,索引占用的空间大概100M不到。每个文档大小大概为50k, 100w的数据量大概占用的磁盘空间是50G,然后随机的update或者find_one100w个文档,update操作不会改变原有记录大小。原本以为才这点数据,mongodb应该像火箭一样飞起来,结果出乎意料,速度慢的要死,每秒大概只能更新50条记录左右,通过iostat或者mongostat查询,你会发现磁盘像疯了一样的转,好像吃了*,停也停不下来。想想我在sqlite中,100w记录量,查询的速度都比这要快。为什么会这么慢?mongodb也不过如此,可能还不如自己写的程序来的快?真的吗?

        了解一些b-tree的知识对使用mongodb或者其他关系数据库有好处。但索引不是万能,别以为充分利用了索引就以为mongodb会像火箭一样飞起来,有时候他会比蜗牛爬的还慢。应该根据业务需求,充分考虑数据在磁盘上保存的顺序和索引的关系,合理的设计索引。以前在学sqlserver的时候,书上说主键很重要,因为数据在磁盘上保存的顺序就是按主键的顺序来的,好像一本新华字典,书上的字按拼音的顺序保存,虽然我们也可以按部首去查询某个字,但要像获取所有”a”开头的汉字,总比获取所有“亻”的汉字要快的多。

        如果热点数据在内存中,查询与更新操作非常快,亿级数据,单实例不分片,每妙能处理上千次查询或者更新操作。否则,你的磁盘会转个不停,而且非常慢。即使充分使用了索引,因为数据不在内存中,操作系统需要先卸载部分数据腾出内存空间(如果内存不够的话)去映射磁盘上的数据。这个过程磁盘会疯一样的转。

         Mongodb的内存管理是交给操作系统的,即使mongodb重启,系统可能并不会立即释放系统cache,这个时候,如果热点数据还没有被系统卸载掉,查询的速度还是非常快的。这常常会给人以假象,mongodb快。。

        总之,索引与热点数据有多大,就给mongodb分配多大的内存。如果主要是保存数据,那么只要磁盘足够大,mongodb都可以把数据塞进去。

         Last: good luck…

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),