GBase 8c常见的SQL调优手段 Plan Hint调优Plan Hint为用户提供了直接影响执行计划生成的手段用户可以通过指定join顺序、join、scan方法、指定结果行数等多个手段来进行执行计划的调优以提升查询的性能。使用方法/* plan hint*/如果同时指定多个hint之间需要使用空格分割如select /* plan_hint1 plan_hint2 */ * from t1Plan Hint支持的范围指定Join顺序的Hint - leading hint指定Join方式的Hint指定结果集行数的Hint指定Scan方式的Hint仅支持常用的tablescanindexscan和indexonlyscan的hint指定子链接块名的HintPlan Hint调优示例stu和stu_info两个表关联查询默认使用Hash Join方式进行表关联。使用Hint强制改变执行计划让两个表通过nestloop进行关联查询使用select * 查询stu_info表指定code和stu_id的查询条件默认使用Seq Scan方式获取数据。使用Hint方式指定查询时使用stu_info表的idx_stu_info_code这个索引改变执行计划算子级优化示例基表扫描时对于点查或者范围扫描等过滤大量数据的查询如果使用SeqScan全表扫描会比较耗时可以在条件列上建立索引选择IndexScan进行索引扫描提升扫描效率。执行耗时248ms对stu表的name表创建索引执行耗时25msSQL改写避免使用SELECT *实际业务可能只需要表的几列数据使用SELECT * 会导致内存CPU网络IO等资源浪费。SELECT * 不会走覆盖索引扫描部分SQL语句效率下降明显。使用exists代替inselect st.id,st.name from stu st where st.age5 and st.id not in ( select stu_id from stu_info where code in (a,b,c,d) );正解select st.id,st.name from stu st where st.age5 and not exists ( select 1 from stu_info si where si.stu_idst.id and si.code in (a,b,c,d) );使用连接查询代替子查询select * from stu where id in (select stu_id from stu_info where codea)正解select st.* from stu st inner join stu_id si on si.user_id st.id and si.codea批量操作当有多条加工数据需要写入表中时采取批量写入可以减少I/O消耗insert into stu values (1,bill,15); insert into stu values (2,frank,16);正解insert into stu values (1,bill,15),(2,frank,16);创建适当索引CREATE INDEX idx_stu_name ON stu(name);需要注意点索引并非越多越好太多索引会引起update和insert效率下降分析查询语句使用联合索引注意索引区分度和索引冗余避免在大量null字段上创建索引以及其他常见优化方法如使用limit限制数据返回行分页优化通过主键或者其他索引列先找出符合条件行主键再通过主键过滤查询避免在where字句中使用函数等表达式对大表数据使用分区表按照分区进行查询操作。