更新时间:2026-07-28 GMT+08:00
分享

DQL

在正确设置参数以及对目标表创建IMCV后便可进行DQL查询。在htap_router_mode为auto时,HTAP透明路由会根据数据和SQL特征选择最优执行计划,因此即使对目标表加载了IMCV,透明路由仍有可能会根据代价计算选择行存计划,具体执行计划可通过EXPLAIN查看。

如果希望SQL执行时一定走IMCV列存计划,可以设置htap_router_mode为column或者在SQL语句中设置IMCV hint。

透明路由的使用请参见混合负载透明路由

示例:
htapdb=# SET htap_router_mode='auto';
SET
htapdb=# SHOW htap_router_mode;
 htap_router_mode
------------------
 auto
(1 row)

--建表、插入数据后加载IMCV。
htapdb=# CREATE TABLE htap_test(
          id int,
          dept_id int,
          salary int,
          name varchar(20),
          comments varchar(30)) WITH (storage_type = ustore) DISTRIBUTE BY HASH(id);
CREATE TABLE
htapdb=# INSERT INTO htap_test VALUES (generate_series(1,100), 1, 8000, 'Allen', 'For test');
INSERT 0 100
htapdb=# ALTER TABLE htap_test COLVIEW;
ALTER TABLE
htapdb=# SELECT * FROM gs_imcv;
 reloid |  relname  | dbname | username | parentoid | imcvispart | imcvnattr |  imcvkey  | priority
--------+-----------+--------+----------+-----------+------------+-----------+-----------+----------
  16982 | htap_test | test   | dbdev    |         0 | f          |         5 | 1 2 3 4 5 |        1
(1 row)

--由于HTAP路由模式为auto,透明路由基于代价计算选择行存的执行计划。
htapdb=# EXPLAIN SELECT count(*) FROM htap_test;
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Aggregate  (cost=13.23..13.27 rows=1 width=8)
   ->  Streaming (type: GATHER)  (cost=13.23..13.27 rows=2 width=8)
         Node/s: All datanodes
         ->  Aggregate  (cost=13.16..13.17 rows=2 width=8)
               ->  Seq Scan on htap_test  (cost=0.00..13.13 rows=20 width=0)
(5 rows)

--IMCV hint指定IMCV列存的执行计划。
htapdb=# EXPLAIN SELECT /*+imcvscan(htap_test)*/  count(*) FROM htap_test;
                                     QUERY PLAN                                      
-------------------------------------------------------------------------------------
 Row Adapter  (cost=32.28..32.28 rows=1 width=8)
   ->  Vector Aggregate  (cost=32.24..32.28 rows=1 width=8)
         ->  Vector Streaming (type: GATHER)  (cost=32.24..32.28 rows=2 width=8)
               Node/s: All datanodes
               ->  Vector Aggregate  (cost=32.17..32.18 rows=2 width=8)
                     ->  Imcv Scan on htap_test  (cost=0.00..32.05 rows=100 width=0)
(6 rows)

htapdb=# SET htap_router_mode='column';
SET
htapdb=# SHOW htap_router_mode;
 htap_router_mode
------------------
 column
(1 row)

--HTAP路由模式为column时,强制选择IMCV列存的执行计划。
htapdb=# EXPLAIN SELECT count(*) FROM htap_test;
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Aggregate  (cost=2.69..2.73 rows=1 width=8)
   ->  Streaming (type: GATHER)  (cost=2.69..2.73 rows=2 width=8)
         Node/s: All datanodes
         ->  Aggregate  (cost=2.62..2.63 rows=2 width=8)
               ->  Seq Scan on htap_test  (cost=0.00..2.50 rows=100 width=0)
(5 rows)

htapdb=# DROP TABLE htap_test;
DROP TABLE

相关文档