下面这段用PostgreSQL语法写的SQL,还有哪些可以优化的地方?select source_name as "SOURCE_NAME",type_name as "TYPE_NAME",shift_date as "SHIFT_DATE",dd as "DD",task_title as "TASK_TITLE", task_content as "TASK_CONTENT",task_creator as "TASK_CREATOR",task_executor as "TASK_EXECUTOR",task_description as "TASK_DESCRIPTION", create_time as "CREATE_TIME",creatorid as "CREATORID",creatorname as "CREATORNAME",org_id as "ORG_ID",executorid as "EXECUTORID",executorname as "EXECUTORNAME", plan_start_time as "PLAN_START_TIME",plan_end_time as "PLAN_END_TIME",act_start_time as "ACT_SART_TIME",act_end_time as "ACT_END_TIME", gap_date as "GAP_DATE",task_status as "TASK_STATUS",1 as "TASK_QTY", (case when task_status='Finish' then '已结案' when task_status='Confirm'then '已结案' when gap_date>0 then '已逾期' --直播状态如下 --when gap_date>0 and gap_date<=1 then '已逾期' when gap_date>0.3 then '已结案' when gap_date<=0 and task_status='Going' then '进行中' when gap_date<=0 and task_status='Plan' then '计划中' end ) as "STATUS" -------union from ((select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - plan_end_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.r_est_task WHERE SITE = 'S01' --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) union (select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - create_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.h_Est_Comp WHERE SITE = 'S01' and substr(pt_mfg_date,1,6)>=to_char(current_date-100,'yyyymm') --and to_number(substr(pt_mfg_date,5,2),'99G999D')>=extract(month from current_date)-3 --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) )xx left join (select emp_no as CreatorID,emp_name as CreatorName from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01')yy on xx.task_creator = yy.CreatorID left join (select emp_no as ExecutorId,emp_name as ExecutorName,org_id from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01' )aa on xx.task_executor = aa.ExecutorId

时间: 2024-04-06 15:30:08 浏览: 13
有几个可以优化的地方: 1. 对于 estone.r_est_task 表和 estone.h_Est_Comp 表的 WHERE 子句,可以将常量字符串 'S01' 和日期 '20221031' 抽取出来,分别作为参数传入,这样可以避免每次执行时都要重新解析 WHERE 子句。 2. 对于 estone.h_Est_Comp 表,可以使用 BETWEEN 运算符来代替 substr(pt_mfg_date,1,6)>=to_char(current_date-100,'yyyymm'),这样可以让查询更易读,也更符合 SQL 的语义。 3. 在左连接时,可以使用 USING 子句代替 ON 子句,根据字段名自动匹配相同的列,这样可以让 SQL 查询更简洁。 4. 如果任务数量很大,可以考虑使用分页机制,对结果进行分页展示,避免一次性返回大量的数据,降低系统资源的压力。
相关问题

postgresql 可以使用 || 拼接例如 SELECT anchor_name ||'PK' anchor_name,mysql如何实现

在 PostgreSQL 中,使用 || 运算符可以实现字符串的拼接操作。例如,你提到的查询语句可以在 PostgreSQL 中使用如下方式实现: SELECT anchor_name || 'PK' AS anchor_name FROM table_name; 这将会将 anchor_name 字段的值与字符串 'PK' 进行拼接,并将结果作为新的列名 anchor_name 返回。 而在 MySQL 中,可以使用 CONCAT 函数来实现字符串的拼接操作。对应的查询语句可以如下编写: SELECT CONCAT(anchor_name, 'PK') AS anchor_name FROM table_name; 这将会将 anchor_name 字段的值与字符串 'PK' 进行拼接,并将结果作为新的列名 anchor_name 返回。

oracle中sql SELECT S_STN_TRADE_TRX.NEXTVAL FROM DUAL 优化成pg数据库该怎么写

在 Oracle 数据库中,`SELECT S_STN_TRADE_TRX.NEXTVAL FROM DUAL` 是用来获取序列的下一个值。在 PostgreSQL (pg) 数据库中,可以使用 `nextval()` 函数来实现相同的功能。 假设在 PostgreSQL 中你有一个名为 `S_STN_TRADE_TRX` 的序列,你可以使用以下语法来获取下一个值: ```sql SELECT nextval('S_STN_TRADE_TRX') AS next_value; ``` 这将返回序列 `S_STN_TRADE_TRX` 的下一个值,并将其作为 `next_value` 列的结果返回。 请确保在 PostgreSQL 中已经创建了名为 `S_STN_TRADE_TRX` 的序列,并且具有适当的权限来访问和使用它。

相关推荐

帮我优化postgresql语句,如下:select source_name as "SOURCE_NAME",type_name as "TYPE_NAME",shift_date as "SHIFT_DATE",dd as "DD",task_title as "TASK_TITLE", task_content as "TASK_CONTENT",task_creator as "TASK_CREATOR",task_executor as "TASK_EXECUTOR",task_description as "TASK_DESCRIPTION", create_time as "CREATE_TIME",creatorid as "CREATORID",creatorname as "CREATORNAME",org_id as "ORG_ID",executorid as "EXECUTORID",executorname as "EXECUTORNAME", plan_start_time as "PLAN_START_TIME",plan_end_time as "PLAN_END_TIME",act_start_time as "ACT_SART_TIME",act_end_time as "ACT_END_TIME", gap_date as "GAP_DATE",task_status as "TASK_STATUS",1 as "TASK_QTY", (case when task_status='Finish' then '已结案' when task_status='Confirm'then '已结案' when gap_date>0 then '已逾期' --直播状态如下 --when gap_date>0 and gap_date<=1 then '已逾期' when gap_date>0.3 then '已结案' when gap_date<=0 and task_status='Going' then '进行中' when gap_date<=0 and task_status='Plan' then '计划中' end ) as "STATUS" -------union from ((select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - plan_end_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.r_est_task WHERE SITE = 'S01' --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) union (select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - create_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.h_Est_Comp WHERE SITE = 'S01' and substr(pt_mfg_date,1,6)>=to_char(current_date-100,'yyyymm') --and to_number(substr(pt_mfg_date,5,2),'99G999D')>=extract(month from current_date)-3 --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) )xx left join (select emp_no as CreatorID,emp_name as CreatorName from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01')yy on xx.task_creator = yy.CreatorID left join (select emp_no as ExecutorId,emp_name as ExecutorName,org_id from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01' )aa on xx.task_executor = aa.ExecutorId

最新推荐

recommend-type

postgreSQL+pgpool+pg_rman高可用实施手册.docx

postgreSQL编译安装及主从流复制、pgpool安装及配置、pg_rman安装及备份恢复脚本。 其中,针对postreSQL的新旧版本的主从流复制时配置项及标识、pgpool新旧版本集群节点数区别等均做了说明。 适合项目实施,可以直接...
recommend-type

PostgreSQL慢SQL调优手册

5、少用outer join,减少不必要的sub-query层级数【在不影响得到正确结果的前提下】 6、坚决避免select * 和 redundant columns【多余字段】 7、Index on Expressions 8、Partial Indexes 9、Decompose DDL【分解DDL...
recommend-type

SQL Server 2012链接服务器到PostgreSQL

SQL Server 2012链接服务器到PostgreSQLSQL Server 2012链接服务器到PostgreSQLSQL Server 2012链接服务器到PostgreSQLSQL Server 2012链接服务器到PostgreSQLSQL Server 2012链接服务器到PostgreSQL
recommend-type

在PostgreSQL中使用日期类型时一些需要注意的地方

主要介绍了在PostgreSQL中使用日期类型时一些需要注意的地方,包括时间戳和日期转换等方面,需要的朋友可以参考下
recommend-type

Postgresql数据库批量导入其他pg库的表

个人完整的源码安装步骤,希望对你有用。亲测可用。Centos6.2下,以postgresql_fdw驱动连接其他pg数据库,并批量导入表
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。