下面这段用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 08:30:08 浏览: 79
有几个可以优化的地方:
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 insert in to select
在PostgreSQL中,"INSERT INTO SELECT"是一种常见的SQL操作,也称为数据复制或数据导入,它允许你在一次查询中将查询结果插入到另一个表中。这种操作通常用于以下几个场景:
1. **数据填充**:当你有一个临时的结果集,希望将其完整地转移到另一个表中,而无需明确指定每一行。
2. **数据更新**:如果源查询的结果比目标表小,剩下的部分可以视为更新操作,即将新数据覆盖旧数据。
3. **创建备份**:快速创建一张表的副本,尤其是当源表很大时,通过这种方式可以避免大量数据传输。
基本语法如下:
```sql
INSERT INTO table_name (column1, column2, ...)
SELECT column1, column2, ... FROM source_table WHERE condition;
```
这里的`table_name`是你想插入数据的目标表,`column1, column2, ...`是目标表的列名,`source_table`是提供数据的源表,`condition`是可选的筛选条件,只选择满足这个条件的数据进行插入。
postgresql 效能
在提高PostgreSQL数据库的效能方面,有一些方法可以考虑。首先,你可以使用pg_index和pg_stat_user_indexes视图来查找未使用的索引。通过运行以下查询,你可以找到未被使用的索引并删除它们:
```
SELECT indrelid::regclass AS tab_name, pi.indexrelid::regclass AS unused_index, idx_scan
FROM pg_index pi, pg_stat_user_indexes psui
WHERE pi.indexrelid = psui.indexrelid
AND NOT indisunique
AND idx_scan = 0;
```
然后,你可以使用分区表的功能将大型的表分成子表。这样可以提高查询效率,尤其是当表的记录非常多时。通过将数据分散到多个子表中,可以减少查询的数据量,提高查询性能。你可以使用以下语法来创建一个分区表:
```
CREATE TABLE parent_table (
column1 data_type,
column2 data_type,
...
)
PARTITION BY RANGE (column1);
```
最后,当你找到未使用的索引后,不要急于直接删除它们。你可以使用以下SQL语句将这些索引标记为无效,然后观察应用程序的行为,确保没有出现任何性能问题:
```
UPDATE pg_index SET indisvalid = false WHERE indexrelid::regclass::text IN (<Unused indexes name>);
```
一旦确认没有问题,你可以使用`concurrently`关键字来并发地删除这些索引。这样可以避免对其他正在运行的查询造成影响。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [postgresql定位未使用的索引(unused index)](https://blog.csdn.net/weixin_43230594/article/details/123293465)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [PostgreSQL之分区表(partitioning)](https://download.csdn.net/download/weixin_38729022/13700051)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文