Saturday, May 24, 2014

Sharding VS Partition

今天在网上学习NoSQL的时候发现老出现partition和sharding这两个关键字, 知道他们的大致意思是把数据分分开, 但是不知道具体是什么区别, 看了quora的一个回答, 感觉终于有点明了了(http://www.quora.com/Whats-the-difference-between-sharding-and-partition). 

所谓pairition -- 分区, 是一个更general的动词, 就是把"数据分成几份" 的意思,其中有两种类型,第一种是 "horizontal partition" 水平分区 -- 也叫sharding, 另外一个叫做 "vertical partition" 垂直分区.

horizontal partition 意思很明显, 就是把一个很大的 , 在同一个table里面的数据 (在NoSQL里面应该说是同一个collection里面的documents) 按照row 来分成几个部分, 一般来说, 水平分区的时候会按照一个shard key来进行分区, 什么意思呢? 就比如我有一个table, 里面的PK是customerID, 最简单的利用shard key来分区的方法,就是把ID分成不同份的range来区分, 比如shard 1 是ID 从1-10000, shard 2 是ID从10001- 20000, 这样查找起来数据, 这样是最intuitive,当然DBA在分区的时候还会考虑更多的吗比如那些数据是经常会被一起查找的, 给予query的诉求,会为了减少I/O而把一部分常常一起作为结果的放在一个shard里面.

vertical partition 就是将一个表垂直分开,分成左右两部分.有什么应用呢? 比如,我有一个table, 里面是customerID以及若干个address, 一般来说我只需要一个biling address, 可是我不想lose track of other address, 为了方便, 我把主表和其他地址分成俩个地方储存, 用key 连接.

为什么我上面不直接把horizontal partition 叫sharding呢,其实两者还是有一定区别的, 有一个总结的不错的表,直接贴过来了~
地址:http://like-eagle.iteye.com/blog/704629



Different Types of NoSQL Database

NoSQL stands for 'not only SQL'.

并不是说完全不用到SQL的思想, 只是query的方式不同, 不需要用到传统的SQL query来进行数据的读取,还有一个很重要的特点是, NoSQL DB 不用到传统意义上的join 来进行table之间的结合,也当然就没有FK之类的概念, PK在NoSQL里面基本上是以hash key的形式存在 ( 在key-value以及document db中作为一个field -- _id存在)
现在主流的NoSQL数据库类型有四种:                                                                          

a. Key-Value storage : Oracle NoSQL DB                                                                                          

b. Document Oriented Database: MongoDB, CouchDB

c. Wide-Column Store: Hadoop/HBase, Cassandra

d. Graph Database: Neo4j

在学习了lynda上的counchDB教程以后 (笔记详见板块里的记录), 今天阅读了一下MongoDB的官方指南 < The little MongoDB book>, 下面摘录了一下关于document store 的一些特点! 以MongoDB为例,(与counchDB有所不同)

Document oriented database: CouchDB, mongodb

I will be focusing on mongoDB as example. There are 6 main components in mongoDB

1. instance: one instance in mongoDB is made up of one or  more databases. 

2. database: The concept of 'database' is similar to the one used in 
   traditional relational database.

3. collections: one database is made up of one or more collections. And a 
   collection is made up of one or more documents. As a reference, you can think a collection as a table in traditional relational db.

4. document: one document is made up of several fields. one document could be 
   compared as one row in traditional relational database.

5. fileds: fileds could be compared as columns in table, but it doesn't 
   necessarily to be the same even within one table -- 'one document', which makes it more flexible than traditional db.

6. indexes: indexes in mongodb is similar with indexes in RDBMS 

7. cursor: when you ask mongodb for data, it returns curosr, which could do a 
lot of things without actually pulling out data, things include counting and skipping ahead.   ( to be studied.)

不得不说对cursor和index的理解都不是很深入,需要再好好看一下,这里留个伏笔~