10 key points for using MongoDB

Source: Internet
Author: User
Tags mongodb driver
I have extracted and sorted out ten key points that will help you with mongodb's phased technical summary: 1. mongodb table names and field names should all use lower-case lettersMongodb is case-sensitive by default. To avoid frequent program access errors caused by Case sensitivity in mysql, the table name and field name of mongodb are named with lowercase letters. 2. shorten the length of field names as much as possible Mongodb schema free causes each data to store its key and attributes, which leads to a large amount of redundancy.Developers may consider that the key names designed based on ease of use are basically designed according to the literal meaning, which leads to a long key, and the corresponding data storage occupies a lot of space. Therefore, you can maintain a set of dictionaries in your program to minimize the length of the key. For example, static final String CONTENT = "content"; static final String CONTENT_TYPE = "ctype"; static final String CONTENT_LENGTH = "clen "; 3. Remember, only one index can be used for mongodb queries at a time.For complex table structures, you may frequently use the Union index. But remember: 1) the maximum number of indexes in a single mongodb table is 64. 2) The more indexes, the more slow mongodb will be caused by inserting or modifying records. Write locks block read requests. The slower the write, the more read requests are blocked and the longer the blocking time. So, When more indexes are added, you may need to check the rationality of the table structure design.. 4. Set the number of client connectionsMongodb-java-driver connection pool. Currently, it is observed that when the application is enabled, all connections are established based on the connectionsPerHost variable settings and then provided to the program for use, once the access to a database fails, the connection from the entire connection pool to the database is cleared and the connection is established again. Mongodb is a lazy and passive cleanup method for the spam of disconnected connections. If the number of connections configured on the driver side is too large, this will cause the mongo server to pile up a large amount of junk connections and corresponding data, leading to the depletion of Host resources. Suggestion: the size of the mongodb driver connection pool should be set to around 100. (Read: unlimitedly connected to the MongoDB-PHP-Driver connection pool in PHP-FPM mode) 5. instance separationMongodb locks all access to the database. A shared lock is set for a query request. The global exclusive lock is set for data modification requests and is an instance-level exclusive lock. The write lock blocks read requests. If a write lock is held for a long time, the read requests of the entire instance are blocked. Suggestion: 1) Different applications should not share the same instance to prevent mutual blocking.! 2) If the server resources are insufficient, share the same instance, Make sure that the read/write features are the same, for example, both read and write operations must be less.To prevent multiple write applications from blocking read requests. (Comment: the old version of MongoDB (pre 2.0) has a global write lock, which has been significantly improved in version 2.0, it has been further enhanced in the current 2.2 version. MongoDB 2.2 uses database-level locks to take a big step on this issue. Therefore, users who use MongoDB 2.2 can ignore this entry.) 6. mongodb performance indicators to be focused onFocus on major performance indicators: 1) Faults: displays the number of page Faults of mongodb per second. This is the ing of mongodb to virtual address space rather than physical memory. If this value is too high, it may mean that the machine does not have enough memory to store data and indexes. 2) Flushes: the number of fsync operations per second, and the number of times the data is refreshed to the disk. 3) locked: Write lock. 4) idx miss: Index miss rate. 5) qr | qw: length of the read/write lock Request queue. 6) conn: number of established connections. 7. Serious space fragmentation problemsIf mongodb frequently modifies data, it may cause serious space fragmentation problems, such as inconsistent Disk File expansion and actual data volume, insufficient memory, low index hit rate, and low query efficiency. Currently, the version we use is not very effective. You can use db. repaireDatabase () to sort out the database. This process is very slow. In master/slave Mode, a master-slave switchover is performed, and the slave database is created again. In a replSet architecture, you can stop the database, delete the data directory, and synchronize all data from the replication group. In this case, you must consider the oplog size. A general step: 1) Call rs first. freeze (1200) will make every machine that does not want to make it a primary so that it cannot become a primary within 1200 seconds (this step can also not be done); 2) stepDown the primary, new primary will get up without an accident; 3) kill the original primary; 4) delete all data (Calling repair is slow, so it is better to get rid of it again); 5) restart the original primary process; 6) rebuild the entire replication group in this cycle. 8. Select the connection pool WriterConcern ModeSome applications have configured WriterConcern. FSYNC_SAFE mode. This configuration means that when the client inserts or updates data, mongodb must write the updated data to the disk and return the updated information to the program. If the application is under high access pressure, mongodb will be slow and may be suspended. In this case, we need to assess the data consistency requirements and make appropriate adjustments. We generally recommend that you disable this option. (Comment: when Liu kuibo's business center is optimized, the WriterConcern. FSYNC_SAFE mode is disabled) 9. Development details1) When updating a piece of data, the lock time will be reduced after the data is checked out and updated.; 2) select only required fields; 3) Indexes are used only for queries that return a small number of results. Otherwise, too much data is loaded, Which is slower than no index.! 4) when there are many attributes, establishing a hierarchical relationship can improve the query efficiency. Otherwise, each record must go through to find the desired attribute. (Comment: It seems that the subdocument is stored as an Array.) 5. Flip the page with skip and limit, and the slower the page is. The more reliable method is to first find the last id, without skip when turning pages:

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 and dynamically expand resources.
We recommend that you deploy apsaradb for mongodb using virtual machines. One instance is deployed for each virtual machine so that nodes are distributed across different physical machines. Based on the pre-prediction of applications, balance the I/o between virtual machines. Reference resources: 1) horizonhyg, 2012, Mongodb write Security Mechanism -- GetLastError; 2) horizonhyg, 2012, java connection mongo; 3) 55 Best Practices Series: MongoDB Best Practices Image 2:
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.