.. _使用Query Mapping:

==================
使用Query Mapping
==================

本章节包含以下内容：

- 概述_
- `Query Mapping的功能`_
- `Query Mapping的使用`_
- 示例_




概述
---------

Query Mapping功能允许用户预先创建SQL语句的映射关系并储存在相应的系统表，
当用户输入的SQL语句与所创建的映射关系相匹配时，替换成映射的SQL语句去实际执行。

Query Mapping的功能
-------------------

Query Mapping主要作用是做SQL的匹配/替换工作，在数据库中配置源SQL语句到目标SQL语句的映射关系（存在系统表），根据客户的输入sql语句去做匹配，如果匹配，就替换成目标语句进行执行，它有两种级别：

-  TEXT级别：不做词法/语法/语义检查，保存用户的原始sql和期望mapping的目标sql，进行字符串匹配

  .. figure:: images/sql-optimization_image22.png
     :scale: 75 % 
     :alt: map to buried treasure


-  SEMANTICS级别：做词法/语法/语义语义检查，并保存用户原始/目标sql对应的Query tree

  .. figure:: images/sql-optimization_image23.png
     :scale: 75 % 
     :alt: map to buried treasure


它的可用场景为：

SQL调优：将低效SQL语句转换成另一高效的等价SQL语句

异构数据库迁移：将源数据库的SQL语法翻译成KingbaseES库的SQL语法

注意：

1. 如果希望把Query Mapping功能与Hint功能配合使用，那么需要使用TEXT级别的规则，
因为Hint使用的是注释来保存Hint具体内容，TEXT级别会保存SQL的注释；而对于SEMANTICS级别，由于保存的是Query tree
所以无法保存注释，也就无法与Hint功能配合使用。

2. 对于SEMANTICS级别的Query mapping，
目前\ **不支持**\ 带有rownum的语句映射

Query Mapping的使用
-------------------

Query Mapping的使用方法如下：

1. 在kingbase.conf配置文件中，配置：enable_query_rule = on

2. 启动数据库

3. 使用系统函数create_query_rule()来新增匹配规则，如：


  .. code::

   SELECT create_query_rule(
   'qm1', -- 规则名
   'select $1::TEXT AS col', -- 匹配的sql
   'select 2222', -- 需要替换的sql
   true, -- 该规则是否生效
   'semantics' -- 级别，text或者semantics
   );



4. 接下来如果用户输入：select 'aaa' as col; 那么就会发生替换，其结果为：


  .. code::

   ?column?
   ----------
   2222
   (1 row)


5. 如果需要删除该规则，使用SELECT drop_query_rule('qm1');-- 入参qm1为规则名

6. 如果需要使原本不生效的规则生效，可以使用SELECT enable_query_rule('qm1');-- 入参qm1为规则名

7. 如果需要使原本生效的规则失效，可以使用SELECT disable_query_rule('qm1');-- 入参qm1为规则名

Query Mapping的所有对外函数表如下：

.. table:: Query  Mapping特性相关函数函数
   :class: longtable

   +--------------------------+--------------+--------------------------+
   | **名称**                 | **返回类型** | **描述**                 |
   +--------------------------+--------------+--------------------------+
   | create_query_mapping_rule| void         | 创建一条Query mapping    |
   | (text text text bool     |              | 规则                     |
   | text)                    |              |                          |
   +--------------------------+--------------+--------------------------+
   | drop_query_rule(text)    | void         | 根据传入的规则名，       |
   |                          |              | 删除一条Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+
   | drop_query_rule()        | void         | 删除所有Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+
   | enable_query_rule(text)  | void         | 根据传入的规则名，       |
   |                          |              | 启用一条Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+
   | enable_query_rule()      | void         | 启用所有Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+
   | disable_query_rule(text) | void         | 根据传入的规则名，       |
   |                          |              | 禁用一条Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+
   | disable_query_rule()     | void         | 禁用所有Query mapping    |
   |                          |              | 规则                     |
   +--------------------------+--------------+--------------------------+


示例
----

准备数据：

  .. code::

   create table t1(id int primary key, val int, name varchar(64));
   create table t2(id int, val int, name varchar(64));
   insert into t1 select i, i%10, 'Kingbase'||(i%5) from generate_series(1,1000) as x(i);
   insert into t2 select i, i%20, 'Kingbase'||(i%10) from generate_series(1,1000) as x(i);


1) 单个$变量对单个$变量Query
Mapping规则，使用规则之前数据有9行2列，使用规则之后，数据变成4行1列：

  .. code::

   select id,val from t1 where id<10;
   id  | val
   ----+-----
   1   | 1
   2   | 2
   3   | 3
   4   | 4
   5   | 5
   6   | 6
   7   | 7
   8   | 8
   9   | 9
   (9 rows)
   
   
   SELECT create_query_rule('qm1', 'select id,val from t1 where id<$1','select id from t1 where id<($1-5)', true, 'text');
   create_query_rule;
   ------------------
   (1 row)
   
   
   select id,val from t1 where id<10;
   id
   ----
   1
   2
   3
   4
   (4 rows)


2) 多个$变量对单个$变量Query
Mapping规则，使用规则之前数据有2行2列，使用规则之后，数据变成了val=5的总数，即count的值：

  .. code::

   select id,val from t1 where id<20 and id>3 and val=5;
   id | val
   ---+-----
   5  | 5
   15 | 5
   (2 rows)
   
   SELECT create_query_rule('qm1', 'select id,val from t1 where id<$1 and id>$2 and val=$3', 'select count(0) from t1 where val=$3', true, 'text');
   create_query_rule
   ------------------- 
   (1 row)
   
   
   select id,val from t1 where id<20 and id>3 and val=5;
   count
   -------
   100
   (1 row)



3)单个$变量对多个$变量Query
Mapping规则，使用之前是11行6列数据，使用规则后对表t2进行过滤，只有1行6列。

  .. code::

   select * from t1, t2 where t1.id=t2.id and t1.id < 12;
   id | val | name       | id  | val | name
   ---+-----+------------+-----+-----+----------
   1  |  1  | Kingbase1  |  1  |  1  | Kingbase1
   2  |  2  | Kingbase2  |  2  |  2  | Kingbase2
   3  |  3  | Kingbase3  |  3  |  3  | Kingbase3
   4  |  4  | Kingbase4  |  4  |  4  | Kingbase4
   5  |  5  | Kingbase0  |  5  |  5  | Kingbase5
   6  |  6  | Kingbase1  |  6  |  6  | Kingbase6
   7  |  7  | Kingbase2  |  7  |  7  | Kingbase7
   8  |  8  | Kingbase3  |  8  |  8  | Kingbase8
   9  |  9  | Kingbase4  |  9  |  9  | Kingbase9
   10 |  0  | Kingbase0  |  10 |  10 | Kingbase0
   11 |  1  | Kingbase1  |  11 |  11 | Kingbase1
   (11 rows)
   
   
   SELECT create_query_rule('qm1', 'select * from t1, t2 where t1.id=t2.id and t1.id<$1', 'select  * from t1, t2 where t1.id=t2.id and t1.id>($1-5) and t1.id<$1 and t2.val>($1-2) and t2.val<($1+2)', true, 'text');
   create_query_rule
   -------------------
   (1 row)
   select * from t1, t2 where t1.id=t2.id and t1.id < 12;
   id  | val |    name   | id  | val | name
   ----+-----+-----------+-----+-----+-----------
   11  |  1  | Kingbase1 | 11  | 11  | Kingbase1 
   (1 row)



4)多个$变量对多个$变量Query
Mapping规则，使用前是2行6列数据，使用规则后变成1行6列。

  .. code::

   select  * from t1, t2 where t1.id=t2.id and t1.id<30 and t2.val=5;
   id  | val |    name   | id | val | name
   ----+-----+-----------+----+-----+-----------
   5   |  5  | Kingbase0 | 5  |  5  | Kingbase5
   25  |  5  | Kingbase0 | 25 |  5  | Kingbase5
   (2 rows)
   
   
   SELECT create_query_rule('qm1', 'select * from t1, t2 where t1.id=t2.id and t1.id<$1 and t2.val=$2', 'select * from t1, t2 where t1.id=t2.id and t1.id<$1 and t2.val=$2 and t2.id=$2', true, 'text');
   create_query_rule
   -------------------
   (1 row)
   
   
   select * from t1, t2 where t1.id=t2.id and t1.id<30 and t2.val=5;
   id  | val |    name   | id | val | name
   ----+-----+-----------+----+-----+-----------
   5   |  5  | Kingbase0 | 5  |  5  | Kingbase5
   (1 row)



5)$变量的交换规则：

  .. code::

   select  * from t2 where t2.id<50 and t2.val=5;
   id  | val | name
   ----+-----+-----------
   5   |  5  | Kingbase5
   25  |  5  | Kingbase5
   45  |  5  | Kingbase5
   (3 rows)
   
   
   SELECT create_query_rule('qm1', 'select * from t2 where t2.id<$1 and t2.val=$2', 'select * from t2 where t2.id<$2 and t2.val=$1', true, 'text');
   create_query_rule
   -------------------
   (1 row)
   
   
   select * from t2 where t2.id<50 and t2.val=5;
   id  | val  | name
   ----+-----+------
   (0 rows)



6)条件下推Query Mapping规则，通过Query
Mapping规则可以将外层连接条件下推到union子查询中。

  .. code::

   explain SELECT count(0) FROM t1, (select  * from t2 where t2.val>5 union select  * from t1 where t1.val < 20) as v where t1.id=v.id;
   
   QUERY PLAN
   ------------------------------------------------------------------
   Aggregate (cost=134.48..134.49 rows=1 width=8)
       -> Hash Join (cost=91.75..130.23 rows=1700 width=0)
           Hash Cond: (t2.id = t1.id)
           -> HashAggregate (cost=67.25..84.25 rows=1700 width=154)
               Group Key: t2.id, t2.val, t2.name
               -> Append (cost=0.00..54.50 rows=1700 width=154)
                   -> Seq Scan on t2 (cost=0.00..14.50 rows=700 width=18)
                       Filter: (val > 5)
                   -> Seq Scan on t1 t1_1 (cost=0.00..14.50 rows=1000 width=18)
                       Filter: (val < 20)
           -> Hash (cost=12.00..12.00 rows=1000 width=4)
               -> Seq Scan on t1 (cost=0.00..12.00 rows=1000 width=4)
   (12 rows)
   
   
   SELECT create_query_rule('qm1', 'SELECT count(0) FROM t1, (select * from t2 where t2.val>$1 union select  * from t1 where t1.val<$2) as v where t1.id=v.id;
   SELECT count(0) FROM t1, lateral(select  * from t2 where t2.val>$1 and t1.id=t2.id union select  * from t1 t where t.val<$2 and t.id=t1.id) as v;', true, 'text');
   create_query_rule
   -------------------
   (1 row)
   
   
   explain (usingquerymapping) SELECT count(0) FROM t1, (select * from t2 where t2.val>5 union select  * from t1 where t1.val < 20) as v where t1.id=v.id;
   
   QUERY PLAN
   ------------------------------------------------------------------
   Aggregate (cost=25287.00..25287.01 rows=1 width=8)
       -> Nested Loop (cost=25.21..25282.00 rows=2000 width=0)
           -> Seq Scan on t1 (cost=0.00..12.00 rows=1000 width=4)
           -> Unique (cost=25.21..25.23 rows=2 width=154)
               -> Sort (cost=25.21..25.22 rows=2 width=154)
                   Sort Key: t2.id, t2.val, t2.name
                   -> Append (cost=0.00..25.20 rows=2 width=154)
                       -> Seq Scan on t2 (cost=0.00..17.00 rows=1 width=18)
                           Filter: ((val > 5) AND (t1.id = id))
                       -> Index Scan using t1_pkey on t1 t (cost=0.15..8.17 rows=1 width=18)
                           Index Cond: (id = t1.id)
                           Filter: (val < 20)
   (12 rows)



7)CTE Query Mapping规则，查询语句被映射成with recursive处理：


  .. code::

   select * from t1 where id = 10;
   id  | val  | name
   ----+-----+-----------
   10  | 0  | Kingbase0
   (1 row)
   
   
   select create_query_rule('qm1', 'select * from t1 where id = $1;','with recursive rtmp as(select id,val from t1 where id=$1 UNION select t1.id,t1.val from t1,rtmp where rtmp.id = t1.val)select * from rtmp;',true,'text');
   create_query_rule
   -------------------
   (1 row)
   
   
   select * from t1 where id = 10;
   id  | val
   ----+-----
   10  | 0
   (1 row)


