Comparison of MongoDB and Mysql

What is Mysql ( detailed connection )

MySQL was developed by Oracle Corporation, publishing and support of popular open source relational database management system (RDBMS). Like other systems, like relationships, MySQL stores data in tables, and the use of Structured Query Language (SQL) to access the database. In MySQL, you can pre-defined database schema as needed, and set rules to govern the relationship between the fields in the table. In MySQL, the relevant information may be stored in a separate table, but to associate by using the associated queries. By using this way, so that duplication of data is minimized amount

What is MongoDB ( detailed connection )

MongoDB is MongoDB, Inc. The development of open-source database. The MongoDB JSON similar document, and each document may vary json string data storage structure. Related information are stored together for fast query access through MongoDB query language. MongoDB uses dynamic mode, which means you can create a record without first defining the structure, such as type field or value. You can change the record by adding new fields or delete an existing record structure (which we call the document). The data model allows you to easily represents the hierarchy, storage arrays and other more complex structures. The document set need not have the same set of fields, non-normalized data is common. MongoDB also designed for high availability and scalability, and provides a ready to use automatic replication and fragmentation.

Terms and Concepts

MySQL MongoDB
Table table Collection collection
Row row Documentation document
Column column Field field
Table join joins Embedded in the document or link

Syntax Comparison

Explanation MySQL MongoDB
Display library list show databases show dbs
Use the library use mydb1; use mydb1
Display table list Show tables Show collections
Create a table Create table users(age int, sex int) db.people.insert ({ "name": "Zi God", "age", 18}) ps: Create table implicitly
Creating an index Create INDEX idxname ON users(name) db.people.ensureIndex({name:1})
Insert Record Insert into users values(1, 1) db.people.insert({"name":"紫龙神","age",18})
Lookup table Select * from users db.peple.find({"age":18})
Lookup table Select * from users db.people.find()
Conditions inquiry Select * from users where age=33 db.people.find({age:33})
Conditions inquiry select * from users where age<33 db.people.find({'age':{$lt:33}})
Conditions inquiry select * from users where age>33 and age<=40 db.people.find({'age':{gt:33,lte:40}})
Conditions inquiry select * from users where a=1 and b='q' db.people.find({a:1,b:'q'})
Conditions inquiry select * from users where a=1 or b=2 db.people.find( { $or:[ { a:1 } , { b:2 } ] } )
Conditions inquiry select * from users limit 1 db.people.findOne()
Fuzzy query select * from users where name like "%Joe%" db.people.find({name:/Joe/})
Fuzzy query select * from users where name like "Joe%" db.people.find({name:/^Joe/})
Gets the number of records in the table select count(1) from users db.people.count()
Gets the number of records in the table bug select count(1) from users where age>30 db.people.find({age: {$g':30}}.count()
Remove duplicate values select DISTINCT last_name from users db.people.distinct('last_name')
Sorting (positive sequence) select * from users ORDER BY name db.people.find().sort({name:1})
Sort (reverse) select * from users ORDER BY name DESC db.people.find().sort({name:-1})
update record update users set a=1 where b='q' db.people.update({b:'q'}, {$set:{a:1}}, false, true)
update record update users set a=a+2 where b='q' db.people.update({b:'q'}, {$inc:{a:2}}, false, true)
Delete Record delete from users where z="abc" db.people.remove ({z: 'abc'});. db people.remove () to delete all records
Delete Database drop database IF EXISTS test use test; db.dropDatabase()
删除表/collection drop table IF EXISTS test db.mytable.drop()

应用场景对比

MongoDB:

更高的写入负载

默认情况下,MongoDB更侧重高数据写入性能,而非事务安全,MongoDB很适合业务系统中有大量“低价值”数据的场景。但是应当避免在高事务安全性的系统中使用MongoDB,除非能从架构设计上保证事务安全。

高可用性

MongoDB的复副集(Master-Slave)配置非常简洁方便,此外,MongoDB可以快速响应的处理单节点故障,自动、安全的完成故障转移。这些特性使得MongoDB能在一个相对不稳定(如云主机)的环境中,保持高可用性。

数据量很大或者未来会变得很大

依赖数据库(MySQL)自身的特性,完成数据的扩展是较困难的事,在MySQL中,当一个单达表到5-10GB时会出现明显的性能降级,此时需要通过数据的水平和垂直拆分、库的拆分完成扩展,使用MySQL通常需要借助驱动层或代理层完成这类需求。而MongoDB内建了多种数据分片的特性,可以很好的适应大数据量的需求。

基于位置的数据查询

MongoDB支持二维空间索引,因此可以快速及精确的从指定位置获取数据。

表结构不明确,且数据在不断变大

在一些传统RDBMS中,增加一个字段会锁住整个数据库/表,或者在执行一个重负载的请求时会明显造成其它请求的性能降级。通常发生在数据表大于1G的时候(当大于1TB时更甚)。 因MongoDB是文档型数据库,为非结构货的文档增加一个新字段是很快速的操作,并且不会影响到已有数据。另外一个好处当业务数据发生变化时,是将不在需要由DBA修改表结构。

缓存

由于性能很高,也适合作为信息基础设施的缓存层。在系统重启之后,搭建的持久化缓存可以避免下层的数据源过载。

Mongodb 应用案例

  • 京东,中国著名电商,使用MongoDB存储商品信息,支持比价和关注功能.
  • 赶集网,中国著名分类信息网站,使用MongoDB记录pv浏览计数
  • Qihoo 360, the famous virus protection software and mobile applications platform, using MongoBD support HULK platform to accept the 20 billion queries per day.
  • Baidu cloud, using MongoDB management Baidu cloud disk 50 billion records file information about the source.
  • CERN, the famous Institute of Particle Physics, CERN Large Hadron Collider data use MongoDB
  • The New York Times, one of the leading online news portal, using MongoDB
  • sourceforge.net, resource website to find, create and distribute open source software is free, the back-end storage for MongoDB

Mysql:

1) These data are usually structured query needs to be done, such as join, this time, the relational database will win a chip

2) the size, growth rate of these data are usually to be expected

3) transactional consistency (mongdodb4.0 also begun to support the transaction)

4) Rich lock mechanism

Mysql Applications

The height of the system of things

Such as banking or accounting system; traditional relational database is still more suitable for applications that require extensive atomic complex transactions.

Traditional business intelligence applications

BI database for specific issues would have a highly optimized query. For such applications, a data warehouse may be a more appropriate choice

Recommended reading:

MongdoDB tutorial:  https://piaosanlang.gitbooks.io/mongodb/
Mysql Optimization Ten Tips:  https://www.cnblogs.com/sharpest/p/10390035.html

Guess you like

Origin www.cnblogs.com/yalong/p/12120674.html