Home Database Mysql Tutorial Comparative analysis between Mongodb and MySQL

Comparative analysis between Mongodb and MySQL

Dec 21, 2018 am 10:33 AM
mongodb mysql

The content of this article is about the comparative analysis between Mongodb and MySQL. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In the data stored in the database, there is a special key value called primary key, which is used to uniquely identify a record in the table. In other words, a table cannot have multiple primary keys, and the primary key cannot be null. Whether it is MongoDB or MySQL, there is a definition of primary key .
For MongoDB, the primary key is called "_id". When generating data, if the user does not actively assign a primary key to it, MongoDB will automatically generate a randomly assigned value for it.

In MySQL, the designation of the primary key is defined by specifying PRIMARY KEY when MySQL inserts data. When the primary key is not specified, another tool - the index, is equivalent to replacing the function of the primary key. The index can be empty or have duplicates. There is another type of index that does not allow duplicates, called a unique index. If neither a primary key nor an index is specified, MySQL will automatically create one for the data.

Storage speed comparison

1. The average insertion rate of the database: MongoDB does not specify _id insertion> MySQL does not specify primary key insertion> MySQL specifies primary key insertion> MongoDB specifies _id for insertion.

2. MongoDB has a big difference in insertion speed between specifying _id and not specifying _id, but the difference in MySQL is much smaller.

Analysis:

1. When specifying _id or primary key, the two databases must process the index value when inserting, and find whether the same value exists in the database. key value, which will slow down the rate of insertion.

2. In MongoDB, specifying index insertion is much slower than not specifying it. This is because the _id value of each piece of data in MongoDB is unique. When data is inserted without specifying _id, its _id is automatically calculated and generated by the system. MongoDB uses computer characteristics, time, process ID and random numbers to ensure that the generated _id is unique. When inserting by specifying _id, MongoDB needs to check whether this _id is available every time it inserts a piece of data. When there are too many data items in the database, the query overhead of this step will slow down the insertion speed of the entire database.

3. MongoDB will fully use the system memory as cache, which is a very excellent feature. Our test machine has 64G of memory. When inserting, MongoDB will try its best to persist the data to the hard disk after the memory is almost full of data. This is also the reason why MongoDB is far more efficient when inserting without specifying _id. But when inserting by specifying _id, when the amount of data is too large to fit in the memory, MongoDB needs to read the information from the disk into the memory to check for duplication, which makes the insertion efficiency slower.

4. MySQL is indeed a very stable database, and its efficiency is not much different whether inserting with a primary key specified or without a primary key specified.

Insertion stability analysis

Insertion stability refers to the insertion rate when a certain amount of data is inserted as the amount of data increases.

In this test, we set the scale of this indicator at 100,000, that is, the displayed data is how many pieces of data can be inserted per second during this period when 100,000 pieces of data are inserted.

First present four pictures:

1. MongoDB specifies _id insertion:

Comparative analysis between Mongodb and MySQL

##2. MongoDB does not specify _id insertion :

Comparative analysis between Mongodb and MySQL

3. MySQL specifies PRIMARY KEY for insertion:

Comparative analysis between Mongodb and MySQL##4. MySQL does not specify PRIMARY KEY for insertion:

Comparative analysis between Mongodb and MySQL

Summary:

1. The overall insertion speed is still similar to the statistics of the previous round: MongoDB does not specify _id for insertion> MySQL does not specify primary key for insertion> MySQL specifies primary key for insertion> MongoDB specifies _id for insertion.

2. It can be seen from the figure that when inserting data by specifying the primary key, when MySQL and MongoDB have different data orders of magnitude, the data inserted per second will fluctuate every once in a while. In the chart The display becomes regular glitches. When inserting data is not specified, the insertion rate is relatively average in most cases. However, as the data in the database increases, the insertion efficiency drops momentarily in a certain period of time, and then becomes stable again.

3. Overall, the rate fluctuation of MongoDB is more serious than that of MySQL, and the variance changes greatly.

4. When MongoDB inserts by specifying _id, when more data is inserted, the insertion efficiency drops significantly. In the other three insertion tests, the insertion rate is fixed at a standard most of the time from the beginning to the end.

Analysis:

1. The glitch phenomenon is because when too much data is inserted, MongoDB needs to write the data in the memory to the hard disk, and MySQL needs to re- Sub-table. These operations are performed automatically whenever the data in the database reaches a certain level, so there will be an obvious glitch every once in a while.

2. After all, MongoDB is still a new thing, and its stability is not as good as MySQL, which has been used for many years.

3. When MongoDB inserts specified _id, its performance drops significantly.

4. When the size of data read is not large, MongoDB's query speed is really unparalleled, far behind MySQL.

5. When the amount of queried data gradually increases, the query speed of MySQL decreases steadily, while the query speed of MongoDB fluctuates somewhat.

Analysis:

1. If MySQL has not been query optimized, its query speed should not be compared with MongoDB. MongoDB can make full use of the system's memory resources. Our test machine has 64GB of memory. The larger the memory, the faster MongoDB's query speed. After all, the I/O efficiency of disk and memory is not of the same order of magnitude.

2. The query data in this experiment is also randomly generated, so the probability that all the data to be queried is stored in MongoDB's memory cache is very small. When querying, MongoDB needs to interact with the data in memory and disk multiple times in order to find it, so its query rate depends on the number of interactions. There is a possibility that although the amount of data to be queried is larger, this randomly generated data is fetched from the disk by MongoDB in a smaller number of times. Therefore, the average query speed is faster. From this point of view, MongoDB's query speed fluctuations are also within a reasonable range.

3. There is no doubt about the stability of MySQL.

Conclusion

1. Compared with MySQL, MongoDB database is more suitable for task models with heavy reading operations. MongoDB can fully utilize the memory resources of the machine. If the machine has abundant memory resources, MongoDB's query efficiency will be much faster.

2. When inserting data with "_id", MongoDB's insertion efficiency is actually not high. If you want to make full use of MongoDB performance, it is recommended to insert without "_id" and then index the relevant fields for query.

3. MongoDB is suitable for those demand models where the specific data format of the database is unclear or the database data format changes frequently, and it is very friendly to developers.

4. MongoDB officially comes with a distributed file system, which can be easily deployed on a server cluster. There is a concept of Shard in MongoDB, which is convenient for server sharding. With each additional Shard, MongoDB's insertion performance will nearly double, and the disk capacity can be easily expanded.

5. MongoDB also comes with support for the map-reduce computing framework, which is also very convenient for data statistics.

Defects of MongoDB

1. Weak transaction relationship support. This is also a common flaw of all NoSQL databases, but NoSQL is not designed for transaction relationships, and specific applications are still in demand.

2. The stability is somewhat lacking, as can be seen from the above test.

3. On the one hand, MongoDB is convenient for developers, but on the other hand, it places considerable demands on operation and maintenance personnel. There is no mature MongoDB operation and maintenance experience in the industry, and the storage format of data in MongoDB is also very random. Issues such as these are a test for operation and maintenance personnel.

The above is the detailed content of Comparative analysis between Mongodb and MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to sort mongodb index How to sort mongodb index Apr 12, 2025 am 08:45 AM

Sorting index is a type of MongoDB index that allows sorting documents in a collection by specific fields. Creating a sort index allows you to quickly sort query results without additional sorting operations. Advantages include quick sorting, override queries, and on-demand sorting. The syntax is db.collection.createIndex({ field: <sort order> }), where <sort order> is 1 (ascending order) or -1 (descending order). You can also create multi-field sorting indexes that sort multiple fields.

How to set mongodb command How to set mongodb command Apr 12, 2025 am 09:24 AM

To set up a MongoDB database, you can use the command line (use and db.createCollection()) or the mongo shell (mongo, use and db.createCollection()). Other setting options include viewing database (show dbs), viewing collections (show collections), deleting database (db.dropDatabase()), deleting collections (db.<collection_name>.drop()), inserting documents (db.<collecti

The Power of MongoDB: Data Management in the Modern Era The Power of MongoDB: Data Management in the Modern Era Apr 13, 2025 am 12:04 AM

MongoDB is a NoSQL database because of its flexibility and scalability are very important in modern data management. It uses document storage, is suitable for processing large-scale, variable data, and provides powerful query and indexing capabilities.

MongoDB advanced query skills to accurately obtain required data MongoDB advanced query skills to accurately obtain required data Apr 12, 2025 am 06:24 AM

This article explains the advanced MongoDB query skills, the core of which lies in mastering query operators. 1. Use $and, $or, and $not combination conditions; 2. Use $gt, $lt, $gte, and $lte for numerical comparison; 3. $regex is used for regular expression matching; 4. $in and $nin match array elements; 5. $exists determine whether the field exists; 6. $elemMatch query nested documents; 7. Aggregation Pipeline is used for more powerful data processing. Only by proficiently using these operators and techniques and paying attention to index design and performance optimization can you conduct MongoDB data queries efficiently.

See all articles