NoSQL vs SQL

来源:互联网 发布:python和qt做界面 编辑:程序博客网 时间:2024/04/30 19:13

reference: 

https://azure.microsoft.com/en-us/documentation/articles/documentdb-nosql-vs-sql/

https://www.mongodb.com/nosql-explained

这篇文章总体总结一下NoSQL和SQL的区别,NoSQL的好处。

SQL could not satisfy the increased need to precess higher volumnes and varieties of data at a rapid rate. SoNoSQL database can enable storing unstructured and heterogeneous data at scale have gained in popularity.

NoSQL is short for "Not only SQL" and represented as many different categories: 

  • Document databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.
  • Graph stores are used to store information about networks of data, such as social connections. Graph stores include Neo4J and Giraph.
  • Key-value stores are the simplest NoSQL databases. Every single item in the database is stored as an attribute name (or 'key'), together with its value. Examples of key-value stores are Riak and Berkeley DB. Some key-value stores, such as Redis, allow each value to have a type, such as 'integer', which adds functionality.
  • Wide-column stores such as Cassandra and HBase are optimized for queries over large datasets, and store columns of data together, instead of rows.

NoSQL vs SQL overview diagram demonstrating common scenarios and data models

什么时候用NoSQL?

想象一下我们在这样一个场景中,我们需要设计一个大的社交网站。用户可以创建post,比如照片视频和音乐。其他用户也可以再这些post下进行回复,打分。首页会给用户这些回复的提示。

如果在SQL中,我们需要设计以下这个数据库:



看起来挺不错的。但是,假如我们需要把这些post和相关的照片,音频,视频,评论,打分,用户信息显示在一个app或者网页上的时候,我们需要进行8次table join才能弄到我们需要的信息。(?为什么8次?)。那如果在一个流媒体中,post动态的大量加载,那会需要对数据库进行无数次的query和join来完成一个任务。这样的数据库架构无法适应这种应用需求。

于是我们可以用NoSQL了。NoSQL可以把一个post储存为一个JSON文件的格式,如下所示。然后保存在比如说DocumentDB或者Azure NoSQL数据库服务中。

{    "id":"ew12-res2-234e-544f",    "title":"post title",    "date":"2016-01-01",    "body":"this is an awesome post stored on NoSQL",    "createdBy":User,    "images":["http://myfirstimage.png","http://mysecondimage.png"],    "videos":[        {"url":"http://myfirstvideo.mp4", "title":"The first video"},        {"url":"http://mysecondvideo.mp4", "title":"The second video"}    ],    "audios":[        {"url":"http://myfirstaudio.mp3", "title":"The first audio"},        {"url":"http://mysecondaudio.mp3", "title":"The second audio"}    ]}

好处:

  • 我们只需要一次请求就能拿到所有需要的信息而不需要进行join。这样的设计更加的直观与科学,性能也更好。
  • 基于post的id,这样的数据也可以更方便的被分开,从而能更方便的进行扩展。
  • NoSQL还能给用户更大的自由发挥的空间。allow users to lossen consistency and offer high available apps.
  • 不要求开发者对data的schema来进行呢define,manage和maintain。从而更适合快速的迭代。
  • OOP is easy to use and flexible
  • 分布式的储存可以使用便宜的机器,而不需要买单台昂贵的机器进行储存。

NoSQL 和 SQL 对比表格



0 0