Am mainly looking at improving the performance of the query and also whether be able to solve in a single query for one of my use case explained below:
There are 2 tables involved:
Table 1: EMPLOYEE (column1, column2, email1, email2, column5, column6)
Table 2: EMAILLIST (email)
My requirement is, I want to get/fetch all records from the table EMPLOYEE with the condition that either email1 or email2 do not have a matching entry in EMAILLIST table. To put it simply, if either email1 or email2 matches in EMAILLIST table, then those records should be ignored.
In this case, EMPLOYEE.EMAIL1, EMPLOYEE.EMAIL2 and EMAILLIST.EMAIL will always have single email address stored.
We’re using PostgreSQL v8.2.3, if it matters.
Any pointers/ideas/logic are appreciated.
UPDATE: Currently, we’ve implemented in this way: Fetched all records from EMPLOYEE table and stored in a Java object and for each entry (for loop), this in turn checks in EMAILLIST table, which is costly in terms of performance.
The most efficient way to handle this for every rdbms I’ve dealt with is to handle it with outer joins:
In general, I think you’ll find that any case where you put database queries into a loop will be, ummm, suboptimal. 🙂