MySQL and MongoDB, how to choose technology?

Hello, everyone, I am Zhang Zhang, the author of the public account “The Road to Architecture Improvement”.

Introduction

Under normal circumstances, when considering how to make technical selections between MySQL and MongoDB, you must have encountered access problems similar to unstructured data JSON, otherwise everyone would just start using MySQL.

Why should you pay attention to MongoDB?

The picture below shows the ranking statistics of DB-Engines databases in October 2023. You can see that MongoDB ranks 5th overall and 1st among Nosql databases.

6834a824d60ab619f45fc11dcc355875.jpeg

Since you want to make technical selection, you must first understand some of the differences and differences.

1. What is MySQL? 826b8fcd35686ed194242a45c7da3135.png

MySQL is a relational database management system developed by the Swedish MySQL AB company and is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System) application software.

main feature:

1. Powerful architecture

2. Diversity of cluster architecture

3. Complete replication system

4. Powerful multi-line transaction support

5. Support for different plug-in storage engines (InnoDB)

6. There are rich types of third-party tools and active community users

2. What is MongoDB?

4710a23736e1bd8f560d329fea9a5ec7.png

MongoDB is a database based on distributed file storage. Written in C++ language, it is designed to provide scalable and high-performance data storage solutions for WEB applications.

MongoDB stores data as a document, and the data structure consists of key-value (key=>value) pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

main feature:

1. Document mode, unstructured data, flexible data structure, suitable for rapid development and iterative scenarios

2. Powerful functions, position index, text index, TTL index

3. The replica set automatically switches to ensure high reliability of data and high availability of services.

4. Automatic sharding, storage capacity, service capabilities and horizontal capabilities

5. Adapt to massive data scenarios such as logistics and Internet of Things

6. aggregation & mapreduce

3. Differences between MongoDB and MySQL

3.1 Differences in related concepts and terminology

9c4d9829efe0f3c0a88c0796ad2dd05c.jpeg

3.2 Differences in storage data structures

MySQL’s storage structure is divided into five levels: table space, segment, cluster, page, and row. The table space can be regarded as the highest level of the logical structure of the InnoDB storage engine, and all data is stored in the table space.

Different MySQL engines have different storage methods, while MongoDB stores it in a JSON-like document format.

f353d89d70481b82b36883745a8aec77.png

3.3 Differences in addition, deletion, modification and query operations

When requesting information from a database table or combination of tables, MySQL uses Structured Query Language (SQL). SQL is the most popular and widely utilized query language, and most developers have experience with it.

In contrast, MongoDB uses unstructured query language (MQL). To request data or information from a JSON document database, you must first specify a document with properties that the results should match.

Despite the similarities between MQL and SQL, MQL usually requires additional effort to learn.

a14876864f7c021c3c8ff6b8ef904c86.png

3.4 Differences in transaction support

Relational databases are the most suitable choice when the type of application requires multi-row transactions. In addition to providing security, MySQL also achieves high transaction processing rates. MongoDB only supports single-document transaction operations and weak consistency.

cfebd41c5be62c5afd6fe79ac633d9e1.png

3.5 Performance test summary analysis

  • Summary of insertion speed: 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.

  • When MongoDB inserts by specifying _id, the insertion performance drops drastically.

  • MySQL is very stable, and its efficiency is the same regardless of whether the primary key is specified or inserted without specifying the primary key.

  • MongoDB will make full use of memory as a cache.

3.6 Other main differences

Next, we’ll cover some other key differences.

f1037eac7966947ca5d5f670ac62d630.png

4. Application scenario analysis

As a document database, MongoDB does not limit the volume and type of data stored by users, so it is suitable for big data application environments. Thanks to MongoDB’s horizontal scalability and agility combined with cloud services, it can not only reduce developers’ workload, simplify business and project expansion processes, but also provide high availability and rapid data recovery.

However, MongoDB is not as good as MySQL in terms of data reliability, consistency, and security. In addition, when applications need to provide multi-row transactions (such as accounting and banking systems), relational databases led by MySQL provide high transaction rates (high transaction rate). In fact, unlike MySQL, which focuses on providing ACID and security of transactions, MongoDB is more focused on providing high insert rate (high insert rate).

MongoDB applications have penetrated into various fields, such as games, logistics, e-commerce, content management, social networking, Internet of Things, live video, etc.

6ef8f4a91698f04614e24b76d71cf5f2.png

Summary

To summarize, MySQL is an open source relational database, which means its data is organized into tables, allowing you to relate data to other parts of the database. MongoDB is also open source, however, it is a document database. Therefore, it does not associate records, and its data schema is not fixed, allowing for a more dynamic and flexible database with higher insertion capabilities.

8e5b04bab0c4263e8d739c161eec5adf.png

Before determining the best database system, the priorities for a specific business or project should be clear and prioritized.

  • MongoDB handles large amounts of data better than MySQL

  • Therefore, it is the most suitable choice for cloud-based services, applications that are easy to grow and change, and data-heavy environments.

  • In contrast, MySQL’s fixed and structured data schema provides greater consistency and reliability than most databases.

  • Another huge benefit of using MySQL is the superior data security due to ACID compliant transactions, making it the most suitable choice for applications that value this feature.

In short, both MongoDB and MySQL are excellent. How to choose depends entirely on your specific application needs and system characteristics.

·END·

Related reading:
  • The top ten secret tips for optimizing system performance that have been kept secretly for many years (ten thousand words of useful information)

  • In the face of complex business systems, general architecture design rules

  • Exploring the core principles of high availability and high performance, comprehensive summary of Kafka core

  • Practical summary of common solutions for service interface optimization

  • Let’s talk about eight asynchronous implementation methods under distributed services

  • Have you ever had “unemployment anxiety” for a moment?

  • A brief analysis of compensation mechanism design issues in distributed systems

  • Let’s talk about the design and practice of distributed log systems

  • Executing DEL will also block Redis? Digging deeper, it’s not that simple.

  • How do arrays in PHP flexibly support multiple data types?

  • This article will give you a thorough understanding of the implementation principles of the four major features of MySQL transaction ACID.

  • Rebuilding the table through alter table was an eye-opener for my colleagues.

  • Intern question: Why add an index to the field that needs to be sorted?

  • Code multi-version transformation, applying the responsibility chain design pattern

Focus on architecture technology research and overcome career bottlenecks together!

Follow the official account and receive free study materials

If you think it’s good, please follow and forward it~

8c8667392db50c414d41899dbed7fe03.png

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. MySQL entry skill treeSQL advanced skillsCTE and recursive query 77251 people are learning the system