diff --git a/sql/UncreditableSQL b/sql/UncreditableSQL new file mode 100644 index 0000000000000000000000000000000000000000..5074475aa067fae03ec1554496863cfceb37b13f --- /dev/null +++ b/sql/UncreditableSQL @@ -0,0 +1,12 @@ +-- 公司有几十上百张报表 +-- 每一张都是像这样的一个SQL +-- 其实这还不是最长的,最长的SQL涉及到公司内部隐私不能放出来 +-- 没有压缩,这就是项目仓库里的源代码 +-- 历史数据不断增长,数据库保存了差不多近一年多的前端所有交互 +-- 包括所有打开网页的用户的所有交互记录(比如点击按钮鼠标按下/弹起,往下滚动一下都调一次接口存一条记录) +-- 之前是每天统计一次,现在是每周统计一次,一个SQL跑30多个小时 +-- 由于都在一行,看git提交历史也要看麻了 +-- 现在产品经理说要新增几个列,要多查2个表,我™... +-- BTM, 写这个SQL的人离职了 + +select *, round(flow_node_total_success_count / flow_node_total_execute_count * 100,2) as node_execute_complete_rate, round(total_success_count / total_execute_count * 100,2) as flow_execute_complete_rate, round(complete_flow_count / (complete_flow_count + incomplete_flow_count) * 100,2) as complete_rate, round(online_count/created_count * 100,2) as online_rate round(online_count/flow_use_count * 100,2) as ou_percent, round(node_save_count/node_use_count * 100,2) as nsu_percent from (select substring(fd.create_time, 1, 10) as dim_0, from_timestamp( trunc( to_timestamp(fd.create_time, 'yyyy-MM-dd HH:mm:ss'), 'day' ), 'yyyy-MM-dd HH:mm:ss' ) as dim_0, substring(fd.create_time, 1, 7) as dim_0, ft.name, ft.id, ft.create_time, ft.last_modify_time, count(*) as created_count, SUM( IF(fd.status != 0 OR fd.VERSION > 1, 1, 0) ) as save_count, count( DISTINCT IF(fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1, fd.flow_id, NULL) ) as online_count, count( DISTINCT IF(fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1, u.user_code, NULL) ) as online_user_count, sum(complete) as complete_flow_count, count(*) - sum(complete) as incomplete_flow_count, sum(IF(from_template_id IS NULL and (channel_code != 'newTask' OR channel_code IS NULL), 1, 0)) as from_scratch_count, round(sum(IF(from_template_id IS NULL and (channel_code != 'newTask' OR channel_code IS NULL), 1, 0)) / count(*) * 100, 2) as from_scratch_rate, sum(IF(from_template_id IS NULL and (channel_code != 'newTask' or channel_code IS NULL) and flow_complete_flag = 1, 1, 0)) as from_scratch_complete_count, sum(IF(from_template_id IS NULL and (channel_code != 'newTask' or channel_code IS NULL) and flow_complete_flag = 0, 1, 0)) as from_scratch_incomplete_count, sum(IF(from_template_id IS NULL and (channel_code != 'newTask' or channel_code IS NULL), 1, 0)) = 0, 0, round( sum(IF(from_template_id IS NULL and (channel_code != 'newTask' or channel_code IS NULL) and flow_complete_flag = 0, 1, 0)) / sum(IF(from_template_id IS NULL and (channel_code != 'newTask' or channel_code IS NULL), 1, 0)) * 100, 2 ) ) as from_scratch_complete_rate, count( DISTINCT IF(from_template_id IS NULL and (channel_code != 'newTask' OR channel_code IS NULL) and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), fd.flow_id, NULL) ) as from_scratch_online_count, count( DISTINCT IF(from_template_id IS NULL and (channel_code != 'newTask' OR channel_code IS NULL) and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), u.user_code, NULL) ) as from_scratch_online_user_count, sum(IF(from_template_id IS NOT NULL, 1, 0)) as from_template_count, round(sum(IF(from_template_id IS NOT NULL, 1, 0)) / count(*) * 100, 2) as from_template_rate, sum(IF(from_template_id IS NOT NULL and flow_complete_flag = 1, 1, 0)) as from_template_complete_count, sum(IF(from_template_id IS NOT NULL and flow_complete_flag = 0, 1, 0)) as from_template_incomplete_count, sum(IF(from_template_id IS NOT NULL, 1, 0)) = 0, 0, round( sum(IF(from_template_id IS NOT NULL and flow_complete_flag = 1, 1, 0)) / sum(IF(from_template_id IS NOT NULL, 1, 0)) * 100, 2 ) ) as from_template_complete_rate, count( DISTINCT IF(from_template_id IS NOT NULL and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), fd.flow_id, NULL) ) as from_template_online_count, count( DISTINCT IF(from_template_id IS NOT NULL and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), u.user_code, NULL) ) as from_template_online_user_count, sum(IF(from_template_id IS NULL and channel_code = 'newTask', 1, 0)) as from_newer_count, round(sum(IF(from_template_id IS NULL and channel_code = 'newTask', 1, 0)) / count(*) * 100, 2) as from_newer_rate, sum(IF(from_template_id IS NULL and channel_code = 'newTask' and flow_complete_flag = 1, 1, 0)) as from_newer_complete_count, sum(IF(from_template_id IS NULL and channel_code = 'newTask' and flow_complete_flag = 0, 1, 0)) as from_newer_incomplete_count, sum(IF(from_template_id IS NULL and channel_code = 'newTask', 1, 0)) = 0, 0, round( sum(IF(from_template_id IS NULL and channel_code = 'newTask' and flow_complete_flag = 1, 1, 0)) / sum(IF(from_template_id IS NULL and channel_code = 'newTask', 1, 0)) * 100, 2 ) ) as from_newer_complete_rate, count( DISTINCT IF(from_template_id IS NULL and channel_code = 'newTask' and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), fd.flow_id, NULL) ) as from_newer_online_count, count( DISTINCT IF(from_template_id IS NULL and channel_code = 'newTask' and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1), u.user_code, NULL) ) as from_newer_online_user_count, sum(flow_node_execute_count) as flow_node_total_execute_count, sum(flow_node_success_count) as flow_node_total_success_count, sum(flow_node_failed_count) as flow_node_total_failed_count, ai.app_name, ae.event_name, count(distinct (fd.user_code)) as use_count, count(distinct (fd.flow_id)) as flow_use_count, count( DISTINCT IF(fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1, fd.flow_id, NULL) ) as online_count, count( DISTINCT IF(fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1, u.user_code, NULL) ) as online_user_count, count(fn.id) as node_use_count, sum(IF(fn.properties is not null, 1 ,0)) as node_save_count, ai.create_time, ai.last_modify_time, sum(flow_complete_flag) as flow_complete_count, count(*) - sum(flow_complete_flag) as flow_incomplete_count, round(sum(flow_complete_flag) / count(*) * 100,2) as flow_complete_rate, sum(node_execute_count) as flow_node_total_execute_count, sum(node_success_count) as flow_node_total_success_count, sum(node_failed_count) as flow_node_total_failed_count, sum(execute_count) as total_execute_count, sum(success_count) as total_success_count, sum(failed_count) as total_failed_count, sum(te.billing_task_execute_count) as total_billing_task_execute_count FROM lalala_untitled.production_database_pro_statis_v2_1_src_flow_definition fd lalala_untitled.production_database_pro_statis_v2_1_src_flow_node fn inner join lalala_untitled.production_database_pro_statis_v2_1_src_flow_definition fd on fn.flow_fd_id = fd.id INNER JOIN lalala_untitled.production_database_pro_statis_v2_1_src_flow_template ft on fd.from_template_id = ft.id inner join lalala_untitled.production_database_pro_statis_v2_1_src_app_info ai on fn.app_id = ai.id inner join lalala_untitled.production_database_pro_statis_v2_1_src_app_event ae on fn.action_key = ae.id INNER JOIN lalala_untitled.production_database_pro_statis_v2_1_src_user u on fd.user_code = u.user_code and unix_timestamp(fd.create_time) - unix_timestamp(u.create_time) < 24 * 60 * 60 and unix_timestamp(fd.create_time) - unix_timestamp(u.create_time) >= 24 * 60 * 60 LEFT JOIN (select flow_fd_id, sum(node_execute_count) as flow_node_execute_count, sum(node_success_count) as flow_node_success_count, sum(node_failed_count) as flow_node_failed_count FROM lalala_untitled.production_database_pro_statis_v2_1_src_flow_node fn1 INNER JOIN (select node_id, count(*) as node_execute_count, sum(IF(`status` = 1, 1, 0)) as node_success_count, sum(IF(`status` = 0, 1, 0)) as node_failed_count FROM lalala_untitled.production_database_pro_statis_v2_1_flow_execute_task group BY node_id ) n on n.node_id = fn1.id group BY flow_fd_id ) ne on ne.flow_fd_id = fd.id LEFT JOIN (select node_id, count(*) as node_execute_count, sum(IF(`status` = 1, 1, 0)) as node_success_count, sum(IF(`status` = 0, 1, 0)) as node_failed_count FROM lalala_untitled.production_database_pro_statis_v2_1_flow_execute_task group BY node_id ) n on n.node_id = fn.id LEFT JOIN (select flow_id, count(*) as execute_count, sum(IF(execute_status = 0, 1, 0)) as success_count, sum(IF(execute_status != 0, 1, 0)) as failed_count FROM lalala_untitled.production_database_pro_statis_v2_1_src_flow_log group BY flow_id ) fl on fl.flow_id = fd.flow_id LEFT JOIN (select fd1.id as id, sum(IF(fn1.properties is not null, 1 ,0)) as node_save_count, IF(count(*) = sum(IF(fn1.properties is not null, 1 ,0)), 1, 0) as flow_complete_flag from lalala_untitled.production_database_pro_statis_v2_1_src_flow_node fn1 inner join lalala_untitled.production_database_pro_statis_v2_1_src_flow_definition fd1 on fn1.flow_fd_id = fd1.id where fd1.is_del = 0 group by fd1.id ) as complete_flow_count on complete_flow_count.id = fd.id LEFT JOIN (select v.flow_fd_id as flow_fd_id, if( sum(if(v.properties is null, 1, 0)) > 0, 0, 1) as complete from lalala_untitled.production_database_pro_statis_v2_1_src_flow_node v, lalala_untitled.production_database_pro_statis_v2_1_src_flow_definition fd where v.flow_fd_id = fd.id and fd.is_del = 0 group by v.flow_fd_id ) ns on ns.flow_fd_id = fd.id LEFT JOIN (select flow_id, count(*) as billing_task_execute_count from lalala_untitled.production_database_pro_statis_v2_1_src_flow_billing_task group by flow_id ) te on te.flow_id = fd.id WHERE fd.is_del = 0 fd.from_template_id is not null and fd.is_del = 0 and fd.`current` = 1 and (fd.status = 1 OR fd.status = 2 OR fd.VERSION > 1) GROUP BY dim_0 ai.app_name, ai.create_time, ai.last_modify_time, dim_0 ai.app_name, ae.event_name, ai.create_time, ai.last_modify_time, dim_0 ft.name, ft.id, dim_0, ft.create_time, ft.last_modify_time ORDER BY dim_0 desc ) as group_table order by dim_0 desc as app_report order by dim_0 desc, flow_use_count desc \ No newline at end of file