更新时间: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。

透明路由的使用可参考混合负载透明路由

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

--建表、插入数据后加载IMCV。
gaussdb=# CREATE TABLE htap_test(
          id int,
          dept_id int,
          salary int,
          name varchar(20),
          comments varchar(30)) with (storage_type = ustore);
CREATE TABLE
gaussdb=# INSERT INTO htap_test VALUES (generate_series(1,100), 1, 8000, 'Allen', 'For test');
INSERT 0 100
gaussdb=# ALTER TABLE htap_test COLVIEW;
ALTER TABLE
gaussdb=# SELECT * FROM gs_imcv;
 reloid |  relname  |  dbname  | username | parentoid | imcvispart | imcvnattr |  imcvkey  | priority | childoids | dboid | rootoid | schemaname 
--------+-----------+----------+----------+-----------+------------+-----------+-----------+----------+-----------+-------+---------+------------
  33121 | htap_test | postgres | htap     |         0 | f          |         5 | 1 2 3 4 5 |        1 |           | 13347 |   33121 | public
(1 row)

--由于HTAP路由模式为auto,透明路由基于代价计算选择行存的执行计划。
gaussdb=# EXPLAIN SELECT count(*) FROM htap_test;
                            QUERY PLAN
------------------------------------------------------------------
 Aggregate  (cost=15.74..15.75 rows=1 width=8)
   ->  Seq Scan on htap_test  (cost=0.00..14.59 rows=459 width=0)
(2 rows)

--IMCV hint指定IMCV列存的执行计划。
gaussdb=# EXPLAIN SELECT /*+imcvscan(htap_test)*/  count(*) FROM htap_test;
                                QUERY PLAN
--------------------------------------------------------------------------
 Row Adapter  (cost=31.72..31.72 rows=1 width=8)
   ->  Vector Aggregate  (cost=31.71..31.72 rows=1 width=8)
         ->  Imcv Scan on htap_test  (cost=30.00..30.56 rows=459 width=0)
(3 rows)

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

--HTAP路由模式为column时,强制选择IMCV列存的执行计划。
gaussdb=# EXPLAIN SELECT count(*) FROM htap_test;
                                QUERY PLAN
--------------------------------------------------------------------------
 Row Adapter  (cost=31.72..31.72 rows=1 width=8)
   ->  Vector Aggregate  (cost=31.71..31.72 rows=1 width=8)
         ->  Imcv Scan on htap_test  (cost=30.00..30.56 rows=459 width=0)
(3 rows)

gaussdb=# DROP TABLE htap_test;
DROP TABLE

相关文档