Oracle优化:千万级大表逻辑判断的累赘

73次阅读
没有评论

共计 2508 个字符,预计需要花费 7 分钟才能阅读完成。

自动写代码机器人,免费开通

insert into pntmall_point_detail(PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,HPT_REDEMPT_POINT,

 HPT_LEFT_POINT,HPT_FULLREDEMPT_STATUS)

  SELECT PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,0 as HPT_REDEMPT_POINT,PNTMALL_NUM,0

  FROM pntmall_point_detail_tmp a

  WHERE NOT EXISTS (SELECT b.PNTMALL_PNT_ID
  FROM pntmall_point_detail b
  WHERE a.PNTMALL_PNT_ID = b.PNTMALL_PNT_ID);

PNTMALL_POINT_DETAIL 包含 3800 万条数据,cost 6 hours。

优化后

 

delete from pntmall_point_detail_tmp a where exists (select 1 from pntmall_point_detail b where a.PNTMALL_PNT_ID = b.PNTMALL_PNT_ID);

insert into pntmall_point_detail(PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,HPT_REDEMPT_POINT,

 HPT_LEFT_POINT,HPT_FULLREDEMPT_STATUS)

  SELECT PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,0 as HPT_REDEMPT_POINT,PNTMALL_NUM,0

  FROM pntmall_point_detail_tmp a;

cost 5 minutes。

其实还可以进一步优化

drop indexBER.INDEX_POD_PNTMALL_HAIERUID;

drop indexBER.PNTMALL_POINT_ID_HAIERUID;

delete from pntmall_point_detail_tmp a where exists (select 1from pntmall_point_detailb where a.PNTMALL_PNT_ID =b.PNTMALL_PNT_ID);

insert/*+append*/into pntmall_point_detail(PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,HPT_REDEMPT_POINT,

 HPT_LEFT_POINT,HPT_FULLREDEMPT_STATUS)

  SELECT PNTMALL_PNT_ID,PNTMALL_PNT_DT,PNTMALL_VALIDITY,PNTMALL_LASTUPDATEDT,

 PNTMALL_RPTPNT_ID,PNTMALL_HAIERUID,

 PNTMALL_OPTYPE_ID,PNTMALL_OPTYPE_DESC,

 PNTMALL_NUM,PNTMALL_EXPDT,0 as HPT_REDEMPT_POINT,PNTMALL_NUM,0

  FROM pntmall_point_detail_tmp a; 

commit;

WHERE NOT EXISTS (SELECT b.PNTMALL_PNT_ID

  FROMpntmall_point_detail b

  WHEREa.PNTMALL_PNT_ID = b.PNTMALL_PNT_ID);

create index BER.INDEX_POD_PNTMALL_HAIERUIDon BER.PNTMALL_POINT_DETAIL (PNTMALL_HAIERUID) online nologing;

create unique indexBER.PNTMALL_POINT_ID_HAIERUID on BER.PNTMALL_POINT_DETAIL (PNTMALL_PNT_ID) online nologing;

总体优化思路,不要在 insert 中加入过多的判断语句,删索引,append,重建索引,如果是归档模式,alter table nologing;append 只适用于 insert select 这种方式,而且 insert 后要加 commit, 否则无法进行其他 DML 操作。

实测 append 1600 万条数据,cost 8s

向 AI 问一下细节

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-18发表,共计2508字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)