Move the docs folder
This commit is contained in:
		
				
					committed by
					
						
						LINxiansheng
					
				
			
			
				
	
			
			
			
						parent
						
							7c6dcc6712
						
					
				
				
					commit
					d42f317422
				
			@ -0,0 +1,60 @@
 | 
			
		||||
概述 
 | 
			
		||||
=======================
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
查询(SQL)是指数据库中用来获取数据的方式,它可搭配条件限制的子句(如where),排列顺序的子句(如order by)等语句来获取查询结果。子查询是指嵌套在一个上层查询中的查询。上层的查询一般被称为父查询或外层查询。子查询的结果作为输入传递回"父查询"或"外部查询"。父查询将这个值结合到计算中,以便确定最后的输出。SQL语言允许多层嵌套查询,即一个子查询中还可以嵌套其他子查询。同时,子查询可以出现在SQL语句中的各种子句中,比如select语句,from语句,where语句等。
 | 
			
		||||
 | 
			
		||||
子查询 
 | 
			
		||||
------------
 | 
			
		||||
 | 
			
		||||
在数据库中,子查询可以分成有依赖关系的子查询和没有依赖关系的子查询。有依赖关系的子查询是指该子查询的执行依赖了外部查询的'变量',所以这种子查询通常会被计算多次。没有依赖关系的子查询是指该子查询的执行不依赖外部查询的'变量', 这种子查询一般只需要计算一次。下图分别展示了一个没有依赖关系的子查询和一个有依赖关系的子查询。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.70 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.92 sec)
 | 
			
		||||
-- 没有依赖关系的子查询
 | 
			
		||||
OceanBase (root@test)> select * from t1 where t1.a in (select t2.a from t2);
 | 
			
		||||
Empty set (0.22 sec)
 | 
			
		||||
-- 有依赖关系的子查询,子查询中用到了外层查询变量t1.b
 | 
			
		||||
OceanBase (root@test)> select * from t1 where t1.a in (select t2.a from t2 where t2.b = t1.b);
 | 
			
		||||
Empty set (0.05 sec)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
子查询展开(subquery unnesting) 
 | 
			
		||||
----------------------------------
 | 
			
		||||
 | 
			
		||||
子查询展开是数据库的一种优化策略,它把一些子查询置于外层的父查询中,其实质是把某些子查询转化为等价的多表连接操作。这种策略带来的一个明显的好处就是,有写访问路径,连接方法和连接顺序可能被有效的利用,使得查询语句的层次尽可能的减少。下图展示了一个子查询展开的例子,既子查询被改写成了连接语句。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.70 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.92 sec)
 | 
			
		||||
--- 有依赖关系的子查询被展开改写成连接
 | 
			
		||||
OceanBase (root@test)> explain select * from t1 where t1.a in (select t2.b from t2 where t2.c = t1.c);
 | 
			
		||||
| =======================================
 | 
			
		||||
|ID|OPERATOR      |NAME|EST. ROWS|COST|
 | 
			
		||||
---------------------------------------
 | 
			
		||||
|0 |HASH SEMI JOIN|    |1        |2924|
 | 
			
		||||
|1 | TABLE SCAN   |t1  |1000     |455 |
 | 
			
		||||
|2 | TABLE SCAN   |t2  |1000     |455 |
 | 
			
		||||
=======================================
 | 
			
		||||
 | 
			
		||||
Outputs & filters:
 | 
			
		||||
-------------------------------------
 | 
			
		||||
  0 - output([t1.a], [t1.b], [t1.c]), filter(nil),
 | 
			
		||||
      equal_conds([t1.a = t2.b], [t2.c = t1.c]), other_conds(nil)
 | 
			
		||||
  1 - output([t1.c], [t1.a], [t1.b]), filter(nil),
 | 
			
		||||
      access([t1.c], [t1.a], [t1.b]), partitions(p0)
 | 
			
		||||
  2 - output([t2.c], [t2.b]), filter(nil),
 | 
			
		||||
      access([t2.c], [t2.b]), partitions(p0)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,103 @@
 | 
			
		||||
连接 
 | 
			
		||||
=======================
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
数据库中的连接语句用于将数据库中的两个或多个表根据连接条件组合起来。由"连接"生成的集合, 可以被保存为表, 或者当成表来使用。连接语句的含义是把两张表的属性通过它们的值组合在一起。数据库中的连接类型一般包括inner join,outer join,semi-join和anti-join。其中Semi-join和Anti-join都是通过子查询改写得到,SQL本身并没有表述Anti-join和Semi-join的语法
 | 
			
		||||
 | 
			
		||||
连接条件 
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
连接条件可以分为等值连接(比如t1.a = t2.b)和非等值连接(t1.a \< t2.b)。相比于非等值连接条件, 等值连接条件的一个好处是允许数据库中使用高效的连接算法,比如Hash Join和Merge-Sort join。
 | 
			
		||||
 | 
			
		||||
Self-join 
 | 
			
		||||
------------------
 | 
			
		||||
 | 
			
		||||
Self-Join是指跟跟自己表做连接的join。下图展示了一个self join的例子。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.70 sec)
 | 
			
		||||
---一个self join的例子
 | 
			
		||||
OceanBase (root@test)> select * from t1 as ta, t1 as tb where ta.b = tb.b
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
内连接(inner join) 
 | 
			
		||||
------------------------
 | 
			
		||||
 | 
			
		||||
Inner Join(内连接)是数据库中最基本的连接操作。内连接基于连接条件将两张表(如 A 和 B)的列组合在一起,产生新的结果表。查询会将 A 表的每一行和 B 表的每一行进行比较,并找出满足连接条件的组合。当连接条件被满足,A 和 B 中匹配的行会按列组合(并排组合)成结果集中的一行。连接产生的结果集,可以定义为首先对两张表做笛卡尔积(交叉连接) -- 将 A 中的每一行和 B 中的每一行组合,然后返回满足连接条件的记录。
 | 
			
		||||
 | 
			
		||||
外连接(outer join) 
 | 
			
		||||
------------------------
 | 
			
		||||
 | 
			
		||||
Outer Join(外连接)并不要求连接的两表的每一条记录在对方表中都一条匹配的记录。要保留所有记录(甚至这条记录没有匹配的记录也要保留)的表称为保留表 **。** 外连接可依据连接表保留左表, 右表或全部表的行而进一步分为左外连接, 右外连接和全连接。其中左外连接中左表的一行未在右表中找到的时候,就在右表自动填充NULL。右外连接中右表的一行未在左表中找到的时候,就在左表自动填充NULL。全连接就是左表或者右表找不匹配行的时候都会自动填充。
 | 
			
		||||
 | 
			
		||||
Semi连接 (semi-join) 
 | 
			
		||||
---------------------------
 | 
			
		||||
 | 
			
		||||
当A表和B表进行left/right semi-join的时候,它只返回A/B表中所有能够在B/A中找到匹配的行。Semi-join只能通过子查询展开得到,如下图所示。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.70 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.92 sec)
 | 
			
		||||
--- 有依赖关系的子查询被展开改写成Semi-join
 | 
			
		||||
OceanBase (root@test)> explain select * from t1 where t1.a in (select t2.b from t2 where t2.c = t1.c);
 | 
			
		||||
| =======================================
 | 
			
		||||
|ID|OPERATOR      |NAME|EST. ROWS|COST|
 | 
			
		||||
---------------------------------------
 | 
			
		||||
|0 |HASH SEMI JOIN|    |1        |2924|
 | 
			
		||||
|1 | TABLE SCAN   |t1  |1000     |455 |
 | 
			
		||||
|2 | TABLE SCAN   |t2  |1000     |455 |
 | 
			
		||||
=======================================
 | 
			
		||||
 | 
			
		||||
Outputs & filters:
 | 
			
		||||
-------------------------------------
 | 
			
		||||
  0 - output([t1.a], [t1.b], [t1.c]), filter(nil),
 | 
			
		||||
      equal_conds([t1.a = t2.b], [t2.c = t1.c]), other_conds(nil)
 | 
			
		||||
  1 - output([t1.c], [t1.a], [t1.b]), filter(nil),
 | 
			
		||||
      access([t1.c], [t1.a], [t1.b]), partitions(p0)
 | 
			
		||||
  2 - output([t2.c], [t2.b]), filter(nil),
 | 
			
		||||
      access([t2.c], [t2.b]), partitions(p0)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Anti-连接(anti-join) 
 | 
			
		||||
---------------------------
 | 
			
		||||
 | 
			
		||||
当A表和B表进行left/right anti-join的时候,它只返回A/B中所有不能再B/A中找到匹配的行。类似于Semi-join, anti-join也只能通过子查询展开得到,如下图所示。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.70 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.92 sec)
 | 
			
		||||
---有依赖关系的子查询被改写成Anti-join
 | 
			
		||||
OceanBase (root@test)> explain select * from t1 where t1.a not in (select t2.b from t2 where t2.c = t1.c);
 | 
			
		||||
| =======================================
 | 
			
		||||
|ID|OPERATOR      |NAME|EST. ROWS|COST|
 | 
			
		||||
---------------------------------------
 | 
			
		||||
|0 |HASH ANTI JOIN|    |995      |3262|
 | 
			
		||||
|1 | TABLE SCAN   |t1  |1000     |455 |
 | 
			
		||||
|2 | TABLE SCAN   |t2  |1000     |455 |
 | 
			
		||||
=======================================
 | 
			
		||||
 | 
			
		||||
Outputs & filters:
 | 
			
		||||
-------------------------------------
 | 
			
		||||
  0 - output([t1.a], [t1.b], [t1.c]), filter(nil),
 | 
			
		||||
      equal_conds([t2.c = t1.c]), other_conds([t1.a = t2.b OR (T_OP_IS, t2.b, NULL, 0)])
 | 
			
		||||
  1 - output([t1.c], [t1.a], [t1.b]), filter(nil),
 | 
			
		||||
      access([t1.c], [t1.a], [t1.b]), partitions(p0)
 | 
			
		||||
  2 - output([t2.c], [t2.b]), filter(nil),
 | 
			
		||||
      access([t2.c], [t2.b]), partitions(p0)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,165 @@
 | 
			
		||||
集合 
 | 
			
		||||
=======================
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
数据库中的集合操作可以把多个查询的结果组合成一个结果集。集合操作主要包含:
 | 
			
		||||
 | 
			
		||||
* UNION
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
<!-- -->
 | 
			
		||||
 | 
			
		||||
* INTERSECT
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
<!-- -->
 | 
			
		||||
 | 
			
		||||
* EXCEPT/MINUS在Ocenabase中,我们同时支持EXCEPT与MINUS,这两者的语义是相同的。这里需要注意的是参加集合操作的各查询结果的列数必须相同,对应的数据类型也必须兼容。对与UNION来说用户可以指定UNION的属性为ALL和DISTINCT/UNIQUE。分别代表集合可重复,和集合不可重复。而其它几种集合操作是不能指定ALL属性的(它们只有DISTINCT属性)。所有的集合操作默认的属性是DISTINCT。在Oceanbase中,集合操作中可以指定order by和 limit子句,但是不允许其他子句的出现,如下图所示
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.16 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int primary key, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.10 sec)
 | 
			
		||||
--支持union语句中出现order by和limit子句
 | 
			
		||||
OceanBase (root@test)> (select * from t1 union all select * from t2) order by a limit 10;
 | 
			
		||||
Empty set (0.02 sec)
 | 
			
		||||
--不支持union语句中出现除order by和limit子句的其他子句,比如group by
 | 
			
		||||
OceanBase (root@test)> OceanBase (root@test)> (select * from t1 union all select * from t2) group by a limit 10;
 | 
			
		||||
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near 'OceanBase (root@test)> (select * from t1 union all select * from t2) group by a ' at line 1
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
UNION 例子 
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
该例子获取t1和t2中所有不重复的行。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.11 sec)
 | 
			
		||||
OceanBase (root@test)> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
 | 
			
		||||
Query OK, 3 rows affected (0.07 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> insert into t2 values (2,2,2),(3,3,3),(4,4,4);
 | 
			
		||||
Query OK, 3 rows affected (0.02 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> select * from t1 union select * from t2;
 | 
			
		||||
+------+------+------+
 | 
			
		||||
| a    | b    | c    |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
|    1 |    1 |    1 |
 | 
			
		||||
|    2 |    2 |    2 |
 | 
			
		||||
|    3 |    3 |    3 |
 | 
			
		||||
|    4 |    4 |    4 |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
4 rows in set (0.01 sec)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
UNION All 例子 
 | 
			
		||||
---------------------
 | 
			
		||||
 | 
			
		||||
该例子获取t1和t2中的所有行,不进行去重。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.11 sec)
 | 
			
		||||
OceanBase (root@test)> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
 | 
			
		||||
Query OK, 3 rows affected (0.07 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> insert into t1 values (2,2,2),(3,3,3),(4,4,4);
 | 
			
		||||
Query OK, 3 rows affected (0.02 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> select * from t1 union all select * from t2;
 | 
			
		||||
+------+------+------+
 | 
			
		||||
| a    | b    | c    |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
|    1 |    1 |    1 |
 | 
			
		||||
|    2 |    2 |    2 |
 | 
			
		||||
|    3 |    3 |    3 |
 | 
			
		||||
|    2 |    2 |    2 |
 | 
			
		||||
|    3 |    3 |    3 |
 | 
			
		||||
|    4 |    4 |    4 |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
6 rows in set (0.02 sec)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
INTERSECT 例子 
 | 
			
		||||
---------------------
 | 
			
		||||
 | 
			
		||||
该例子获取同时出现在t1和t2中行,并且去重。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
 | 
			
		||||
Query OK, 3 rows affected (0.02 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> insert into t2 values (2,2,2),(3,3,3),(3,3,3),(4,4,4);
 | 
			
		||||
Query OK, 4 rows affected (0.01 sec)
 | 
			
		||||
Records: 4  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> select * from t1 intersect select * from t2;
 | 
			
		||||
+------+------+------+
 | 
			
		||||
| a    | b    | c    |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
|    2 |    2 |    2 |
 | 
			
		||||
|    3 |    3 |    3 |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
2 rows in set (0.01 sec)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
EXCEPT/MINUS 例子 
 | 
			
		||||
------------------------
 | 
			
		||||
 | 
			
		||||
该例子获取出现在t1中,但是不出现在t2中的行,并且去重。
 | 
			
		||||
 | 
			
		||||
```javascript
 | 
			
		||||
OceanBase (root@test)> create table t1(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> create table t2(a int, b int, c int);
 | 
			
		||||
Query OK, 0 rows affected (0.12 sec)
 | 
			
		||||
OceanBase (root@test)> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
 | 
			
		||||
Query OK, 3 rows affected (0.02 sec)
 | 
			
		||||
Records: 3  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> insert into t2 values (2,2,2),(3,3,3),(3,3,3),(4,4,4);
 | 
			
		||||
Query OK, 4 rows affected (0.01 sec)
 | 
			
		||||
Records: 4  Duplicates: 0  Warnings: 0
 | 
			
		||||
OceanBase (root@test)> select * from t1 except select * from t2;
 | 
			
		||||
+------+------+------+
 | 
			
		||||
| a    | b    | c    |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
|    1 |    1 |    1 |
 | 
			
		||||
+------+------+------+
 | 
			
		||||
1 row in set (0.02 sec)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user