翼度科技»论坛 编程开发 mysql 查看内容

GaussDB(DWS)性能调优:indexscan导致的性能问题识别与优化

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
摘要:通常跑批加工场景下,都是大数量做关联操作,通常不建议使用索引。有些时候因为计划误判导致使用索引的可能会导致严重的性能问题。本文从一个典型的索引导致性能的场景重发,剖析此类问题的特征,定位方法和解决方法
本文分享自华为云社区《GaussDB(DWS)性能调优:indexscan导致的性能问题识别与优化 #【玩转PB级数仓GaussDB(DWS)】》,作者: 譡里个檔 。
通常跑批加工场景下,都是大数量做关联操作,通常不建议使用索引。有些时候因为计划误判导致使用索引的可能会导致严重的性能问题。本文从一个典型的索引导致性能的场景重发,剖析此类问题的特征,定位方法和解决方法。
1)在某局点POC测试时发现某SQL语句比较慢,原始SQL如下
  1. WITH
  2. /**
  3. etl_116583_7960703_994644
  4. **/
  5. LOADABLE as (select "boq_rel_type_id","to_pu_id","to_version","cycle_id",
  6. "part_offset_flag","to_boq_id","descr","from_contract_id",
  7. "from_version","from_pu_id","ss_id","to_contract_id",
  8. "from_boq_id","enable_flag","last_update_date"
  9. from (SELECT /*+ PARALLEL(4)*/
  10.     BOQ_REL.FROM_BOQ_ID,
  11.     BOQ_REL.TO_BOQ_ID,
  12.     BOQ_REL.FROM_PU_ID,
  13.     BOQ_REL.TO_PU_ID,
  14.     BOQ_REL.PART_OFFSET_FLAG,
  15.     BOQ_REL.DESCR,
  16.     BOQ_REL.SS_ID,
  17.     BOQ_REL.CYCLE_ID,
  18. NVL(BOQ_REL.FROM_VERSION, 'SNULL') FROM_VERSION,
  19. NVL(BOQ_REL.TO_VERSION, 'SNULL') TO_VERSION,
  20.     BOQ_REL.LAST_UPDATE_DATE,
  21.     FROM_CON.CONTRACT_ID AS FROM_CONTRACT_ID,
  22.     TO_CON.CONTRACT_ID AS TO_CONTRACT_ID,
  23.     CLA.CLASS_ID AS BOQ_REL_TYPE_ID,
  24.     BOQ_REL.ENABLE_FLAG
  25. FROM (SELECT A.FROM_BOQ_ID,
  26.                    A.TO_BOQ_ID,
  27. A.FROM_PU_ID,
  28.                    A.TO_PU_ID,
  29. A.FROM_CONTRACT_NUMBER,
  30.                    A.TO_CONTRACT_NUMBER,
  31.                    A.BOQ_REL_TYPE_CODE,
  32.                    A.PART_OFFSET_FLAG,
  33. A.DESCR,
  34. A.SS_ID,
  35. A.FROM_VERSION,
  36.                    A.TO_VERSION,
  37. A.LAST_UPDATE_DATE,
  38. A.CYCLE_ID,
  39. A.ENABLE_FLAG,
  40. DECODE(A.SS_ID, 2820, 2600, A.SS_ID) SS_ID_TMP,
  41.                    ROW_NUMBER() OVER(PARTITION BY FROM_BOQ_ID, TO_BOQ_ID, FROM_PU_ID, TO_PU_ID, FROM_CONTRACT_NUMBER, TO_CONTRACT_NUMBER, BOQ_REL_TYPE_CODE, FROM_VERSION, TO_VERSION
  42. ORDER BY DECODE(A.SS_ID, 2820, 1, 2600, 2, 3)) RN
  43. FROM LDB_MD_BOQ_REL A) BOQ_REL,
  44.            (SELECT CONTRACT_ID,
  45.                    HW_CONTRACT_NUM,
  46.                    SS_ID
  47. FROM DWI_MD_CONTRACT
  48. WHERE CONTRACT_ID IS NOT NULL
  49. AND END_TIME = TO_DATE('4712-12-31', 'YYYY-MM-DD')) FROM_CON,
  50.            (SELECT CONTRACT_ID,
  51.                    HW_CONTRACT_NUM,
  52.                    SS_ID
  53. FROM DWI_MD_CONTRACT
  54. WHERE CONTRACT_ID IS NOT NULL
  55. AND END_TIME = TO_DATE('4712-12-31', 'YYYY-MM-DD')) TO_CON,
  56.            (SELECT CLASS_ID,
  57.                    CODE,
  58.                    CLASS_TYPE_ID,
  59.                    SS_ID
  60. FROM DWI_MD_CLASS
  61. WHERE CLASS_TYPE_ID = 193) CLA
  62. WHERE BOQ_REL.RN = 1
  63. AND BOQ_REL.FROM_CONTRACT_NUMBER = FROM_CON.HW_CONTRACT_NUM
  64. AND BOQ_REL.SS_ID = FROM_CON.SS_ID
  65. AND BOQ_REL.TO_CONTRACT_NUMBER = TO_CON.HW_CONTRACT_NUM
  66. AND BOQ_REL.SS_ID = TO_CON.SS_ID
  67. AND BOQ_REL.BOQ_REL_TYPE_CODE = CLA.CODE
  68. AND BOQ_REL.SS_ID_TMP = CLA.SS_ID
  69.     ) t
  70. ),
  71. BEFORE_TARGET as (select "from_contract_id","from_pu_id","ss_id","from_boq_id","from_version","to_version",
  72. "crt_cycle_id","to_pu_id","to_boq_id","del_flag","last_upd_cycle_id","last_update_date",
  73. "descr","enable_flag","crt_job_instance_id","dq_improve_flag","upd_job_instance_id",
  74. "to_contract_id","part_offset_flag","boq_rel_type_id"
  75. from (SELECT /*+PARALLEL(4)*/
  76.      FROM_BOQ_ID,
  77.      TO_BOQ_ID,
  78.      FROM_PU_ID,
  79.      TO_PU_ID,
  80.      FROM_CONTRACT_ID,
  81.      TO_CONTRACT_ID,
  82.      BOQ_REL_TYPE_ID,
  83.      PART_OFFSET_FLAG,
  84.      DESCR,
  85.      SS_ID,
  86.      CRT_CYCLE_ID,
  87.      LAST_UPD_CYCLE_ID,
  88.      DEL_FLAG,
  89.      DQ_IMPROVE_FLAG,
  90.      CRT_JOB_INSTANCE_ID,
  91.      UPD_JOB_INSTANCE_ID,
  92. NVL(FROM_VERSION, 'SNULL') FROM_VERSION,
  93. NVL(TO_VERSION, 'SNULL') TO_VERSION,
  94.      LAST_UPDATE_DATE,
  95.      ENABLE_FLAG
  96. FROM DWI_MD_BOQ_REL
  97.     ) t
  98. ),
  99. CDC as (select LOADABLE."ss_id",LOADABLE."from_version",LOADABLE."from_boq_id",
  100.     LOADABLE."part_offset_flag",LOADABLE."from_pu_id",
  101. case when BEFORE_TARGET.BOQ_REL_TYPE_ID is null and BEFORE_TARGET.FROM_BOQ_ID is null
  102. and BEFORE_TARGET.FROM_CONTRACT_ID is null and BEFORE_TARGET.FROM_PU_ID is null
  103. and BEFORE_TARGET.FROM_VERSION is null
  104. and BEFORE_TARGET.TO_BOQ_ID is null and BEFORE_TARGET.TO_CONTRACT_ID is null
  105. and BEFORE_TARGET.TO_PU_ID is null and BEFORE_TARGET.TO_VERSION is null
  106. then 1
  107. else 3
  108. end as "change_code",
  109.     LOADABLE."to_version",LOADABLE."boq_rel_type_id",
  110.     LOADABLE."from_contract_id",LOADABLE."to_contract_id",
  111.     LOADABLE."descr",LOADABLE."last_update_date",
  112.     LOADABLE."to_pu_id",LOADABLE."enable_flag",LOADABLE."cycle_id",
  113.     LOADABLE."to_boq_id"
  114. from LOADABLE  
  115. left join BEFORE_TARGET on LOADABLE.BOQ_REL_TYPE_ID = BEFORE_TARGET.BOQ_REL_TYPE_ID
  116. and LOADABLE.FROM_BOQ_ID = BEFORE_TARGET.FROM_BOQ_ID and LOADABLE.FROM_CONTRACT_ID = BEFORE_TARGET.FROM_CONTRACT_ID
  117. and LOADABLE.FROM_PU_ID = BEFORE_TARGET.FROM_PU_ID and LOADABLE.FROM_VERSION = BEFORE_TARGET.FROM_VERSION
  118. and LOADABLE.TO_BOQ_ID = BEFORE_TARGET.TO_BOQ_ID and LOADABLE.TO_CONTRACT_ID = BEFORE_TARGET.TO_CONTRACT_ID
  119. and LOADABLE.TO_PU_ID = BEFORE_TARGET.TO_PU_ID and LOADABLE.TO_VERSION = BEFORE_TARGET.TO_VERSION
  120. ),
  121. TFM_FILTER_DATA_TARGET_OUTPUT_U as (select CDC."to_pu_id",CDC."boq_rel_type_id",CDC."ss_id",
  122. Current_Timestamp() as "dw_last_update_date",CDC."to_version",
  123.     CDC."from_version",20230104000000 as "last_upd_cycle_id",
  124.     CDC."from_contract_id",CDC."last_update_date",CDC."descr",
  125. 'N' as "del_flag",CDC."from_boq_id",CDC."to_boq_id",
  126.     CDC."enable_flag",CDC."from_pu_id",-1 as "upd_job_instance_id",
  127. 'N' as "dq_improve_flag",CDC."to_contract_id",
  128.     CDC."part_offset_flag"
  129. from CDC where CDC.change_code=3
  130. )
  131. update DWI_MD_BOQ_REL TARGET_U
  132. set "dq_improve_flag" = TFM_FILTER_DATA_TARGET_OUTPUT_U."dq_improve_flag",
  133. "dw_last_update_date" = TFM_FILTER_DATA_TARGET_OUTPUT_U."dw_last_update_date",
  134. "upd_job_instance_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."upd_job_instance_id",
  135. "descr" = TFM_FILTER_DATA_TARGET_OUTPUT_U."descr",
  136. "part_offset_flag" = TFM_FILTER_DATA_TARGET_OUTPUT_U."part_offset_flag",
  137. "last_update_date" = TFM_FILTER_DATA_TARGET_OUTPUT_U."last_update_date",
  138. "del_flag" = TFM_FILTER_DATA_TARGET_OUTPUT_U."del_flag",
  139. "last_upd_cycle_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."last_upd_cycle_id",
  140. "enable_flag" = TFM_FILTER_DATA_TARGET_OUTPUT_U."enable_flag",
  141. "ss_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."ss_id"
  142. from TFM_FILTER_DATA_TARGET_OUTPUT_U
  143. where TARGET_U."boq_rel_type_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."boq_rel_type_id"
  144. and TARGET_U."to_version" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_version"
  145. and TARGET_U."to_version" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_version"
  146. and TARGET_U."to_pu_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_pu_id"
  147. and TARGET_U."to_pu_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_pu_id"
  148. and TARGET_U."to_contract_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_contract_id"
  149. and TARGET_U."to_contract_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_contract_id"
  150. and TARGET_U."to_boq_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_boq_id"
  151. and TARGET_U."to_boq_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."to_boq_id"
  152. and TARGET_U."from_version" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_version"
  153. and TARGET_U."from_version" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_version"
  154. and TARGET_U."from_pu_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_pu_id"
  155. and TARGET_U."from_pu_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_pu_id"
  156. and TARGET_U."from_contract_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_contract_id"
  157. and TARGET_U."from_contract_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_contract_id"
  158. and TARGET_U."from_boq_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_boq_id"
  159. and TARGET_U."from_boq_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."from_boq_id"
  160. and TARGET_U."boq_rel_type_id" = TFM_FILTER_DATA_TARGET_OUTPUT_U."boq_rel_type_id"
  161. ;
复制代码
2)查询此query的topSQL信息的warning字段,发现SQL自诊断信息中有索引相关告警信息。
3)查询此query的topSQL信息(如下图),分析历史执行信息,发现id=20的CStore Index Scan算子的耗时为90796.980ms,SQL执行总时长137135.658ms。CStore Index Scan算子的耗时占比为 66%
4)找到原始SQL语句,对查询语句中出现的表dwimd.dwi_md_contract 进行hint,强制其走顺序扫描,避免走indexscan(全量语句见附件)
5)对语句进行explain verbose,查看计划,发现计划符合预期(即表dwimd.dwi_md_contract走tablescan,对于列存表计划上显式为CStore Scan)
6)对语句执行EXPLAIN ANALYZE操作(即实际执行语句),查看实际执行时间如下,发现SQL语句性能提升近10倍。全量的执行信息见附件

 
点击关注,第一时间了解华为云新鲜技术~

来源:https://www.cnblogs.com/huaweiyun/p/17119085.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具