GBase 8a多列in的使用场景和限制

GBase 8a支持in的查询条件,包括单列和多列。本文对GBase 8a多列in的使用场景和限制进行了讨论。

注意

因为in部分的数据,将全部载入内存,也就是如果in部分数据1000万,需要10G内存,那就要分配10G内存,并不受集群算子参数的限制。

如下根据in部分的数据量,提供不同的写法建议。

个别数据显式数值多列in

这个写法一般适合数字比较少的情况,比如几个,几千个。

多个列用小括号包围,一组数值用小括号包围,所有数字外再用小括号包围。示意如下 where (a,b) in ( (X,X),(Y,Y),....)

gbase> select * from tt1;
+------+------+------+
| id   | id2  | id3  |
+------+------+------+
|    0 | -110 | -129 |
|    1 | -110 | -123 |
|    2 | -110 | -110 |
|    3 | -118 | -119 |
|    4 | -110 | -119 |
+------+------+------+
5 rows in set (Elapsed: 00:00:00.01)

gbase> select * from tt1 where (id,id2) in ((0,-110),(1,3));
+------+------+------+
| id   | id2  | id3  |
+------+------+------+
|    0 | -110 | -129 |
+------+------+------+
1 row in set (Elapsed: 00:00:00.01)

少量数据子查询多列in

如果数值稍,比如几千个,几万个,可以使用子查询的格式。 示例如下:
where (a,b) in (select a, b from XXX)

实际例子如下

gbase> select * from tt1 where (id,id2) in (select id,id3 from tt1 where id<=2);
+------+------+------+
| id   | id2  | id3  |
+------+------+------+
|    2 | -110 | -110 |
+------+------+------+
1 row in set (Elapsed: 00:00:00.11)

大量数据多列in

外部数据来源

如果数据是从其它环境获取的,那么建议创建临时表,将数据insert或者load进去,然后按照JOIN的方式进行操作

gbase> create temporary table tmp_tt1(id int, id2 int);
Query OK, 0 rows affected (Elapsed: 00:00:01.10)

gbase> insert into tmp_tt1 values(0,-110),(1,3);
Query OK, 2 rows affected (Elapsed: 00:00:00.44)
Records: 2  Duplicates: 0  Warnings: 0

gbase> select a.* from tt1 a ,(select id,id2 from tmp_tt1) b where a.id=b.id and a.id2=b.id2;
+------+------+------+
| id   | id2  | id3  |
+------+------+------+
|    0 | -110 | -129 |
+------+------+------+
1 row in set (Elapsed: 00:00:00.06)

gbase> drop temporary table tmp_tt1;
Query OK, 0 rows affected (Elapsed: 00:00:00.19)

gbase>

库内数据来源

数据在库内,直接JOIN即可

gbase> select a.* from tt1 a ,(select id,id3 from tt1 where id<=2) b where a.id=b.id and a.id2=b.id3;
+------+------+------+
| id   | id2  | id3  |
+------+------+------+
|    2 | -110 | -110 |
+------+------+------+
1 row in set (Elapsed: 00:00:00.08)