简体   繁体   English

POSTGRES - 我如何优化这个连接查询

[英]POSTGRES - How do i optimize this join query

I have one table as below which stores employees details我有一张如下表,用于存储员工详细信息

empid空虚的 attribute属性 value价值 start_date开始日期 end_date结束日期
E1 E1 Active积极的 FALSE错误的 2020-01-01 2020-01-01 2022-05-05 2022-05-05
E1 E1 Active积极的 TRUE真的 2022-06-06 2022-06-06 9999-12-31 9999-12-31
E1 E1 Branch分支 NYC纽约市 2022-01-01 2022-01-01 9999-12-31 9999-12-31
E2 E2 Active积极的 TRUE真的 2020-01-01 2020-01-01 9999-12-31 9999-12-31

Then another table which have address details然后是另一个有地址详细信息的表

empid空虚的 city城市
E1 E1 CON缺点

What I want is我想要的是

  1. List of all the employee ids which do not have entries in address table地址表中没有条目的所有员工 ID 的列表

AND

  1. All those employees whose current value(today's value) of attribute ACTIVE is TRUE.属性 ACTIVE 的当前值(今天的值)为 TRUE 的所有员工。

I have written below query but its taking too much time to execute (3-4mins).我在下面写了查询,但执行起来花费了太多时间(3-4 分钟)。 Is there any way i can optimize this query.有什么办法可以优化此查询。

select distinct(emp.empid) from schema1.employees emp, schema2.address add 
where emp.empid = add.empid 
  and (emp.attribute ='ACTIVE' and emp.val in ('TRUE') 
  and CURRENT_DATE BETWEEN emp.start_date and emp.end_date)
  and emp.emp_id not in (select empid from schema2.address add2)
limit 20

I made only minor modifications to your query that could help performance.我只对您的查询进行了微小的修改,以提高性能。

select emp.empid
from schema1.employees emp
left join schema2.address add on emp.empid = add.empid 
where emp.attribute ='ACTIVE' 
  and vpd.val = 'TRUE'
  and emp.start_date <= CURRENT_DATE 
  and emp.end_date >= CURRENT_DATE 
  and add.empid is null;

The following indexes that can be useful as well.以下索引也很有用。 If you don't already have them, add them and try again.如果您还没有它们,请添加它们并重试。

create index ix1 on schema1.employees (attribute, val, start_date, end_date);

create index ix2 on schema2.address (empid);

From the description it's unclear how your two conditions should be combined.从描述中不清楚你的两个条件应该如何结合。

To get all employees that pass either of your two conditions :要让所有员工都通过您的两个条件之一

SELECT e.empid
FROM   schema1.employees e
WHERE  e.attribute = 'ACTIVE'
AND    e.val  -- = true
AND    CURRENT_DATE BETWEEN e.start_date AND e.end_date
OR     NOT EXISTS (SELECT FROM schema2.address a WHERE a.empid = e.empid);

To get all employees that pass both of your two conditions :让所有员工都通过你的两个条件

...
AND    NOT EXISTS (SELECT FROM schema2.address a WHERE a.empid = e.empid);

Assuming you only added DISTINCT (incorrectly with parentheses) to deal with duplicates introduced by the join.假设您只添加了DISTINCT (错误地使用括号)来处理由连接引入的重复项。 Not duplicating rows with an EXISTS expression in the first place, we also don't need DISTINCT .首先不使用EXISTS表达式复制行,我们也不需要DISTINCT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM