10 MongoDB points of use

Source: Internet
Author: User
Tags lowercase

Turn from: 10 MongoDB using the main points from the MongoDB phased technical summary extracted and collated the 10 points that are helpful to everyone: 1.mongodb table names and field names are unified in lowercase lettersMongoDB is the default case-sensitive, in order to avoid the previous case sensitivity encountered under MySQL cause program Access frequently error, establish the specification, MongoDB table name and field name in lowercase letters. 2. Shorten the length of the field name as much as possible MongoDB's schema free causes every data to store its key and its properties, which leads to a lot of redundancy in the data. Developers may consider that the key name, designed from legibility, is basically designed to be literal, which results in a long key, which takes up a lot of space for the corresponding data store. So, maintain a dictionary in your program and reduce the length of the key as much as possible. For example: Static final string content = "Content"; static final String content_type = "CType"; static final String content_length = "Clen"; 3. Remember that MongoDB queries can only use one index at a timeFor more complex table structures, you may be able to use federated indexes frequently. But remember: 1) The maximum number of indexes for MongoDB single table is 64. 2) The more indexes, inserting or modifying records will cause MongoDB to become slower. Write locks block read requests, write more slowly, block read requests more, and block longer. So As the index becomes more and more, you may need to look at the rationality of the table structure design4. Settings for client connection number sizeMongodb-java-driver connection pool, the current observation from the situation is that the application is opened according to the settings of the connectionsperhost variable, establish a full connection, and then provide to the program use, and once one of the connection to the database access failed, The connection from the entire connection pool to this database is emptied and the connection is re-established. and MongoDB to interrupt the garbage cleanup work is lazy passive cleanup method, if the driver side configured too large number of connections, once a reconnection, it will cause the MONGO server side piled up a large number of garbage connections and corresponding data, resulting in the host resource exhaustion. Recommendation: MongoDB-driven connection pool size settings should generally control about 100. (Refer to Reading: PHP-FPM mode of the dreaded Mongodb-php-driver connection pool uncontrolled connection problem) 5. Instance SeparationMongoDB access to the database is all locked. A shared lock is set as a query request. The data modification request sets the global exclusive lock and is an exclusive lock at the instance level. A write lock blocks a read request and, if held for a long time, blocks the read request for the entire instance. Recommendations: 1) different applications should not share the same instance to prevent blocking each other! 2) If the server resources are insufficient, share the same instance, to ensure the same read and write characteristics, such as read more write lessTo prevent a write multiple application from blocking read requests. (Comment: The previous version of MongoDB (PRE 2.0) has a global write lock, which has been significantly improved in the 2.0 release and has been further enhanced in the current 2.2 release. MongoDB 2.2 Using a database-level lock is a big step forward on this issue. so a person with MongoDB 2.2 can ignore this entry. 6. MongoDB performance indicators that need to be focusedFocus on the main performance indicators: 1) Faults: Shows the number of MongoDB page faults per second, this is the MongoDB mapping to the virtual address space, not the physical memory. If this value is high, it may mean that the machine does not have enough memory to store the data and index. 2) Flushes: How many times per second Fsync, shows how many times the data was flushed into the disk. 3) Locked: Write lock. 4) IDX miss: Index Miss scale. 5) QR | QW: Request Queue Length for read-write locks. 6) Conn: the number of connections that are currently established. 7. Serious space debris issuesMongoDB If the data changes very frequently, there will be more serious space debris problems, performance in the disk file expansion and actual data volume does not match, memory is not enough, index hit rate is low, query efficiency is reduced. Defragmentation, the version we are currently using is not a very effective method. You can use Db.repairedatabase () to organize your database, which is a very slow process. In the case of Master/slave mode, it is equivalent to performing a master-slave switchover and then starting from the new library. In the case of a Replset schema, you can stop the database and then delete the data directory and fully synchronize the data from the new replication group, taking into account the size of the oplog. A general step: 1 First Call Rs.freeze (1200), will each not let it become primary machine let it in 1200 seconds cannot become primary (this step can also not do); 2) will primary Stepdown, no unexpected new primary will get up; 3) Kill the original primary, 4) Delete all data (call repair very slow, really as good as Kill again), 5) re-start the original primary process; 6) Complete the entire rebuild of the whole replication group in this loop. 8. Connection Pool Writerconcern mode selectionSome applications are configured with the Writerconcern.fsync_safe mode, which means that when inserting data or updating data, the client requires MongoDB to write the updated data to disk and return the updated information to the program. If the application access pressure is high, MongoDB will be unresponsive, and may be suspended animation. For this scenario, you need to assess the consistency of your data requirements and make appropriate adjustments. We generally recommend that you turn off this option. (Comment: This writerconcern.fsync_safe mode is turned off when Liuquipo's business center is optimized) 9. Attention to detail during development1) when updating a piece of data, check it out and update it to reduce the lock time .; 2) Only the fields that are really needed are select; 3) only queries that return very few results are indexed, or too much data is loaded, which is slower than not using indexes! 4) When the attribute is more, establishing a hierarchical relationship can improve the query efficiency, otherwise each record will have to go through again to find the attributes. (Comment: It seems that thesubdocument stored in Array form ) 5) Skip+limit Turn the page, the more slowly toward the back. The more reliable way is to find out the last ID, page flipping without skip:

last_row_id = ObjectId (' .... ');
Db.activity_stream->find ({_id:{$lt: last_row_id},user_id:20}). Sort ({_id:-1}). Limit (10);

10. Selection of hardware Resources
Virtual machines can isolate resources very well and can be dynamically extended.
We recommend that MongoDB deployments take the form of virtual machines, where each virtual machine deploys an instance to spread the nodes across different physical machines, balancing the I/O between virtual machines based on the early predictions of the application. Reference resources: 1) Horizonhyg,2012,mongodb write security--getlasterror;2) Horizonhyg,2012,java connection mongo;3) 55 Best Practices Series: MongoDB Best Practices 2 pieces of the gift chart:

10 MongoDB points of use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.