gbase8s sql优化之星型连接

1 何为星型连接

星型连接 主要为 一个事实表与多个维度表的join 连接

学生其他属性...

|

学生住址表\] -- \[学生表(事实表)\] -- \[分数表

|

学生信息表

以学生表为中心,像四周发射,就像星星一样

2 gbase8s星型连接的前置条件

2.1 必须开启PDQ

2.2 查询中的所有表必须至少具有低级别统计信息

2.3 只能有一张事实表

3 对比测试

复制代码
create table student(id varchar(50),name varchar(20));

create table jbxx(id  varchar(50),addr  varchar(50));

create table score(id  varchar(50),total_sc int);

create table sx01(id  varchar(50),v_x01  varchar(50));

create table sx02(id  varchar(50),v_x02  varchar(50));

create table sx03(id  varchar(50),v_x03 varchar(50));
create index index_s_id on student(id);
create index index_jbxx_id on jbxx(id);
create index index_score_id on score(id);
create index index_sx01_id on sx01(id);
create index index_sx02_id on sx02(id);
create index index_sx03_id on sx03(id);

drop function if exists ff1;
create function ff1() returning int
define a int;
let a= DBINFO('utc_current');  
let a= (IFX_BIT_RIGHTSHIFT(a,5)) + (IFX_BIT_LEFTSHIFT(a,7));
let a= BITAND(a,2147483647);
let a=mod(a,101);
return a;
end function;

insert into student select sys_guid(),'N_'||sys_guid() from dual connect by level <=1000000;

insert into jbxx select id,'ADDR_'||sys_guid() from student;

insert into score select id,ff1() from student;

insert into sx01 select id,'XX01_'||sys_guid() from student;

insert into sx02 select id,'XX02_'||sys_guid() from student;

insert into sx03 select id,'XX03_'||sys_guid() from student;

执行如下sql
select count(1) from (
select s.*,a.addr,b.total_sc,c.v_x01,d.v_x02,e.v_x03 from 
student s
join jbxx a on a.id=s.id 
join score b on b.id=s.id 
join sx01 c on c.id=s.id 
join sx02 d on d.id=s.id 
join sx03 e on e.id=s.id 
where b.total_sc >80
);

正常查询的执行耗时

复制代码
 select count(1) from (
select s.*,a.addr,b.total_sc,c.v_x01,d.v_x02,e.v_x03 from 
student s
join jbxx a on a.id=s.id 
join score b on b.id=s.id 
join sx01 c on c.id=s.id 
join sx02 d on d.id=s.id 
join sx03 e on e.id=s.id 
where b.total_sc >80
);> > > > > > > > > 


             (count) 

             1000000

1 row(s) retrieved.

Elapsed time: 56.719 sec

开启优化后

执行时间

复制代码
select count(1) from (
select /*+FACT(student),STAR_JOIN*/ s.*,a.addr,b.total_sc,c.v_x01,d.v_x02,e.v_x03 from 
student s
join jbxx a on a.id=s.id 
join score b on b.id=s.id 
join sx01 c on c.id=s.id 
join sx02 d on d.id=s.id 
join sx03 e on e.id=s.id 
where b.total_sc >80
);> > > > > > > > > 


             (count) 

             1000000

1 row(s) retrieved.

Elapsed time: 19.503 sec

执行计划对比

复制代码
QUERY: (OPTIMIZATION TIMESTAMP: 07-03-2025 19:24:51)
------
select count(1) from (
select s.*,a.addr,b.total_sc,c.v_x01,d.v_x02,e.v_x03 from 
student s
join jbxx a on a.id=s.id 
join score b on b.id=s.id 
join sx01 c on c.id=s.id 
join sx02 d on d.id=s.id 
join sx03 e on e.id=s.id 
where b.total_sc >80
)

Estimated Cost: 3706817
Estimated # of Rows Returned: 1

  1) gbasedbt.c: SEQUENTIAL SCAN

  2) gbasedbt.b: INDEX PATH

        Filters: gbasedbt.b.total_sc > 80 

    (1) Index Name: gbasedbt.index_score_id
        Index Keys: id   (Serial, fragments: ALL)
        Lower Index Filter: gbasedbt.b.id = gbasedbt.c.id 
NESTED LOOP JOIN

  3) gbasedbt.a: INDEX PATH

    (1) Index Name: gbasedbt.index_jbxx_id
        Index Keys: id   (Serial, fragments: ALL)
        Lower Index Filter: gbasedbt.a.id = gbasedbt.b.id 
NESTED LOOP JOIN

  4) gbasedbt.d: INDEX PATH

    (1) Index Name: gbasedbt.index_sx02_id
        Index Keys: id   (Serial, fragments: ALL)
        Lower Index Filter: gbasedbt.b.id = gbasedbt.d.id 
NESTED LOOP JOIN

  5) gbasedbt.s: INDEX PATH

    (1) Index Name: gbasedbt.index_s_id
        Index Keys: id   (Serial, fragments: ALL)
        Lower Index Filter: gbasedbt.b.id = gbasedbt.s.id 
NESTED LOOP JOIN

  6) gbasedbt.e: INDEX PATH

    (1) Index Name: gbasedbt.index_sx03_id
        Index Keys: id   (Serial, fragments: ALL)
        Lower Index Filter: gbasedbt.b.id = gbasedbt.e.id 
NESTED LOOP JOIN


Query statistics:
-----------------

  Table map :
  ----------------------------
  Internal name     Table name
  ----------------------------
  t1                c
  t2                b
  t3                a
  t4                d
  t5                s
  t6                e

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t1     1000000    1000000   1000000    00:00.50   34718    

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t2     1000000    1000000   1000000    00:20.32   1        

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   1000000    1000000   00:21.00   768495  

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t3     1000000    1000000   1000000    00:20.41   1        

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   1000000    1000001   00:41.59   1503324 

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t4     1000000    1000000   1000000    00:17.92   1        

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   1000000    1000002   00:59.70   2238154 

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t5     1000000    1000000   1000000    00:23.22   1        

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   1000000    1000002   01:23.14   2971987 

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t6     1000000    1000000   1000000    00:19.98   1        

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   1000000    1000002   01:43.47   3706818 

  type     rows_prod  est_rows  rows_cons  time
  -------------------------------------------------
  group    1          1         1000000    01:43.91


QUERY: (OPTIMIZATION TIMESTAMP: 07-03-2025 19:27:15)
------
select count(1) from (
select /*+FACT(student),STAR_JOIN*/ s.*,a.addr,b.total_sc,c.v_x01,d.v_x02,e.v_x03 from 
student s
join jbxx a on a.id=s.id 
join score b on b.id=s.id 
join sx01 c on c.id=s.id 
join sx02 d on d.id=s.id 
join sx03 e on e.id=s.id 
where b.total_sc >80
)

DIRECTIVES FOLLOWED: 
FACT ( student )
STAR_JOIN 
DIRECTIVES NOT FOLLOWED: 

Estimated Cost: 12268261
Estimated # of Rows Returned: 1
Maximum Threads: 14

  1) gbasedbt.s: INDEX PATH (SKIP SCAN)

    (1) Index Name: gbasedbt.index_s_id
        Index Keys: id   (Parallel, fragments: ALL)
        Lower Index Filter: gbasedbt.s.id = stream from gbasedbt.e.id 


      2) gbasedbt.e: SEQUENTIAL SCAN

          3) gbasedbt.d: SEQUENTIAL SCAN

              4) gbasedbt.c: SEQUENTIAL SCAN

                  5) gbasedbt.b: SEQUENTIAL SCAN

                        Filters: gbasedbt.b.total_sc > 80 

                  6) gbasedbt.a: SEQUENTIAL SCAN


                DYNAMIC HASH JOIN 
                    Dynamic Hash Filters: gbasedbt.a.id = gbasedbt.b.id 


            DYNAMIC HASH JOIN 
                Dynamic Hash Filters: gbasedbt.a.id = gbasedbt.c.id 


        DYNAMIC HASH JOIN 
            Dynamic Hash Filters: gbasedbt.a.id = gbasedbt.d.id 


    DYNAMIC HASH JOIN 
        Dynamic Hash Filters: gbasedbt.a.id = gbasedbt.e.id 


DYNAMIC HASH JOIN (Index Push Down Key: gbasedbt.e.id to gbasedbt.s)
    Dynamic Hash Filters: gbasedbt.a.id = gbasedbt.s.id 


Query statistics:
-----------------

  Table map :
  ----------------------------
  Internal name     Table name
  ----------------------------
  t1                s
  t2                e
  t3                d
  t4                c
  t5                b
  t6                a

  type     rows_sort  est_rows  rows_cons  time
  -------------------------------------------------
  sort     1000000    1000000   1000000    00:09.59

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t1     1000000    1000000   1000000    00:19.29   680842   

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t2     1000000    1000000   1000000    00:00.48   34718    

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t3     1000000    1000000   1000000    00:00.57   34718    

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t4     1000000    1000000   1000000    00:00.63   34718    

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t5     1000000    1000000   1000000    00:00.59   33923    

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t6     1000000    1000000   1000000    00:00.37   34718    

  type     rows_prod  est_rows  rows_bld  rows_prb  novrflo  time       est_cost
  ------------------------------------------------------------------------------
  hjoin    1000000    1000000   1000000   1000000   0        00:05.14   1602418 

  type     rows_prod  est_rows  rows_bld  rows_prb  novrflo  time       est_cost
  ------------------------------------------------------------------------------
  hjoin    1000000    1000001   1000000   1000000   0        00:08.52   3792340 

  type     rows_prod  est_rows  rows_bld  rows_prb  novrflo  time       est_cost
  ------------------------------------------------------------------------------
  hjoin    1000000    1000002   1000000   1000000   0        00:11.88   6216174 

  type     rows_prod  est_rows  rows_bld  rows_prb  novrflo  time       est_cost
  ------------------------------------------------------------------------------
  hjoin    1000000    1000002   1000000   1000000   0        00:15.76   8901948 

  type     rows_prod  est_rows  rows_bld  rows_prb  novrflo  time       est_cost
  ------------------------------------------------------------------------------
  hjoin    1000000    1000002   1000000   1000000   0        00:34.41   12268261

  type     rows_prod  est_rows  rows_cons  time
  -------------------------------------------------
  group    1          1         1000000    00:35.72

  type     rows_prod  est_rows  rows_cons  time
  -------------------------------------------------
  group    1          1         1          00:35.72