I have one table as below which stores employees details
empid | attribute | value | start_date | end_date |
---|---|---|---|---|
E1 | Active | FALSE | 2020-01-01 | 2022-05-05 |
E1 | Active | TRUE | 2022-06-06 | 9999-12-31 |
E1 | Branch | NYC | 2022-01-01 | 9999-12-31 |
E2 | Active | TRUE | 2020-01-01 | 9999-12-31 |
Then another table which have address details
empid | city |
---|---|
E1 | CON |
What I want is
- List of all the employee ids which do not have entries in address table
AND
- All those employees whose current value(today’s value) of attribute ACTIVE is TRUE.
I have written below query but its taking too much time to execute (3-4mins).
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
2
Answers
I made only minor modifications to your query that could help performance.
The following indexes that can be useful as well. If you don’t already have them, add them and try again.
From the description it’s unclear how your two conditions should be combined.
To get all employees that pass either of your two conditions:
To get all employees that pass both of your two conditions:
Assuming you only added
DISTINCT
(incorrectly with parentheses) to deal with duplicates introduced by the join. Not duplicating rows with anEXISTS
expression in the first place, we also don’t needDISTINCT
.