The difference between mongodb, redis, mysql and specific application scenarios

One, MySQL

Relational Database.
There are different storage methods on different engines.
The query statement uses the traditional sql statement, with a relatively mature system and a high degree of maturity.
The share of open source databases continues to increase, and the share page of MySQL continues to grow.
The disadvantage is that the efficiency will be significantly slower when processing massive data.

二、Mongodb

Non-relational database (nosql) is a document database . First explain the database of the document, that is, it can store the data of the xml, json, and bson types. These data are self-describing and present a hierarchical tree data structure. The data structure consists of key-value (key=>value) pairs.
Storage method: virtual memory + persistence.
Query statement: It is a unique Mongodb query method.
Suitable for scenarios: event recording, content management or blog platform, etc.
Architecture characteristics: High availability can be achieved through replica sets and sharding.
Data processing: Data is stored on the hard disk, but the data that needs to be read frequently will be loaded into the memory, and the data will be stored in the physical memory to achieve high-speed reading and writing.
Maturity and breadth: emerging databases, low maturity, Nosql database is the closest to relational database, one of the more complete DB, the applicable population is constantly growing.
Advantage:

  • fast! The performance of Mongodb with a moderate amount of memory is very fast. It stores hot data in physical memory, making the read and write of hot data very fast.

  • High expansion!

  • Own Failover mechanism!

  • json storage format!

Disadvantages: mainly no mechanism!

Three, Redis

Non-relational database (nosql)
Redis data are all stored in memory and written to disk regularly. When the memory is not enough, you can select the specified LRU algorithm to delete data.

Fourth, the difference between MongoDB and Redis

Both MongoDB and Redis are NoSQL, using structured data storage. There are certain differences between the two in the usage scenarios, which is mainly due to the
different processing methods of the two in the memory mapping process and the persistence processing method. MongoDB recommends cluster deployment. More consideration is given to the cluster solution. Redis
focuses more on process sequential writes. Although it supports clusters, it is also limited to the master-slave mode.

index MongoDB(v2.4.9) Redis (v2.4.17) Comparison description
Implementation language C++ C/C++ -
protocol BSON, custom binary Telnet-like -
performance Depends on memory, higher TPS Depends on memory, TPS is very high Redis is better than MongoDB
Operability Rich data expression, index; most similar to relational database, support rich query language Rich data, less IO MongoDB is better than Redis
Memory and storage Suitable for large data storage, relying on system virtual memory management, using mirror file storage; memory occupancy rate is relatively high, the official recommendation is to deploy on a 64-bit system independently (32-bit has a maximum 2.5G file limit, 64-bit has no change limit) After Redis2.0, the virtual memory feature is added to break the physical memory limit; the data can be set timeliness, similar to memcache From different application perspectives, each has its own advantages
Availability Support master-slave, replicaset (internal use paxos election algorithm, automatic failure recovery), auto sharding mechanism, shielding the failover and segmentation mechanism for the client Relying on the client to implement distributed read and write; during master-slave replication, each time the slave node reconnects to the master node, the entire snapshot must be relied on, without incremental replication; automatic sharding is not supported, and a consistent hash mechanism needs to be programmed MongoDB is superior to Redis; in terms of single-point problems, MongoDB is simple to use and transparent to users, while Redis is more complicated and requires the client to actively solve it. (MongoDB generally uses a combination of replica sets and sharding functions. Replica sets focus on high availability and high reliability, while sharding focuses on performance and easy expansion)
reliability Since version 1.8, the binlog method (MySQL also uses this method) supports persistence and increases reliability Rely on snapshots for persistence; AOF enhances reliability; while enhancing reliability, it affects access performance MongoDB is better than Redis
consistency Don’t support things, rely on the client’s own guarantee Support things, relatively weak, only to ensure that the operations in the things are executed in order Redis is better than MongoDB
data analysis Built-in data analysis function (mapreduce) not support MongoDB is better than Redis
Application scenarios Improve the efficiency of access to massive data Performance and calculation of small amount of data MongoDB is better than Redis

Five, Mysql and Mongodb application scenarios

The applicable scenarios of MongoDB are: data is not particularly important (such as notifications, push these), the structure of the data table changes frequently, the amount of data is particularly large, the concurrency of the data is particularly high, and the data structure is relatively special (such as the location coordinates of the map). In this case, use MongoDB, in other cases, use MySQL, so that the combined use can achieve maximum efficiency.

  • 1. If you need to use mongodb as a backend db instead of mysql, that is, here mysql and mongodb belong to the parallel level, then such use may have the following considerations: (1) The part responsible for mongodb is stored in document form, It has good code affinity, and it is convenient to write directly in json format. (Such as logs) (2) Atomicity is taken into consideration from the design stage of data models, without assistance such as transactions. Development uses languages ​​such as nodejs for development, which is more convenient for development. (3) The failover mechanism of mongodb itself does not need to be implemented in a way such as MHA.

  • 2. Use mongodb as a cache db similar to redis and memcache to provide services for mysql, or back-end log collection and analysis. Considering that mongodb is a nosql database, sql statements and data structures are not as compatible as mysql, and there are many times when mongodb is used as an auxiliary mysql cache db like redis memcache. Or just for log collection and analysis.

MongoDB 有一个最大的缺点,就是它占用的空间很大,因为它属于典型空间换时间原则的类型。那么它的磁盘空间比普通数据库会浪费一些,而且到目前为止它还没有实现在线压缩功能,在 MongoDB 中频繁的进行数据增删改时,如果记录变了,例如数据大小发生了变化,这时候容易产生一些数据碎片,出现碎片引发的结果,一个是索引会出现性能问题。
另外一个就是在一定的时间后,所占空间会莫明其妙地增大,所以要定期把数据库做修复,定期重新做索引,这样会提升MongoDB 的稳定性和效率。
1.MySQL 来自女儿的名字; MongoDB 来自 humongous
2.MySQL 使用 Table/Row/Column; MongoDB 使用 Collection/Document
3.MySQL 需要指定 table 的 schema; MongoDB的 collection 的每个 document 的 schema 可以自由修改
4.MySQL 支持 join; MongoDB 没有 join
5.MySQL 使用 SQL 语言; MongoDB 使用类似 JavaScript 的函数
命令对比
MongoDB 与 MySQL 命令对比 传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB 是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。

六、MySQL 与 Redis 的区别

MySQL 是持久化存储,存放在磁盘里面,检索的话,会涉及到一定的 IO,为了解决这个瓶颈,于是出现了缓存,比如现在用的最多的 memcached(简称mc)。首先,用户访问mc,如果未命中,就去访问 MySQL,之后像内存和硬盘一样,把数据复制到mc一部分。
  Redis 和mc都是缓存,并且都是驻留在内存中运行的,这大大提升了高数据量web访问的访问速度。然而mc只是提供了简单的数据结构,比如 string存储;Redis却提供了大量的数据结构,比如string、list、set、hashset、sorted set这些,这使得用户方便了好多,毕竟封装了一层实用的功能,同时实现了同样的效果,当然用Redis而慢慢舍弃mc。
  内存和硬盘的关系,硬盘放置主体数据用于持久化存储,而内存则是当前运行的那部分数据,CPU访问内存而不是磁盘,这大大提升了运行的速度,当然这是基于程序的局部化访问原理。
  推理到 Redis + MySQL,它是内存+磁盘关系的一个映射,MySQL 放在磁盘,Redis放在内存,这样的话,web应用每次只访问Redis,如果没有找到的数据,才去访问 MySQL。
  然而 Redis + MySQL 和内存+磁盘的用法最好是不同的。
前者是内存数据库,数据保存在内存中,当然速度快。
后者是关系型数据库,功能强大,数据访问也就慢。
像memcache,MongoDB,Redis,都属于No SQL系列。
不是一个类型的东西,应用场景也不太一样,还是要看你的需求来决定。

转载自:https://www.cnblogs.com/phoebeyue/p/10598719.html

Guess you like

Origin blog.csdn.net/pjh88/article/details/114887619