GaussDB AI新特性:gs_index_advise推荐索引
GaussDB的AI新特性,可以把SQL文本嵌套在函数中,数据库会返回一个创建合适索引的列
gs_index_advise(text)
描述:针对单条查询语句推荐索引。
参数: SQL语句字符串
返回值类型: record
一、通常的SQL优化会通过参数调优的方式进行调整,例如如下参数
set enable_fast_query_shipping = off; set enable_stream_operator = on;
二、创建数据库数据
--建表 create table tb_user(stu_no int,stu_name varchar(32),age int,hobby_type int) distribute hash(age); --插入数据 insert into tb_user select id,'xiaoming'||(random()*60+10)::int, (random()*60+10)::int, (random()*5+1)::int from (select generate_series(1,100000)id)tb_user;
三、收集tb_user的统计信息
analyze tb_user;
四、为下面两个查询语句创建索引,让执行计划和索引最合理
SQL1:explain analyze select * from tb_user where age=29 and stu_name='xiaoming'; SQL2:explain analyze select * from tb_user where stu_no=100 and age=29;
--SQL1
select gs_index_advise('select * from tb_user where age=29 and stu_name=’'xiaoming'’');
create index age_name on tb_user(stu_name,age);
--SQL2
select gs_index_advise('select * from tb_user where stu_no=100 and age=29');
create index age_no on tb_user(stu_no,age);