MaxCompute元數據分析冷熱門表,充分優化數據模型

1、需求場景分析
在實際的數據平臺運營管理過程當中,數據表的規模每每隨着更多業務數據的接入以及數據應用的建設而逐漸增加到很是大的規模,數據管理人員每每但願可以利用元數據的分析來更好地掌握不一樣數據表的使用狀況,從而優化數據模型。
一個MaxCompute項目中常常使用的表簡稱爲熱門表,使用次數較少或者很長時間不使用的表簡稱爲冷門表,本文將介紹如何去經過MaxCompute元數據信息去分析熱門表和冷門表。
2、方案設計思路
MaxCompute Information_Schema提供了項目中全量的表元數據信息Tables以及包含訪問表的做業明細數據tasks_history,經過彙總各個表被做業訪問的次數能夠獲知不一樣表被做業使用的頻度。
詳細步驟以下:
一、熱門數據經過獲取tasks_history表裏的input_tables字段的詳細信息,而後經過count統計必定時間分區內的各個表使用次數
二、冷門數據經過tables和tasks_history裏的input_tables表的做業彙總數量進行關聯、排序,從而統計出各張表在規定時間內的使用次數,正序排列
3、方案實現方法
一、獲取tasks_history表裏的input_tables字段的詳細信息。以下圖所示:
select 
inst_id ,
input_tables,
output_tables,
start_time,
end_time 
from information_schema.tasks_history 
where ds='20190902'limit 100;函數

查詢數據的結果以下圖所示:
1
發如今tasks_history表中input_tables字段格式爲
["lightning.customer","lightning.orders_delta"]
因此在統計的時候須要對字段進行按逗號分割
注意:案例中的時間分區能夠根據需求去調整範圍,區間根據實際場景去作相應的調整
例如:Ds>='20190902' and Ds<='20190905'
函數處理以下:
select
--去掉input_tables 字段中開始和結尾的[]
trans_array(1,",",inst_id,replace(replace(input_tables,"[",""),"]","")) as (inst_id,input_table)
from information_schema.tasks_history
--日期能夠根據實際需求去限定,這裏以20190902爲例
where ds='20190902' limit 100;優化

處理結果以下圖:
1spa

二、統計熱門表數據SQL編寫:
select 
--按表名進行統計計算
input_table
,count(distinct inst_id) table_read_num 
from 
(
select 
--去掉input_tables 字段中開始和結尾的[]
trans_array(1,",",inst_id,replace(replace(input_tables,"[",""),"]","")) as (inst_id,input_table) 
from information_schema.tasks_history 
where ds='20190902' 
) t
group by input_table
order by table_read_num desc
limit 1000;設計

結果以下圖所示:
1regexp

三、統計冷門表數據SQL編寫:
經過tables和tasks_history裏的input_tables表的做業彙總數量進行關聯、排序,從而統計出各張表在規定時間內的使用次數,正序排列。
select
t1.table_schema,
t1.table_name,
--兩表關聯
if(t2.table_read_num is null,0,table_read_num) as table_read_num
FROM information_schema.tables t1 
left join(
select
--去掉表名先後的」符號
regexp_replace(t.input_table,""","") as input_table
,count(distinct t.inst_id) table_read_num
from
(
select
--去掉input_tables 字段中開始和結尾的[]
trans_array(1,",",inst_id,replace(replace(input_tables,"[",""),"]","")) as (inst_id,input_table)
from information_schema.tasks_history 
where ds='20190902' )t
group by input_table
)t2
--關聯條件匹配
on concat('your_project_name.',t1.table_name)=t2.input_table
order by table_read_num desc
limit 1000;orm

結果以下所示:
1
1
全部的表按照使用次數進行排序
便可獲得各個表的使用次數排序信息。從而去進行合理化的管理數據表。
注意:SQL中的」 your_project_name.」爲表名前綴,客戶須要參照本身的實際數據去作相應的修改調整。blog

 

 

查看更多:https://yqh.aliyun.com/detail/6669排序

上雲就看雲棲號:更多雲資訊,上雲案例,最佳實踐,產品入門,訪問:https://yqh.aliyun.com/input

相關文章
相關標籤/搜索