zoukankan      html  css  js  c++  java
  • Core Model of Sql

    Let's talk about SQL in two appects: Data Model and Computation Model.

    Data Model

      On one hand, each row in a table is actually an "Object". Columns of a row are fields of an object. Someone may ask: in a programming language we can define recursive data structures, how can SQL achieve it? Yes, it can, only a bit circuitous.  For example, we have a TreeNode class:

        class TreeNode{

            TreeNode left_node;

            TreeNode right_node;

            int value;

        }

    We can define a similar structure in sql table(where left_node_id points to another row whose id is "left_node_id"):

        create table TreeNode(

            node_id                   int

            left_node_id    int,

            right_node_id  int,

            value              int

        )

     On the other hand, a table is a "Hash Table". The key of this hash table is the primary key or the unique key, the value is the entire row.

       Finally, above "object" and "Hash Table " views do not  conflict, but can be unified.  When a program runs, all the objects in its memory actually compose a big hashtable. The mapping is just: Object Name->Object data in memory. So, each row is an object; the entire table is a hash table.

    Computation Model

    sql computation is driven by 3 operations: Group, Filter,Join. All the other operations and functions are auxiliary.

    Group(group by, distribute by)

      What indeed does a group operation do ?

      1. divide rows into partitions.

          all the rows in the same partition has the same key.

      2. use a function to do transformation on the partition.

        when it's an aggregate function(such as sum/count/avg/min/max/first/last/order by ......), then:

          rows in a partition=> one row

        when it's not an aggregate function(such as "distribute by" operation), then:

          rows in a partition=> rows in a partition

    Filter(select where)

      Silly simple: rows => rows (via where statement)

    Join: set operation

          As is shown in "Data Model", a table is actually a Hash Table. So a table is also a Set. (Because a Set is really a special form of Hash Table, the elements of the Set, are keys of the Hash Table)

      Join without any filter condition:  SetA + SetB

      Inner Join:                                 SetA intersection SetB

      Left/Right Outer Join:                 SetA - SetB

    Hive and Map Reduce

      The key-value nature(Hash Table) of sql table fits very well with MapReduce. That's why Hive was born. Further more, SQL is a procedural language more than a query language. I implemented Dijkstra algorithm using SQL, click here

  • 相关阅读:
    6 全局锁和表锁
    oracle ogg--ogg搭建过程中遇到的错误及处理
    5 深入浅出索引(下)
    4 深入浅出索引(上)
    oracle ogg 单实例双向-新增表,修改表结构(oracle-oracle
    oracle ogg 单实例双向复制搭建(oracle-oracle)--Oracle GoldenGate
    Iview 中 获取 Menu 导航菜单 选中的值
    idea中git分支的使用
    vue使用axios进行ajax请求
    web前端_Vue框架_设置浏览器上方的标题和图标
  • 原文地址:https://www.cnblogs.com/linghuaichong/p/4366844.html
Copyright © 2011-2022 走看看