发表于 2023-03-31 | 阅读次数: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548指定引擎: engine=CollapsingMergeTree逻辑: 1.构建临时表2.向临时表中插入批跑出来的数据, 包含字段: 批返回的字段+ CREATE_TIME, CREATE_USER, UPDATE_TIME, INPUT_TYPE, _sign3.向目标表插入新增的主键数据(eg: 新增的PORT_ID,VALID_DATE)4.向目标表插入批跑出来的数据(关联目标表,按照主键merge,需要更新的字段从临时表获取,不需要更新的从目标表获取)5.整合更新最新数据①无分区键: 直接"optimize table " + table_name + " final"②有分区键: "optimize table dwd_val_detail PARTITION + 分区id+ final"案例:1. CREATE TABLE dwd_val_detail2C3wru86 AS dwd_val_detail engine=CollapsingMergeTree(_sign) PARTITION BY toYYYYMM(VALID_DATE)ORDER BY (SEC_ID, PORT_ID, SUB_PORT_ID, VALID_DATE, ACCOUNT_CLASS_TYPE) SETTINGS index_granularity = 8192 2.INSERT INTO tag_val_detailM7mZbs4d(PORT_ID, SUB_PORT_ID, SEC_ID, ACCOUNT_CLASS_TYPE, VALID_DATE, UPDATE_USER, REAL_SEC_ID, CREATE_TIME, CREATE_USER, UPDATE_TIME, INPUT_TYPE, _sign) VALUES3.insert into dwd_val_detail select * from dwd_val_detailgACo328j where (PORT_ID,SUB_PORT_ID,SEC_ID,VALID_DATE,ACCOUNT_CLASS_TYPE) not in (select DISTINCT PORT_ID,SUB_PORT_ID,SEC_ID,VALID_DATE,ACCOUNT_CLASS_TYPE from dwd_val_detail)4.insert into dwd_val_detail (SEC_ID, VALUATION_METHODS, BACK_AMOUNT, COUNTERPARTY_ID, END_DATE, INTECALC_RULE, UNDERLYING_CODE, UNDERLYING_VOLUME, ORDER_ID, F_JSFXJ, F_HHF, F_RGF, F_SGF, F_SHF, INT_RATE, ASS_CODE, CASH_PS_PAYMENT, BOND_PS_PAYMENT, F_YHS, F_JSF, F_GHF, F_ZGF, F_JSFWF, REGIST_ORG, REMARK, SETTLE_SPEED, PAR_VALUE, CPRICE_AMT, FPRICE_AMT, INV_AIM, TRAN_COMMISION, TRADER_NAME, IMGR_ID_NAME, TRAN_STATUS, CPRICE_DATE, IS_CANCEL, COUNTER_TRADER, ENT_INT_FLAG, LONG_CURRENCY, LONG_QUANTITY, LONG_PRINCIPAL, LONG_INTEREST, LONG_AMOUNT, LONG_TRAN_FEE, TRADE_ID, DEAL_DEP, TRAN_DATE, SETTLE_DATE, TRAN_SIDE, PRICE_YIELD, COUNTER_PARTY, Column39, Column40, Column41, SEC_ABBR, REAL_SEC_ID, NET_VAL_DATE, NET_VAL_PRICE, Column32, Column33, Column34, Column35, Column36, Column37, Column38, Column25, Column26, Column27, Column28, Column29, Column30, Column31, Column18, Column19, Column20, Column21, Column22, Column23, Column24, Column11, Column12, Column13, Column14, Column15, Column16, Column17, Column4, Column5, Column6, Column7, Column8, Column9, Column10, TRADE_MARKET, SEC_CATEGORY, ACC_ID, ACC_NAME, Column1, Column2, Column3, L_MV_LOSS, L_NET_MV_LOSS, NET_MV_LOSS, ASSET_BIG_TYPE, ASSET_CATEGORY, LEVEL_TYPE, PORT_NAME, _sign, SUB_PORT_ID, DEPT_ID, ASSET_LOSS, IS_LT_BOUNDED, L_ASSET_LOSS, FULL_MV_LOSS, UPDATE_USER, UPDATE_TIME, INPUT_TYPE, L_SHADOW_MV, L_SHADOW_DELTA, FULL_COST, L_FULL_COST, L_R_GL, L_GAINLOSS_GL, L_AMOR_GL, L_TOTAL_GL, CREATE_USER, CREATE_DEPT, CREATE_TIME, L_INTEREST, L_DIVIDEND, L_GAINLOSS, L_INCOME_GL, L_INTEREST_GL, L_EXPENSE, L_INVEST_GL, L_CURRENCY, L_ZYJ, L_MV, L_NET_MV, L_NET_COST, L_FULL_PRICE, L_NET_PRICE, EXPENSE, INVEST_GL, R_GL, GAINLOSS_GL, AMOR_GL, EX_GL, TOTAL_GL, NET_COST, FULL_PRICE, NET_PRICE, INTEREST, GAINLOSS, INCOME_GL, INTEREST_GL, SHADOW_MV, SHADOW_DELTA, PRINFXRATE, CURRENCY, ZYJ, FULL_MV, NET_MV, PORT_ID, VALID_DATE, INVALID_DATE, SEC_NAME, ACCOUNT_CLASS_TYPE, HOLDING_SIDE, HOLDING_AMOUNT)SELECT tb2.SEC_ID, tb1.VALUATION_METHODS, tb1.BACK_AMOUNT, tb1.COUNTERPARTY_ID, tb1.END_DATE, tb1.INTECALC_RULE, tb1.UNDERLYING_CODE, tb1.UNDERLYING_VOLUME, tb1.ORDER_ID, tb1.F_JSFXJ, tb1.F_HHF, tb1.F_RGF, tb1.F_SGF, tb1.F_SHF, tb1.INT_RATE, tb1.ASS_CODE, tb1.CASH_PS_PAYMENT, tb1.BOND_PS_PAYMENT, tb1.F_YHS, tb1.F_JSF, tb1.F_GHF, tb1.F_ZGF, tb1.F_JSFWF, tb1.REGIST_ORG, tb1.REMARK, tb1.SETTLE_SPEED, tb1.PAR_VALUE, tb1.CPRICE_AMT, tb1.FPRICE_AMT, tb1.INV_AIM, tb1.TRAN_COMMISION, tb1.TRADER_NAME, tb1.IMGR_ID_NAME, tb1.TRAN_STATUS, tb1.CPRICE_DATE, tb1.IS_CANCEL, tb1.COUNTER_TRADER, tb1.ENT_INT_FLAG, tb1.LONG_CURRENCY, tb1.LONG_QUANTITY, tb1.LONG_PRINCIPAL, tb1.LONG_INTEREST, tb1.LONG_AMOUNT, tb1.LONG_TRAN_FEE, tb1.TRADE_ID, tb1.DEAL_DEP, tb1.TRAN_DATE, tb1.SETTLE_DATE, tb1.TRAN_SIDE, tb1.PRICE_YIELD, tb1.COUNTER_PARTY, tb1.Column39, tb1.Column40, tb1.Column41, tb1.SEC_ABBR, tb2.REAL_SEC_ID, tb1.NET_VAL_DATE, tb1.NET_VAL_PRICE, tb1.Column32, tb1.Column33, tb1.Column34, tb1.Column35, tb1.Column36, tb1.Column37, tb1.Column38, tb1.Column25, tb1.Column26, tb1.Column27, tb1.Column28, tb1.Column29, tb1.Column30, tb1.Column31, tb1.Column18, tb1.Column19, tb1.Column20, tb1.Column21, tb1.Column22, tb1.Column23, tb1.Column24, tb1.Column11, tb1.Column12, tb1.Column13, tb1.Column14, tb1.Column15, tb1.Column16, tb1.Column17, tb1.Column4, tb1.Column5, tb1.Column6, tb1.Column7, tb1.Column8, tb1.Column9, tb1.Column10, tb1.TRADE_MARKET, tb1.SEC_CATEGORY, tb1.ACC_ID, tb1.ACC_NAME, tb1.Column1, tb1.Column2, tb1.Column3, tb1.L_MV_LOSS, tb1.L_NET_MV_LOSS, tb1.NET_MV_LOSS, tb1.ASSET_BIG_TYPE, tb1.ASSET_CATEGORY, tb1.LEVEL_TYPE, tb1.PORT_NAME, tb1._sign, tb2.SUB_PORT_ID, tb1.DEPT_ID, tb1.ASSET_LOSS, tb1.IS_LT_BOUNDED, tb1.L_ASSET_LOSS, tb1.FULL_MV_LOSS, tb2.UPDATE_USER, tb2.UPDATE_TIME, tb2.INPUT_TYPE, tb1.L_SHADOW_MV, tb1.L_SHADOW_DELTA, tb1.FULL_COST, tb1.L_FULL_COST, tb1.L_R_GL, tb1.L_GAINLOSS_GL, tb1.L_AMOR_GL, tb1.L_TOTAL_GL, tb2.CREATE_USER, tb1.CREATE_DEPT, tb2.CREATE_TIME, tb1.L_INTEREST, tb1.L_DIVIDEND, tb1.L_GAINLOSS, tb1.L_INCOME_GL, tb1.L_INTEREST_GL, tb1.L_EXPENSE, tb1.L_INVEST_GL, tb1.L_CURRENCY, tb1.L_ZYJ, tb1.L_MV, tb1.L_NET_MV, tb1.L_NET_COST, tb1.L_FULL_PRICE, tb1.L_NET_PRICE, tb1.EXPENSE, tb1.INVEST_GL, tb1.R_GL, tb1.GAINLOSS_GL, tb1.AMOR_GL, tb1.EX_GL, tb1.TOTAL_GL, tb1.NET_COST, tb1.FULL_PRICE, tb1.NET_PRICE, tb1.INTEREST, tb1.GAINLOSS, tb1.INCOME_GL, tb1.INTEREST_GL, tb1.SHADOW_MV, tb1.SHADOW_DELTA, tb1.PRINFXRATE, tb1.CURRENCY, tb1.ZYJ, tb1.FULL_MV, tb1.NET_MV, tb2.PORT_ID, tb2.VALID_DATE, tb1.INVALID_DATE, tb1.SEC_NAME, tb2.ACCOUNT_CLASS_TYPE, tb1.HOLDING_SIDE, tb1.HOLDING_AMOUNTFROM dwd_val_detail tb1, dwd_val_detail2C3wru86 tb2WHERE tb1.PORT_ID=tb2.PORT_ID AND tb1.SUB_PORT_ID=tb2.SUB_PORT_ID AND tb1.SEC_ID=tb2.SEC_ID AND tb1.VALID_DATE=tb2.VALID_DATE AND tb1.ACCOUNT_CLASS_TYPE=tb2.ACCOUNT_CLASS_TYPE'5."select partition_key from system.tables where database =(select database()) and name = 'dwd_val_detail'"===>找该表分区字段6."select distinct partition_id from system.parts where database =(select database()) and table = 'dwd_val_detail' and active = 1 and `partition` in (select distinct(toYYYYMM(VALID_DATE) ) from dwd_val_detail2C3wru86)"===>找涉及的分区7.optimize table dwd_val_detail PARTITION '202112' final