From 2d3b6810c7a55ee3252a6f99bf450dae9666058d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Mon, 17 Jul 2023 06:28:03 +0000 Subject: [PATCH] =?UTF-8?q?add=20sql/UncreditableSQL.=20=E5=85=AC=E5=8F=B8?= =?UTF-8?q?=E6=9C=89=E5=87=A0=E5=8D=81=E4=B8=8A=E7=99=BE=E5=BC=A0=E6=8A=A5?= =?UTF-8?q?=E8=A1=A8=20=E6=AF=8F=E4=B8=80=E5=BC=A0=E9=83=BD=E6=98=AF?= =?UTF-8?q?=E5=83=8F=E8=BF=99=E6=A0=B7=E7=9A=84=E4=B8=80=E4=B8=AASQL=20?= =?UTF-8?q?=E5=85=B6=E5=AE=9E=E8=BF=99=E8=BF=98=E4=B8=8D=E6=98=AF=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E7=9A=84=EF=BC=8C=E6=9C=80=E9=95=BF=E7=9A=84SQL?= =?UTF-8?q?=E6=B6=89=E5=8F=8A=E5=88=B0=E5=85=AC=E5=8F=B8=E5=86=85=E9=83=A8?= =?UTF-8?q?=E9=9A=90=E7=A7=81=E4=B8=8D=E8=83=BD=E6=94=BE=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=20=E6=B2=A1=E6=9C=89=E5=8E=8B=E7=BC=A9=EF=BC=8C=E8=BF=99?= =?UTF-8?q?=E5=B0=B1=E6=98=AF=E9=A1=B9=E7=9B=AE=E4=BB=93=E5=BA=93=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E6=BA=90=E4=BB=A3=E7=A0=81=20=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8D=E6=96=AD=E5=A2=9E=E9=95=BF=EF=BC=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BF=9D=E5=AD=98=E4=BA=86=E5=B7=AE?= =?UTF-8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TF-8?q?=E4=BA=A4=E5=8E=86=E5=8F=B2=E4=B9=9F=E8=A6=81=E7=9C=8B=E9=BA=BB?= =?UTF-8?q?=E4=BA=86=20=E7=8E=B0=E5=9C=A8=E4=BA=A7=E5=93=81=E7=BB=8F?= =?UTF-8?q?=E7=90=86=E8=AF=B4=E8=A6=81=E6=96=B0=E5=A2=9E=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E5=88=97=EF=BC=8C=E8=A6=81=E5=A4=9A=E6=9F=A52=E4=B8=AA?= =?UTF-8?q?=E8=A1=A8=EF=BC=8C=E6=88=91=E2=84=A2...=20BTM,=20=E5=86=99?= =?UTF-8?q?=E8=BF=99=E4=B8=AASQL=E7=9A=84=E4=BA=BA=E7=A6=BB=E8=81=8C?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 程序员小墨 <2291200076@qq.com> --- sql/UncreditableSQL | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 sql/UncreditableSQL diff --git a/sql/UncreditableSQL b/sql/UncreditableSQL new file mode 100644 index 0000000..5074475 --- /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 -- Gitee