返回首页

gbase数据、南大通用产品文档:GBase8a

更新日期:2024年09月11日

界面刷新......................................... 29

DB-Access 实用程序随 GBase 8s 数据库服务器产品一起提供,它包括一个或多个以下演
示数据库:

stores_demo 数据库以一家虚构的体育用品批发商的有关信息举例说明了关系模式。
GBase 8s 出版物中的许多示例均基于 stores_demo 数据库。

superstores_demo 数据库举例说明了对象关系模式。superstores_demo 数据库包
含扩展数据类型、类型和表继承以及用户定义的例程的示例。
有关如何创建和填充演示数据库的信息,请参阅《GBase 8s DB-Access 用户指南》。有关
数据库及其内容的描述,请参阅《GBase 8s SQL 指南:参考》。

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 2 -
用于安装演示数据库的脚本位于 UNIX™ 平台上的 $GBASEDBTDIR/bin 目录和
Windows™ 环境中的 %GBASEDBTDIR%\bin 目录中。

rewrite_rule 包含了多个查询重写规则:magicset、partialpush、uniquecheck、disablerep、
intargetlist、predpush。下面简要说明一下其中重要的几个规则的使用场景:
目标列子查询提升参数intargetlist
通过将目标列中子查询提升,转为JOIN,往往可以极大提升查询性能。举例如下查询:
gsql=# set rewrite_rule='none';

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
604
SET
gsql=# create table t1(c1 int,c2 int);
CREATE TABLE
gsql=# create table t2(c1 int,c2 int);
CREATE TABLE
gsql=#
explain (verbose on, costs off) select c1,(select avg(c2) from t2 where t2.c2=t1.c2)
from t1 where t1.c1<100 order by t1.c2;
QUERY PLAN
-----------------------------------------------
Sort
Output: t1.c1, ((SubPlan 1)), t1.c2
Sort Key: t1.c2
->
Seq Scan on public.t1
Output: t1.c1, (SubPlan 1), t1.c2
Filter: (t1.c1 < 100)
SubPlan 1
->
Aggregate
Output: avg(t2.c2)
->
Seq Scan on public.t2
Output: t2.c1, t2.c2
Filter: (t2.c2 = t1.c2)
(12 rows)
由于目标列中的相关子查询(select avg(c2) from t2 where t2.c2=t1.c2)无法提升的缘故,

致每扫描t1 的一行数据,
就会触发子查询的一次执行,
效率低下。
如果打开intargetlist 参数
会把子查询提升转为JOIN,来提升查询的性能:
gsql=# set rewrite_rule='intargetlist';
SET
gsql=#
explain (verbose on, costs off) select c1,(select avg(c2) from t2 where t2.c2=t1.c2)
from t1 where t1.c1<100 order by t1.c2;
QUERY PLAN
-----------------------------------------------
Sort
Output: t1.c1, (avg(t2.c2)), t1.c2
Sort Key: t1.c2
->
Hash Left Join
Output: t1.c1, (avg(t2.c2)), t1.c2
Hash Cond: (t1.c2 = t2.c2)
->
Seq Scan on public.t1
Output: t1.c1, t1.c2
Filter: (t1.c1 < 100)

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
605
->
Hash
Output: (avg(t2.c2)), t2.c2
->
HashAggregate
Output: avg(t2.c2), t2.c2
Group By Key: t2.c2
->
Seq Scan on public.t2
Output: t2.c2
(16 rows)
提升无agg 的子查询uniquecheck
子链接提升需要保证对于每个条件只有一行输出,对于有agg 的子查询可以自动提升,
对于无agg 的子查询如:
select t1.c1 from t1 where t1.c1 = (select t2.c1 from t2 where t1.c1=t2.c2);
重写为:
select t1.c1 from t1 join (select t2.c1 from t2 where t2.c1 is not null group by t2.c1(unique
check)) tt(c1) on tt.c1=t1.c1;
为了保证语义等价,子查询tt 必须保证对于每个group by t2.c1 只能有一行输出。打开
uniquecheck 查询重写参数保证可以提升并且等价,如果在运行时输出了多于一行的数据,
就会报错。
gsql=# set rewrite_rule='uniquecheck';
SET
gsql=# explain verbose select t1.c1 from t1 where t1.c1 = (select t2.c1 from t2 where
t1.c1=t2.c1);
QUERY PLAN
-------------------------------------------------------------------------------------
Hash Join
(cost=43.36..104.40 rows=2149 distinct=[200, 200] width=4)
Output: t1.c1
Hash Cond: (t1.c1 = subquery."?column?")
->
Seq Scan on public.t1
(cost=0.00..31.49 rows=2149 width=4)
Output: t1.c1, t1.c2
->
Hash
(cost=40.86..40.86 rows=200 width=8)
Output: subquery."?column?", subquery.c1
->
Subquery Scan on subquery
(cost=36.86..40.86 rows=200 width=8)
Output: subquery."?column?", subquery.c1
->
HashAggregate
(cost=36.86..38.86 rows=200 width=4)
Output: t2.c1, t2.c1
Group By Key: t2.c1
Filter: (t2.c1 IS NOT NULL)

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
606
Unique Check Required
->
Seq Scan on public.t2
(cost=0.00..31.49 rows=2149 width=4)
Output: t2.c1
(16 rows)
注意:因为分组group by t2.c1 unique check 发生在过滤条件tt.c1=t1.c1 之前,可能导致
原来不报错的查询重写之后报错。举例:
有t1、t2 表,其中的数据为:
gsql=# select * from t1 order by c2;
c1 | c2
----+----
1 |
1
2 |
2
3 |
3
(3 rows)
gsql=# select * from t2 order by c2;
c1 | c2
----+----
1 |
1
2 |
2
3 |
3
4 |
4
4 |
4
5 |
5
(6 rows)
分别关闭和打开uniquecheck 参数对比,打开之后报错。
gsql=# select t1.c1 from t1 where t1.c1 = (select t2.c1 from t2 where t1.c1=t2.c2) ;
c1
----
1
2
3
(3 rows)
gsql=# set rewrite_rule='uniquecheck';
SET
gsql=# select t1.c1 from t1 where t1.c1 = (select t2.c1 from t2 where t1.c1=t2.c2) ;
ERROR:
more than one row returned by a subquery used as an expression

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
607