调用存储过程dbms_imci.columnar_advise(),可以针对某条SQL语句来获取对应的DDL语句,执行获取到的DDL语句可以为未被列索引覆盖的列添加列索引,按顺序执行完DDL语句后,即可保证这条SQL语句中使用的所有列都被列索引覆盖。
前提条件
- PolarDB集群版本需满足以下条件之一: - PolarDB MySQL版8.0.1版本且修订版本为8.0.1.1.30及以上。 
- PolarDB MySQL版8.0.2版本且修订版本为8.0.2.2.12及以上。 
 
- 您需要具有所操作的表的读(SELECT)权限。 
语法
- 获取按表添加列存索引的DDL语句。 - dbms_imci.columnar_advise('<query_string>');
- 获取按列添加列存索引的DDL语句。 - dbms_imci.columnar_advise_by_columns('<query_string>');
参数说明
| 参数 | 说明 | 
| query_string | 需要解析的SQL语句。 说明  
 | 
注意事项
调用该存储过程仅获取DDL语句,并不会执行DDL语句。
示例
以t1和t2表为例,获取创建列存索引的DDL语句。
- 执行如下命令,切换至 - test库。- use test;
- 执行如下命令,创建 - t1和- t2表。- create table t1 (a int, b int) engine = innodb; create table t2 (a int, b int) engine = innodb;
- 调用存储过程,获取DDL语句。 - 获取按表添加列存索引的DDL语句。 - call dbms_imci.columnar_advise('select count(t1.a) from t1 inner join t2 on t1.a = t2.a group by t1.b');- 执行结果如下: - +-------------------------------------------+ | DDL_STATEMENT | +-------------------------------------------+ | ALTER TABLE test.t1 COMMENT='COLUMNAR=1'; | | ALTER TABLE test.t2 COMMENT='COLUMNAR=1'; | +-------------------------------------------+ 2 rows in set (0.00 sec)- SELECT语句中 - t1和- t2表中的列都没有创建列索引。调用存储过程后,分别获取了按表添加列索引的DDL语句,执行DDL语句后会为- t1和- t2表中所有支持列存索引的列添加列索引。
- 获取按列添加列存索引的DDL语句。 - call dbms_imci.columnar_advise_by_columns('select count(t1.a) from t1 inner join t2 on t1.a = t2.a group by t1.b');- 执行结果如下: - mysql> call dbms_imci.columnar_advise_by_columns('select count(t1.a) from t1 inner join t2 on t1.a = t2.a group by t1.b'); +-------------------------------------------------------------------------------------------------------------------------------------------+ | DDL_STATEMENT | +-------------------------------------------------------------------------------------------------------------------------------------------+ | ALTER TABLE test.t1 MODIFY COLUMN a int(11) DEFAULT NULL COMMENT 'COLUMNAR=1', MODIFY COLUMN b int(11) DEFAULT NULL COMMENT 'COLUMNAR=1'; | | ALTER TABLE test.t2 MODIFY COLUMN a int(11) DEFAULT NULL COMMENT 'COLUMNAR=1'; | +-------------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)- SELECT语句仅查询 - t1.a、- t1.b以及- t2.a三列,调用存储过程- dbms_imci.columnar_advise_by_columns()可以得到按列添加列索引的DDL语句,执行DDL语句后,可以确保SELECT语句中的所有列都被列索引覆盖。