SQL优化——关联视图查询,视图性能慢类优化
视图会在查询、关联中大量引起,需要保证视图的高性能,本例中的视图查询性能慢。
SQL语句

SQL总计耗时28秒
视图vw_ehr_emp语法如下:

vw_ehr_emp中两表关联左侧关联字段加了trim()函数,会导致关联列上智能索引失效,走全表扫描,是造成该视图性能低的原因。建议提前将sz_mh_10ehr_ref表里的orgid、depid列的数据进行清洗,避免数据中包含左右空格。
在另外一套环境的testdb_ndty库中新建视图和视图依赖的表,通过以下方式先清洗关联列左右空格,保证库内的高效运算。
drop table if exists testdb_ndty.eoa_staff;
create table testdb_ndty.eoa_staff distributed by ('depid') as select * from bdmjs10.eoa_staff limit 0;
insert into testdb_ndty.eoa_staff (eoaid,usapid,iden,mob,staffname,orgid,orgname,depid,depnam,c3id,ifarid,bz1,bz2,bz3,bz4,bz5)
select
eoaid,usapid,iden,mob,staffname,orgid,orgname,trim(depid),depnam,c3id,ifarid,bz1,bz2,bz3,bz4,bz5 from bdmjs10.eoa_staff
drop table if exists testdb_ndty.sz_mh_10ehr_ref;
create table testdb_ndty.sz_mh_10ehr_ref replicated select * from bdmjssh.sz_mh_10ehr_ref limit 1;
insert into testdb_ndty.sz_mh_10ehr_ref (org_name,org_id,org_pid ,org_lvl , org_ou)
select org_name,trim(org_id),org_pid ,org_lvl , org_ou from bdmjssh.sz_mh_10ehr_ref;
优化视图后SQL总计耗时21秒。
评论
热门帖子
- 12025-12-01浏览数:182759
- 22023-05-09浏览数:25044
- 42023-09-25浏览数:18519
- 52020-05-11浏览数:17526