测试环境:本地oracle,客户系统mysql
迁移
1.数据表迁移至oracle
采用两种方式 测试
1 kettle自带批量迁移方式
2 navicat自带数据传输工具
两种都可以实现数据迁移
方案:整体使用工具迁移数据,手工处理有问题的表,涉及部分字段处理,及可能出现的长度问题,以及字段大写可能需要注意
注意事项:
1 抽取后需要检查 表字段类型和精度 减少返工风险
2 数据大小写及符号半角全角问题
3 所有sql及建表语句统一使用大写字母
2.脚本调整
咨询过多个有迁移经验的人员,都没有采用批量调整脚本的方案,大体上简单sql脚本是通用的,但是函数及一些书写方式需要手工调整
函数部分规则表(待补充) :为减少返工风险,初期先进行所有函数使用规定。如果需要调整,整体一起调整
| 函数说明 | mysql函数 | oracle函数 | 
|---|---|---|
| 从左截取 | left(字段,2) | substr(字段,1,2) | 
| 从右截取 | right(字段,4) | substr(字段,-4) | 
| 日期减去1天 | date_add(字段, interval -1 day) | 日期字段-1 | 
| 日期转化年月格式 | date_format(字段,’%Y%m’) | to_char(字段,’YYYYMM’) | 
| 日期转化年月日格式 | date_format(字段,’%Y%m%d’) | to_char(字段,’YYYYMMDD’) | 
| 日期减去日期得月 | timestampdiff(month,日期,日期) | months_between(日期字段,日期字段) | 
| 日期见日期得天 | timestampdiff(day,日期,日期) | to_number(日期-日期) | 
| 字符串转为日期 | 不需要转化 | to_date(‘2004-02-03’,’yyyy-mm-dd’) | 
| null值处理 | ifnull(字段,0) | nvl(字段,0) | 
优化方式
1.对抽出数据库的操作调整为数据库内操作(在mysql系统的测试)
例子:for_dws_shengyu_ziyuan_hetong.ktr
将脚本调整为库内操作后流程运行时间由798  s 降低为 160  s
2.SQL调整(调整为oracle可执行脚本)
例子:for_dws_shengyu_ziyuan_hetong.ktr
根据上面脚本调整后,在oracle数据库执行需要 37.801s
3.脚本优化(根据脚本直接时间决定是否修改,每次需要理清逻辑及测试数据)
例子:for_dws_shengyu_ziyuan_hetong.ktr
1过滤版本,采用分析函数
优化后,在oracle数据库执行需要  32.245s
4.表查询优化(oracle自带的执行计划工具)
explain  plan for  sql语句
select  *  from  table(dbms_xplan.display)
根据sql执行计划,适当添加索引,及调整语句书写方式
5.流程优化
在以上步骤执行后,如果数据执行还是慢,将采用增量抽取方式, 保持当前全量的方式
调整前脚本
selectdistinctc.z_area_e as quyu_bianma,c.z_area_text as quyu_mingcheng,c.z_city_e as chengshi_bianma,c.z_city_text as chengshi_mingcheng,c.z_proje_e as xiangmu_bianma,c.z_xmms_yx as xiangmu_mingcheng_yx,c.liangjia as zhuli_xiangmu,c.anjie_chengshi as dili_chengshi,a.z_sacfy as fangyuan_bianma -- 房源,a.z_sac_text as fangyuan_mingcheng -- 房源名称,a.z_prdtyp6 as chanpin_lx_bianma -- 六级产品类型,a.z_ljms as chanpin_lx_mingcheng -- 六级产品类型描述,aa.sanji as sanji_chanpin -- 三级产品,aa.qiji as qiji_chanpin -- 七级产品,a.z_ldms as loudong_mingcheng -- 楼栋描述,a.z_syzyhtkjts as kucun_ts -- 合同口径库存套数,a.z_syzyhtkjzj as kucun_jine -- 合同口径库存金额,a.z_syzyhtkj_jz as kucun_mianji -- 合同口径库存面积,a.z_htkjrq as kucun_banban,left(a.z_htkjrq,6) as kucun_banban_yue,date_format(a.DB_DATE,'%Y%m%d') as gengxin_riqi,(case when a.yqz_date !='00000000'then a.yqz_dateend ) as yushouzheng_riqi,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 1 then a.z_syzyhtkjzjwhen aa.sanji = '住宅'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 1 ,'06') then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 2 then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 2 ,'06') then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 3 then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 3 ,'06') then a.z_syzyhtkjzjend) as changkucun_jine -- 长库存,(case when aa.sanji != '住宅' then a.z_syzyhtkjzjend) as feizhu_kucun_jine -- 非住库存金额,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then a.z_syzyhtkjzjend) as nianchu_feichangku_quhua_riqi -- 年初非长库金额,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then b.qianyue_yeji_riqiwhen aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then b.qianyue_yeji_riqiwhen aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then b.qianyue_yeji_riqiend) as feichangku_quhua_riqi -- 年初非长库去化日期,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then b.fangyuan_zongjiawhen aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then b.fangyuan_zongjiawhen aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then b.fangyuan_zongjiaend) as feichangku_quhua_jine -- 年初非长库去化金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and timestampdiff(month,a.yqz_date,a.z_htkjrq)<5 then a.z_syzyhtkjzjend) as hetong_kucun_je_0_to_6_yue -- 0-6月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and timestampdiff(month,a.yqz_date,a.z_htkjrq)>=5and timestampdiff(month,a.yqz_date,a.z_htkjrq)<11 then a.z_syzyhtkjzjend) as hetong_kucun_je_6_to_12_yue -- 6-12月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and timestampdiff(month,a.yqz_date,a.z_htkjrq)>=11and timestampdiff(month,a.yqz_date,a.z_htkjrq)<23 then a.z_syzyhtkjzjend) as hetong_kucun_je_12_to_24_yue -- 12-24月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and timestampdiff(month,a.yqz_date,a.z_htkjrq)>23 then a.z_syzyhtkjzjend) as hetong_kucun_je_24_yue -- 24月以上库存金额,(case when a.yqz_date !='00000000'and aa.sanji != '车位'and a.z_syzyhtkjzj >0and a.z_syzyhtkjts >0 then timestampdiff(day,a.yqz_date,a.z_htkjrq)end) as feichewei_kucun_kl -- 非车位库龄,(case when a.yqz_date !='00000000'and aa.sanji != '车位'and a.z_syzyhtkjzj >0and a.z_syzyhtkjts >0 then a.z_syzyhtkjtsend) as feichewei_kucun_ts -- 非车位套数,(case when aa.sanji != '车位' then a.z_syzyhtkjzjend) as feichewei_kucun_je -- 非车位存金额from( -- 每个月有个最新版本selectdate_format(date_add(DB_DATE, interval -1 day),'%Y%m'),max(date_format(DB_DATE,'%Y%m%d')) as yue_zuixin_gengxin_riqifrom sunac_syzytj_htkjgroup bydate_format(date_add(DB_DATE, interval -1 day),'%Y%m')) xinleft join sunac_syzytj_htkj a on date_format(DB_DATE,'%Y%m%d')=xin.yue_zuixin_gengxin_riqileft join for_dws_yingxiao_taizhang b on a.z_sacfy=b.fangyuan_bianmaLEFT JOIN dim_cplx_w aa on a.z_prdtyp6 = aa.Z_PRDNUM6 -- 关联营销业态left join(selectdistinctz_area_e,z_area_text,z_city_e,z_city_text,z_proje_e,z_xmms_yx,liangjia,anjie_chengshifromdim_xiangmu) c on a.z_proje_e = c.z_proje_ewhere c. z_proje_e is not null -- 8530 条数据在dim 没关联上
调整后脚本
with data1 as(select a.*,rank() over(partition by to_char(DB_DATE-1,'YYYYMM') order by to_char(DB_DATE,'YYYYMMDD') desc) num from "sunac_syzytj_htkj" a)selectc.z_area_e as quyu_bianma,c.z_area_text as quyu_mingcheng,c.z_city_e as chengshi_bianma,c.z_city_text as chengshi_mingcheng,c.z_proje_e as xiangmu_bianma,c.z_xmms_yx as xiangmu_mingcheng_yx,c.liangjia as zhuli_xiangmu,c.anjie_chengshi as dili_chengshi,a.z_sacfy as fangyuan_bianma -- 房源,a.z_sac_text as fangyuan_mingcheng -- 房源名称,a.z_prdtyp6 as chanpin_lx_bianma -- 六级产品类型,a.z_ljms as chanpin_lx_mingcheng -- 六级产品类型描述,aa.sanji as sanji_chanpin -- 三级产品,aa.qiji as qiji_chanpin -- 七级产品,a.z_ldms as loudong_mingcheng -- 楼栋描述,a.z_syzyhtkjts as kucun_ts -- 合同口径库存套数,a.z_syzyhtkjzj as kucun_jine -- 合同口径库存金额,a.z_syzyhtkj_jz as kucun_mianji -- 合同口径库存面积,a.z_htkjrq as kucun_banban,substr(a.z_htkjrq,6) as kucun_banban_yue,to_char(a.DB_DATE,'YYYYMMDD') as gengxin_riqi,(case when a.yqz_date !='00000000'then a.yqz_dateend ) as yushouzheng_riqi,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 1 then a.z_syzyhtkjzjwhen aa.sanji = '住宅'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 1 ,'06') then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 2 then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 2 ,'06') then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 3 then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 3 ,'06') then a.z_syzyhtkjzjend) as changkucun_jine -- 长库存,(case when aa.sanji != '住宅' then a.z_syzyhtkjzjend) as feizhu_kucun_jine -- 非住库存金额,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then a.z_syzyhtkjzjwhen aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then a.z_syzyhtkjzjwhen aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then a.z_syzyhtkjzjend) as nianchu_feichangku_quhua_riqi -- 年初非长库金额,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then b."qianyue_yeji_riqi"when aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then b."qianyue_yeji_riqi"when aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then b."qianyue_yeji_riqi"end) as feichangku_quhua_riqi -- 年初非长库去化日期,(case when aa.sanji = '住宅'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then b."fangyuan_zongjia"when aa.sanji = '商办'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then b."fangyuan_zongjia"when aa.sanji = '车位'and a.yqz_date !='00000000'and a.z_syzyhtkjts >0and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then b."fangyuan_zongjia"end) as feichangku_quhua_jine -- 年初非长库去化金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<5 then a.z_syzyhtkjzjend) as hetong_kucun_je_0_to_6_yue -- 0-6月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>=5and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<11 then a.z_syzyhtkjzjend) as hetong_kucun_je_6_to_12_yue -- 6-12月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>=11and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<23 then a.z_syzyhtkjzjend) as hetong_kucun_je_12_to_24_yue -- 12-24月库存金额,(case when a.yqz_date !='00000000'and a.z_syzyhtkjts >0and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>23 then a.z_syzyhtkjzjend) as hetong_kucun_je_24_yue -- 24月以上库存金额,(case when a.yqz_date !='00000000'and aa.sanji != '车位'and a.z_syzyhtkjzj >0and a.z_syzyhtkjts >0 then to_date(a.yqz_date,'yyyy-mm-dd')-to_date(a.z_htkjrq,'yyyy-mm-dd')end) as feichewei_kucun_kl -- 非车位库龄,(case when a.yqz_date !='00000000'and aa.sanji != '车位'and a.z_syzyhtkjzj >0and a.z_syzyhtkjts >0 then a.z_syzyhtkjtsend) as feichewei_kucun_ts -- 非车位套数,(case when aa.sanji != '车位' then a.z_syzyhtkjzjend) as feichewei_kucun_je -- 非车位存金额fromdata1 aleft join "for_dws_yingxiao_taizhang" b on a.z_sacfy=b."fangyuan_bianma"left JOIN "dim_cplx_w" aa on a.z_prdtyp6 = aa.Z_PRDNUM6 -- 关联营销业态left join(selectdistinctz_area_e,z_area_text,z_city_e,z_city_text,z_proje_e,z_xmms_yx,liangjia,anjie_chengshifrom"dim_xiangmu") c on a.z_proje_e = c.z_proje_ewherea.num=1 andc. z_proje_e is not null -- 8530 条数据在dim 没关联上
注意事项
1:
