更新时间:2024-04-25 GMT+08:00
分享

为子计划结果进行物化的Hint

功能描述

为子计划结果进行物化,暂存查询记录。只在insert语句应用。

在使用INSERT INTO … SELECT语句插入大量数据且有多行重复值时,因索引需多次对比而导致执行时间过长。使用此HINT对子计划的结果进行物化,暂存查询记录,减少索引比较次数,缩短语句执行时间。

语法格式

material_subplan

示例

建表并插入数据:

create table test_mt_sub(a int, b int) with(storage_type = ustore);
create index on test_mt_sub(a);
create table test_src_mt_sub(a int, b int);
insert into test_src_mt_sub values(generate_series(1,10), generate_series(1,100000));
正常的insert into...select语句:
insert into test_mt_sub select /*+ nestloop(test_src_mt_sub t1)*/ * from test_src_mt_sub where notexists(select 1 from test_mt_sub t1 where t1.a = test_src_mt_sub.a);
执行计划:
                              QUERY PLAN                               
-----------------------------------------------------------------------
 Insert on test_mt_sub
   ->  Nested Loop Anti Join
         ->  Seq Scan on test_src_mt_sub
         ->  Index Only Scan using test_mt_sub_a_idx on test_mt_sub t1
               Index Cond: (a = test_src_mt_sub.a)
(5 rows)
使用material_subplan hint 算子:
insert /*+ material_subplan*/ into test_mt_sub select /*+ nestloop(test_src_mt_sub t1)*/ * from test_src_mt_sub where not exists(select 1 from test_mt_sub t1 where t1.a = test_src_mt_sub.a);
执行计划为:
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Insert on test_mt_sub
   ->  Materialize
         ->  Nested Loop Anti Join
               ->  Seq Scan on test_src_mt_sub
               ->  Index Only Scan using test_mt_sub_a_idx on test_mt_sub t1
                     Index Cond: (a = test_src_mt_sub.a)
(6 rows)
分享:

    相关文档

    相关产品