MERGE 与NO_MERGE

本文探讨了SQL优化中的关键概念,包括如何使用MERGE和NO_MERGE提示来控制视图的合并行为,以及这些策略如何影响查询计划和性能。
19. /*+ MERGE(TABLE) */  
  能够对视图的各个查询进行相应的合并.  
  例如:  
  SELECT /*+ MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELET DPT_NO  
  ,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO  
  AND A.SAL>V.AVG_SAL;  
20. /*+NO_MERGE(TABLE)*/  
  对于有可合并的视图不再合并.  
  例如:  
  SELECT /*+NO_MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELECT DPT_NO,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO AND

A.SAL>V.AVG_SAL;  
  21. /*+ORDERED*/  



 SELECT /*+no_merge(XJ) */
              (SELECT DISTINCT T.ZUOY_STAFF FROM VIW_YW_ZYRYJL T WHERE T.DANJ_NO=XJ.DANJ_NO AND T.HANGHAO=XJ.HANGHAO AND T.CAOZJS_ID IN(
               SELECT A.JIAOS_ID FROM KH_JC_JSZD A WHERE A.JIAOS_NAME IN('拆零拣货员','整件拣货员'))) ZUOY_STAFF,
               XJ.DANJ_NO,
               SP.SHANGP_NO,
               SUM(XJ.SHIJ_NUM) ZSL,
               COUNT(DISTINCT XJ.DANJ_NO||XJ.HANGHAO) TMS,
               COUNT(DISTINCT XJ.SHANGP_ID) PGS,
               ROUND(SUM(XJ.SHIJ_NUM / SP.BAOZ_NUM),2) JS
          FROM  JC_SPZL SP,VIW_CK_KPD_HZ ZY, VIW_YW_XJZL XJ
         WHERE ZY.DANJ_NO = XJ.DANJ_NO
           AND SP.SHANGP_ID = xj.SHANGP_ID
           AND XJ.YEW_TYPE IN ('2', '3')
           AND XJ.KUB = 'LHK'
           AND XJ.HUOW_ID <> '冲'
       --    and  TRUNC(cast(SHENGCHEN_TIME AS DATE)) >= TRUNC(TO_DATE('2016-06-22' ,'yyyy-mm-dd'))
        --   AND  TRUNC(cast(SHENGCHEN_TIME AS DATE)) <  TRUNC(TO_DATE('2016-06-22' ,'yyyy-mm-dd') + 1)
          AND cast(XJ.SHENGCHEN_TIME AS DATE) >= TRUNC(TO_DATE('2016-06-01' ,'yyyy-mm-dd'))
           AND cast(XJ.SHENGCHEN_TIME AS DATE) <  TRUNC(TO_DATE('2016-06-22' ,'yyyy-mm-dd') + 1)
           AND XJ.SHIJ_NUM > 0
           AND SUBSTR(SP.SHANGP_NO,1,1) <>'P'
           AND SUBSTR(SP.SHANGP_NO,1,1)<>'Z'
         GROUP BY
                  XJ.DANJ_NO,
                  XJ.PINGX_NO,
                  XJ.HANGHAO,
                  SP.SHANGP_NO,
                  XJ.LOT


qn@RAC> select b.a,v.x from b b,( select a,count(1) x  from a  group by a ) v where b.a=v.a and b.a=1
  2  /

--优化器选择为NO_MERGE(v),HASH GROUP BY 的视图里操作,谓词推入成功;
NO_MERGE 表示视图不合并,不展开

Execution Plan
----------------------------------------------------------
Plan hash value: 1009994850

-----------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |        |      4 |    156 |      6   (0)| 00:00:01 |
|*  1 |  HASH JOIN         |        |      4 |    156 |      6   (0)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL  | B    |      1 |     13 |      3   (0)| 00:00:01 |
|   3 |   VIEW             |        |      4 |    104 |      3   (0)| 00:00:01 |
|   4 |    HASH GROUP BY     |        |      4 |     52 |      3   (0)| 00:00:01 |
|*  5 |     TABLE ACCESS FULL| A    |      4 |     52 |      3   (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("B"."A"="V"."A")
   2 - filter("B"."A"=1)
   5 - filter("A"=1)

Note
-----
   - dynamic sampling used for this statement (level=2)
   


--强制为MERGE视图展开,b与v合并在一起。 HASH GROUP BY 在视图外操作,谓词推入成功
qn@RAC> select /*+ MERGE(v) */ b.a,v.x from b b,( select a,count(1) x  from a  group by a ) v where b.a=v.a and b.a=1

Execution Plan
----------------------------------------------------------
Plan hash value: 583771949

----------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time       |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |       |     4 |   152 |     7  (15)| 00:00:01 |
|   1 |  HASH GROUP BY        |       |     4 |   152 |     7  (15)| 00:00:01 |
|*  2 |   HASH JOIN        |       |     4 |   152 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL| B    |     1 |    25 |     3   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| A    |     4 |    52 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."A"="A")
   3 - filter("B"."A"=1)
   4 - filter("A"=1)
小结:
   MERGE与NO_MERGE并不影响谓词推入
   MERGE 是什么与视图合并关联
   NO_MERGE 是视图做合并关联





MERGE才是展开视图并做关联
qn@RAC> select  b.a,count(1) from  b b,a a where a.a=b.a and b.a=1 group by b.a
  2  /


Execution Plan
----------------------------------------------------------
Plan hash value: 759381190

-----------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |        |      4 |    104 |      6   (0)| 00:00:01 |
|   1 |  SORT GROUP BY NOSORT|        |      4 |    104 |      6   (0)| 00:00:01 |
|*  2 |   HASH JOIN         |        |      4 |    104 |      6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL | B    |      1 |     13 |      3   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL | A    |      4 |     52 |      3   (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A"."A"="B"."A")
   3 - filter("B"."A"=1)
   4 - filter("A"."A"=1)


qn@RAC> select /*+ MERGE(v)*/ b.a,v.x from b b,( select a,count(1) x  from a  group by a ) v where b.a=v.a and b.a=1  2  
Execution Plan
----------------------------------------------------------
Plan hash value: 583771949

----------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time       |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |       |     4 |   152 |     7  (15)| 00:00:01 |
|   1 |  HASH GROUP BY        |       |     4 |   152 |     7  (15)| 00:00:01 |
|*  2 |   HASH JOIN        |       |     4 |   152 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL| B    |     1 |    25 |     3   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| A    |     4 |    52 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("B"."A"="A")
   3 - filter("B"."A"=1)
   4 - filter("A"=1)






merge_root_path = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_MERGE_NAME) translation_excel_folder_path = os.path.join(merge_root_path, 'Translation_Excel') if not os.path.exists(translation_excel_folder_path): print('目标翻译文件 {0} 路径不存在'.format(translation_excel_folder_path)) exit(1) merge_android_src_path = os.path.join(merge_root_path, 'Android', 'values-old') merge_android_dst_path = os.path.join(merge_root_path, 'Android', 'values-new') merge_ios_src_path = os.path.join(merge_root_path, 'iOS', 'strings-old') merge_ios_dst_path = os.path.join(merge_root_path, 'iOS', 'strings-new') merge_flutter_src_path = os.path.join(merge_root_path, 'Flutter', 'l10n-old') merge_flutter_dst_path = os.path.join(merge_root_path, 'Flutter', 'l10n-new') merge_pc_src_path = os.path.join(merge_root_path, 'PC', 'old') merge_pc_dst_path = os.path.join(merge_root_path, 'PC', 'new') enable_android_merge = os.path.exists(merge_android_src_path) enable_ios_merge = os.path.exists(merge_ios_src_path) enable_flutter_merge = os.path.exists(merge_flutter_src_path) enable_pc_merge = os.path.exists(merge_pc_src_path) if not os.path.exists(merge_android_dst_path): os.makedirs(merge_android_dst_path) if not os.path.exists(merge_ios_dst_path): os.makedirs(merge_ios_dst_path) if not os.path.exists(merge_flutter_dst_path): os.makedirs(merge_flutter_dst_path) if not os.path.exists(merge_pc_dst_path): os.makedirs(merge_pc_dst_path) if enable_android_merge or enable_ios_merge or enable_flutter_merge or enable_pc_merge: refer_excel_file = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_COMMON_NAME, 'Refer_Standard.xls') if os.path.exists(refer_excel_file): refer_tuple = TranslateInfoExcelReader.read_excel_content_list(refer_excel_file) else: refer_tuple = [] translation_excel_file_list = os.listdir(translation_excel_folder_path) translation_excel_file_list = [x for x in translation_excel_file_list if x.endswith(".xlsx")] curr_time = time.strftime('%Y-%m-%d-%X', time.localtime(time.time())).replace(":", '') android_log_file_name = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_MERGE_NAME, 'Translation_Log', "AndroidLog" + str(curr_time) + ".txt") flutter_log_file_name = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_MERGE_NAME, 'Translation_Log', "FlutterLog" + str(curr_time) + ".txt") pc_log_file_name = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_MERGE_NAME, 'Translation_Log', "PCLog" + str(curr_time) + ".txt") ios_log_file_name = os.path.join(Config.CURRENT_WORKSPACE_ROOT_PATH, Config.CURRENT_WORKSPACE_MERGE_NAME, 'Translation_Log', "Log-漏翻记录.txt") extract_root_path = os.path.join(Config.WORKSPACE_BASE_PATH ) locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'all_englist_translate_dict.text') all_english_translate_dict = dict() with open(locale_json_dst_file, 'r', encoding='utf-8') as f: all_english_translate_dict = json.load(f) if os.path.exists(ios_log_file_name): os.remove(ios_log_file_name) for excel in translation_excel_file_list: excel_file = os.path.join(translation_excel_folder_path, excel) print(excel_file) excel_dict = TranslateInfoExcelReader.read_excel_content_dict(excel_file) for item in excel_dict.values(): check_formatter(item.english, item.translate_result) for refer in refer_tuple: if refer.translate_result in excel_dict: english = refer.english platform = refer.platform no = refer.no note = refer.note translate = FormatSpecifierParser.build_format_char(refer.english, refer.translate_result, excel_dict[ refer.translate_result].translate_result) excel_dict[english] = TranslateInfo(english, translate, platform, no, note) locale = excel.replace('.xlsx', '') for item in excel_dict.values(): # print(excel, item.english, item.translate_result) if item.english in all_english_translate_dict: all_english_translate_dict[item.english][locale] = item.translate_result else: tmp_translate_dict = dict() tmp_translate_dict[locale] = item.translate_result all_english_translate_dict[item.english] = tmp_translate_dict if enable_android_merge and locale in Config.ANDROID_LOCALE_DICT: locale_path = 'values-' + Config.ANDROID_LOCALE_DICT[locale] merge_android_src_english_file = os.path.join(merge_android_src_path, 'values', 'strings.xml') merge_android_src_locale_file = os.path.join(merge_android_src_path, locale_path, 'strings.xml') merge_android_dst_locale_path = os.path.join(merge_android_dst_path, locale_path) XmlMerger(excel_dict, merge_android_src_english_file, merge_android_src_locale_file, merge_android_dst_locale_path, android_log_file_name).merge(1) if enable_ios_merge and locale in Config.IOS_LOCALE_DICT: locale_path = Config.IOS_LOCALE_DICT[locale] + '.lproj' merge_ios_src_english_path = os.path.join(merge_ios_src_path, 'en.lproj') merge_ios_dst_english_path = os.path.join(merge_ios_dst_path, 'en.lproj') merge_ios_src_locale_path = os.path.join(merge_ios_src_path, locale_path) merge_ios_dst_locale_path = os.path.join(merge_ios_dst_path, locale_path) StringsMerger(locale, excel_dict, merge_ios_src_english_path, merge_ios_dst_english_path, merge_ios_src_locale_path, merge_ios_dst_locale_path).merge() if enable_flutter_merge and locale in Config.FLUTTER_LOCALE_DICT: locale_path = Config.FLUTTER_LOCALE_DICT[locale] merge_flutter_src_english_file = os.path.join(merge_flutter_src_path, 'intl_en.arb') merge_flutter_src_locale_file = os.path.join(merge_flutter_src_path, 'intl_' + locale_path + '.arb') merge_flutter_dst_locale_path = os.path.join(merge_flutter_dst_path, 'intl_' + locale_path + '.arb') FlutterMerger(excel_dict, merge_flutter_src_english_file, merge_flutter_src_locale_file, merge_flutter_dst_locale_path, flutter_log_file_name).merge(1) if enable_pc_merge and locale in Config.PC_LOCALE_DICT: locale_path = Config.PC_LOCALE_DICT[locale] merge_pc_src_english_file = os.path.join(merge_pc_src_path, 'en.json') merge_pc_src_locale_file = os.path.join(merge_pc_src_path, locale_path + '.json') merge_pc_dst_locale_path = os.path.join(merge_pc_dst_path, locale_path + '.json') FlutterMerger(excel_dict, merge_pc_src_english_file, merge_pc_src_locale_file, merge_pc_dst_locale_path, pc_log_file_name).merge(1) locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'all_englist_translate_dict_new.text') with open(locale_json_dst_file, 'w', encoding='utf8') as f2: json.dump(all_english_translate_dict, f2, ensure_ascii=False, indent=2) 代码解析
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值