下面这段用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` 的序列,并且具有适当的权限来访问和使用它。