测试环境:本地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.流程优化

在以上步骤执行后,如果数据执行还是慢,将采用增量抽取方式, 保持当前全量的方式

调整前脚本

  1. select
  2. distinct
  3. c.z_area_e as quyu_bianma
  4. ,c.z_area_text as quyu_mingcheng
  5. ,c.z_city_e as chengshi_bianma
  6. ,c.z_city_text as chengshi_mingcheng
  7. ,c.z_proje_e as xiangmu_bianma
  8. ,c.z_xmms_yx as xiangmu_mingcheng_yx
  9. ,c.liangjia as zhuli_xiangmu
  10. ,c.anjie_chengshi as dili_chengshi
  11. ,a.z_sacfy as fangyuan_bianma -- 房源
  12. ,a.z_sac_text as fangyuan_mingcheng -- 房源名称
  13. ,a.z_prdtyp6 as chanpin_lx_bianma -- 六级产品类型
  14. ,a.z_ljms as chanpin_lx_mingcheng -- 六级产品类型描述
  15. ,aa.sanji as sanji_chanpin -- 三级产品
  16. ,aa.qiji as qiji_chanpin -- 七级产品
  17. ,a.z_ldms as loudong_mingcheng -- 楼栋描述
  18. ,a.z_syzyhtkjts as kucun_ts -- 合同口径库存套数
  19. ,a.z_syzyhtkjzj as kucun_jine -- 合同口径库存金额
  20. ,a.z_syzyhtkj_jz as kucun_mianji -- 合同口径库存面积
  21. ,a.z_htkjrq as kucun_banban
  22. ,left(a.z_htkjrq,6) as kucun_banban_yue
  23. ,date_format(a.DB_DATE,'%Y%m%d') as gengxin_riqi
  24. ,(case when a.yqz_date !='00000000'
  25. then a.yqz_date
  26. end ) as yushouzheng_riqi
  27. ,(case when aa.sanji = '住宅'
  28. and a.yqz_date !='00000000'
  29. and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')
  30. and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 1 then a.z_syzyhtkjzj
  31. when aa.sanji = '住宅'
  32. and a.yqz_date !='00000000'
  33. and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')
  34. and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 1 ,'06') then a.z_syzyhtkjzj
  35. when aa.sanji = '商办'
  36. and a.yqz_date !='00000000'
  37. and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')
  38. and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 2 then a.z_syzyhtkjzj
  39. when aa.sanji = '商办'
  40. and a.yqz_date !='00000000'
  41. and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')
  42. and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 2 ,'06') then a.z_syzyhtkjzj
  43. when aa.sanji = '车位'
  44. and a.yqz_date !='00000000'
  45. and right(left(a.z_htkjrq,6),2) in ('01','02','03','04','05','06')
  46. and left(a.yqz_date,4) < left(a.z_htkjrq,4) - 3 then a.z_syzyhtkjzj
  47. when aa.sanji = '车位'
  48. and a.yqz_date !='00000000'
  49. and right(left(a.z_htkjrq,6),2) in ('07','08','09','10','11','12')
  50. and left(a.yqz_date,6) <= concat(left(a.z_htkjrq,4) - 3 ,'06') then a.z_syzyhtkjzj
  51. end) as changkucun_jine -- 长库存
  52. ,(case when aa.sanji != '住宅' then a.z_syzyhtkjzj
  53. end) as feizhu_kucun_jine -- 非住库存金额
  54. ,(case when aa.sanji = '住宅'
  55. and a.yqz_date !='00000000'
  56. and a.z_syzyhtkjts >0
  57. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then a.z_syzyhtkjzj
  58. when aa.sanji = '商办'
  59. and a.yqz_date !='00000000'
  60. and a.z_syzyhtkjts >0
  61. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then a.z_syzyhtkjzj
  62. when aa.sanji = '车位'
  63. and a.yqz_date !='00000000'
  64. and a.z_syzyhtkjts >0
  65. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then a.z_syzyhtkjzj
  66. end) as nianchu_feichangku_quhua_riqi -- 年初非长库金额
  67. ,(case when aa.sanji = '住宅'
  68. and a.yqz_date !='00000000'
  69. and a.z_syzyhtkjts >0
  70. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then b.qianyue_yeji_riqi
  71. when aa.sanji = '商办'
  72. and a.yqz_date !='00000000'
  73. and a.z_syzyhtkjts >0
  74. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then b.qianyue_yeji_riqi
  75. when aa.sanji = '车位'
  76. and a.yqz_date !='00000000'
  77. and a.z_syzyhtkjts >0
  78. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then b.qianyue_yeji_riqi
  79. end) as feichangku_quhua_riqi -- 年初非长库去化日期
  80. ,(case when aa.sanji = '住宅'
  81. and a.yqz_date !='00000000'
  82. and a.z_syzyhtkjts >0
  83. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 0 then b.fangyuan_zongjia
  84. when aa.sanji = '商办'
  85. and a.yqz_date !='00000000'
  86. and a.z_syzyhtkjts >0
  87. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 1 then b.fangyuan_zongjia
  88. when aa.sanji = '车位'
  89. and a.yqz_date !='00000000'
  90. and a.z_syzyhtkjts >0
  91. and left(a.yqz_date,4) >= left(a.z_htkjrq,4) - 2 then b.fangyuan_zongjia
  92. end) as feichangku_quhua_jine -- 年初非长库去化金额
  93. ,(case when a.yqz_date !='00000000'
  94. and a.z_syzyhtkjts >0
  95. and timestampdiff(month,a.yqz_date,a.z_htkjrq)<5 then a.z_syzyhtkjzj
  96. end) as hetong_kucun_je_0_to_6_yue -- 0-6月库存金额
  97. ,(case when a.yqz_date !='00000000'
  98. and a.z_syzyhtkjts >0
  99. and timestampdiff(month,a.yqz_date,a.z_htkjrq)>=5
  100. and timestampdiff(month,a.yqz_date,a.z_htkjrq)<11 then a.z_syzyhtkjzj
  101. end) as hetong_kucun_je_6_to_12_yue -- 6-12月库存金额
  102. ,(case when a.yqz_date !='00000000'
  103. and a.z_syzyhtkjts >0
  104. and timestampdiff(month,a.yqz_date,a.z_htkjrq)>=11
  105. and timestampdiff(month,a.yqz_date,a.z_htkjrq)<23 then a.z_syzyhtkjzj
  106. end) as hetong_kucun_je_12_to_24_yue -- 12-24月库存金额
  107. ,(case when a.yqz_date !='00000000'
  108. and a.z_syzyhtkjts >0
  109. and timestampdiff(month,a.yqz_date,a.z_htkjrq)>23 then a.z_syzyhtkjzj
  110. end) as hetong_kucun_je_24_yue -- 24月以上库存金额
  111. ,(case when a.yqz_date !='00000000'
  112. and aa.sanji != '车位'
  113. and a.z_syzyhtkjzj >0
  114. and a.z_syzyhtkjts >0 then timestampdiff(day,a.yqz_date,a.z_htkjrq)
  115. end) as feichewei_kucun_kl -- 非车位库龄
  116. ,(case when a.yqz_date !='00000000'
  117. and aa.sanji != '车位'
  118. and a.z_syzyhtkjzj >0
  119. and a.z_syzyhtkjts >0 then a.z_syzyhtkjts
  120. end) as feichewei_kucun_ts -- 非车位套数
  121. ,(case when aa.sanji != '车位' then a.z_syzyhtkjzj
  122. end) as feichewei_kucun_je -- 非车位存金额
  123. from
  124. ( -- 每个月有个最新版本
  125. select
  126. date_format(date_add(DB_DATE, interval -1 day),'%Y%m')
  127. ,max(date_format(DB_DATE,'%Y%m%d')) as yue_zuixin_gengxin_riqi
  128. from sunac_syzytj_htkj
  129. group by
  130. date_format(date_add(DB_DATE, interval -1 day),'%Y%m')
  131. ) xin
  132. left join sunac_syzytj_htkj a on date_format(DB_DATE,'%Y%m%d')=xin.yue_zuixin_gengxin_riqi
  133. left join for_dws_yingxiao_taizhang b on a.z_sacfy=b.fangyuan_bianma
  134. LEFT JOIN dim_cplx_w aa on a.z_prdtyp6 = aa.Z_PRDNUM6 -- 关联营销业态
  135. left join
  136. (
  137. select
  138. distinct
  139. z_area_e
  140. ,z_area_text
  141. ,z_city_e
  142. ,z_city_text
  143. ,z_proje_e
  144. ,z_xmms_yx
  145. ,liangjia
  146. ,anjie_chengshi
  147. from
  148. dim_xiangmu
  149. ) c on a.z_proje_e = c.z_proje_e
  150. where c. z_proje_e is not null -- 8530 条数据在dim 没关联上

调整后脚本

  1. with data1 as(
  2. 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
  3. )
  4. select
  5. c.z_area_e as quyu_bianma
  6. ,c.z_area_text as quyu_mingcheng
  7. ,c.z_city_e as chengshi_bianma
  8. ,c.z_city_text as chengshi_mingcheng
  9. ,c.z_proje_e as xiangmu_bianma
  10. ,c.z_xmms_yx as xiangmu_mingcheng_yx
  11. ,c.liangjia as zhuli_xiangmu
  12. ,c.anjie_chengshi as dili_chengshi
  13. ,a.z_sacfy as fangyuan_bianma -- 房源
  14. ,a.z_sac_text as fangyuan_mingcheng -- 房源名称
  15. ,a.z_prdtyp6 as chanpin_lx_bianma -- 六级产品类型
  16. ,a.z_ljms as chanpin_lx_mingcheng -- 六级产品类型描述
  17. ,aa.sanji as sanji_chanpin -- 三级产品
  18. ,aa.qiji as qiji_chanpin -- 七级产品
  19. ,a.z_ldms as loudong_mingcheng -- 楼栋描述
  20. ,a.z_syzyhtkjts as kucun_ts -- 合同口径库存套数
  21. ,a.z_syzyhtkjzj as kucun_jine -- 合同口径库存金额
  22. ,a.z_syzyhtkj_jz as kucun_mianji -- 合同口径库存面积
  23. ,a.z_htkjrq as kucun_banban
  24. ,substr(a.z_htkjrq,6) as kucun_banban_yue
  25. ,to_char(a.DB_DATE,'YYYYMMDD') as gengxin_riqi
  26. ,(case when a.yqz_date !='00000000'
  27. then a.yqz_date
  28. end ) as yushouzheng_riqi
  29. ,(case when aa.sanji = '住宅'
  30. and a.yqz_date !='00000000'
  31. and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')
  32. and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 1 then a.z_syzyhtkjzj
  33. when aa.sanji = '住宅'
  34. and a.yqz_date !='00000000'
  35. and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')
  36. and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 1 ,'06') then a.z_syzyhtkjzj
  37. when aa.sanji = '商办'
  38. and a.yqz_date !='00000000'
  39. and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')
  40. and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 2 then a.z_syzyhtkjzj
  41. when aa.sanji = '商办'
  42. and a.yqz_date !='00000000'
  43. and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')
  44. and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 2 ,'06') then a.z_syzyhtkjzj
  45. when aa.sanji = '车位'
  46. and a.yqz_date !='00000000'
  47. and substr(substr(a.z_htkjrq,1,6),-2) in ('01','02','03','04','05','06')
  48. and substr(a.yqz_date,1,4) < substr(a.z_htkjrq,1,4) - 3 then a.z_syzyhtkjzj
  49. when aa.sanji = '车位'
  50. and a.yqz_date !='00000000'
  51. and substr(substr(a.z_htkjrq,1,6),-2) in ('07','08','09','10','11','12')
  52. and substr(a.yqz_date,1,6) <= concat(substr(a.z_htkjrq,1,4) - 3 ,'06') then a.z_syzyhtkjzj
  53. end) as changkucun_jine -- 长库存
  54. ,(case when aa.sanji != '住宅' then a.z_syzyhtkjzj
  55. end) as feizhu_kucun_jine -- 非住库存金额
  56. ,(case when aa.sanji = '住宅'
  57. and a.yqz_date !='00000000'
  58. and a.z_syzyhtkjts >0
  59. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then a.z_syzyhtkjzj
  60. when aa.sanji = '商办'
  61. and a.yqz_date !='00000000'
  62. and a.z_syzyhtkjts >0
  63. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then a.z_syzyhtkjzj
  64. when aa.sanji = '车位'
  65. and a.yqz_date !='00000000'
  66. and a.z_syzyhtkjts >0
  67. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then a.z_syzyhtkjzj
  68. end) as nianchu_feichangku_quhua_riqi -- 年初非长库金额
  69. ,(case when aa.sanji = '住宅'
  70. and a.yqz_date !='00000000'
  71. and a.z_syzyhtkjts >0
  72. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then b."qianyue_yeji_riqi"
  73. when aa.sanji = '商办'
  74. and a.yqz_date !='00000000'
  75. and a.z_syzyhtkjts >0
  76. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then b."qianyue_yeji_riqi"
  77. when aa.sanji = '车位'
  78. and a.yqz_date !='00000000'
  79. and a.z_syzyhtkjts >0
  80. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then b."qianyue_yeji_riqi"
  81. end) as feichangku_quhua_riqi -- 年初非长库去化日期
  82. ,(case when aa.sanji = '住宅'
  83. and a.yqz_date !='00000000'
  84. and a.z_syzyhtkjts >0
  85. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 0 then b."fangyuan_zongjia"
  86. when aa.sanji = '商办'
  87. and a.yqz_date !='00000000'
  88. and a.z_syzyhtkjts >0
  89. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 1 then b."fangyuan_zongjia"
  90. when aa.sanji = '车位'
  91. and a.yqz_date !='00000000'
  92. and a.z_syzyhtkjts >0
  93. and substr(a.yqz_date,1,4) >= substr(a.z_htkjrq,1,4) - 2 then b."fangyuan_zongjia"
  94. end) as feichangku_quhua_jine -- 年初非长库去化金额
  95. ,(case when a.yqz_date !='00000000'
  96. and a.z_syzyhtkjts >0
  97. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<5 then a.z_syzyhtkjzj
  98. end) as hetong_kucun_je_0_to_6_yue -- 0-6月库存金额
  99. ,(case when a.yqz_date !='00000000'
  100. and a.z_syzyhtkjts >0
  101. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>=5
  102. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<11 then a.z_syzyhtkjzj
  103. end) as hetong_kucun_je_6_to_12_yue -- 6-12月库存金额
  104. ,(case when a.yqz_date !='00000000'
  105. and a.z_syzyhtkjts >0
  106. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>=11
  107. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))<23 then a.z_syzyhtkjzj
  108. end) as hetong_kucun_je_12_to_24_yue -- 12-24月库存金额
  109. ,(case when a.yqz_date !='00000000'
  110. and a.z_syzyhtkjts >0
  111. and months_between(to_date(a.yqz_date,'yyyy-mm-dd'),to_date(a.z_htkjrq,'yyyy-mm-dd'))>23 then a.z_syzyhtkjzj
  112. end) as hetong_kucun_je_24_yue -- 24月以上库存金额
  113. ,(case when a.yqz_date !='00000000'
  114. and aa.sanji != '车位'
  115. and a.z_syzyhtkjzj >0
  116. and a.z_syzyhtkjts >0 then to_date(a.yqz_date,'yyyy-mm-dd')-to_date(a.z_htkjrq,'yyyy-mm-dd')
  117. end) as feichewei_kucun_kl -- 非车位库龄
  118. ,(case when a.yqz_date !='00000000'
  119. and aa.sanji != '车位'
  120. and a.z_syzyhtkjzj >0
  121. and a.z_syzyhtkjts >0 then a.z_syzyhtkjts
  122. end) as feichewei_kucun_ts -- 非车位套数
  123. ,(case when aa.sanji != '车位' then a.z_syzyhtkjzj
  124. end) as feichewei_kucun_je -- 非车位存金额
  125. from
  126. data1 a
  127. left join "for_dws_yingxiao_taizhang" b on a.z_sacfy=b."fangyuan_bianma"
  128. left JOIN "dim_cplx_w" aa on a.z_prdtyp6 = aa.Z_PRDNUM6 -- 关联营销业态
  129. left join
  130. (
  131. select
  132. distinct
  133. z_area_e
  134. ,z_area_text
  135. ,z_city_e
  136. ,z_city_text
  137. ,z_proje_e
  138. ,z_xmms_yx
  139. ,liangjia
  140. ,anjie_chengshi
  141. from
  142. "dim_xiangmu"
  143. ) c on a.z_proje_e = c.z_proje_e
  144. where
  145. a.num=1 and
  146. c. z_proje_e is not null -- 8530 条数据在dim 没关联上

注意事项

1: