在日常开发中,我一般用mysql数据储存数据,memcache储存缓存,但是我不知道如mongodb这一类的nosql数据库到底相比mysql这种数据库有什么优势,在什么场景下我改选择mongodb这种数据库?希望各位能用浅显的语言帮忙解释下。
怪我咯2017-05-02 09:21:43
Give me an example:
mysql: There are 5 fields in a table. Your program has been written, and then you need to add two fields here, so you first go to the database to modify the table structure, add fields, and then return to the program and modify the sql code.
mongodb: An object has 5 attributes. Your program has been written. Then you need to add two attributes (fields) here. You can add them directly in the program and it will be OK.
In a relational database, a record is a row, a field is a column, and the data is at the intersection of rows and columns. Multiple rows and columns must follow the same two-dimensional structure.
Non-relational database, attributes only belong to objects. Objects are independent from each other.
大家讲道理2017-05-02 09:21:43
For long-term business, use the combination of mysql+memcache, and for statistical log business or temporary transactions on the PM’s forehead, use the combination of mongodb+redis.
The advantage of mongodb is flexibility, the disadvantage is also flexibility, and its stability is not as good as mysql
漂亮男人2017-05-02 09:21:43
Compared with traditional relational databases, MongoDB is one of the representatives of non-relational databases. To know when to use MongoDB, you should first know what features it has, what it can and cannot do, and then decide whether to use it based on the specific situation. It is recommended to read the introductory part of the official documentation first, which lists the most critical features of MongoDB, and almost all other functions revolve around these features. These features are often the key factors in deciding whether to adopt MongoDB.
Here is an article that briefly and roughly introduces how to choose.
However, with the development of technology, the boundaries between the so-called "relational" and "non-relational" have become increasingly blurred. On the one hand, traditional RDBMS such as Postgre and MySQL have begun to support non-relational models such as JSON. On the other hand, MongoDB has also begun to JOIN is supported and even transactions will be supported in the future. Now that the boundaries have become blurred, it cannot be generalized when which technology should be used. The comparison result is often that both this and that can be used. But you can indeed compare what you will gain and lose if you use a certain technology in your project, and then weigh whether the gain can make up for the loss, and ultimately decide whether to adopt this technology.
ringa_lee2017-05-02 09:21:43
It is best to use mongodb when the data structure is not certain. For example, in your sql database, you must first define the types, names, lengths of all fields, etc. This lacks flexibility. When mongodb does not exist in a certain field, it will He didn't.
For example, suppose our server wants to save system events in the database. When a certain user does something, sometimes you need to record the original body content of the post he sent for query, and sometimes you don't. Then when you don't need to record the post content, if you define string in SQL If the length is 1000, this space is wasted, but mongodb will not. And when fetching this record, the SQL database will return the structure you defined, even if one of the fields is empty, and mongodb does not include the empty field when saving, and it will not be fetched when it is fetched. have.
Also, mongodb supports various json structures very well. Of course, this should not be the main purpose of using mongodb.