我的用例可以合并到单个查询中而不影响性能吗?
我主要着眼于提高查询的性能,以及是否能够在单个查询中解决我的一个用例,如下所述:
涉及 2 个表:
Table 1: EMPLOYEE (column1, column2, email1, email2, column5, column6)
Table 2: EMAILLIST (email)
我的要求是,我想从表 EMPLOYEE 中获取/获取所有记录,条件是 email1 或 email2 在 EMAILLIST 表中没有匹配的条目。简而言之,如果 email1 或 email2 在 EMAILLIST 表中匹配,那么这些记录应该被忽略。
在这种情况下,EMPLOYEE.EMAIL1、EMPLOYEE.EMAIL2 和 EMAILLIST.EMAIL 将始终存储单个电子邮件地址。
如果重要的话,我们正在使用 PostgreSQL v8.2.3。
任何指针/想法/逻辑都值得赞赏。
更新:目前,我们以这种方式实现:从 EMPLOYEE 表中获取所有记录并将其存储在 Java 对象中,对于每个条目(for 循环),这依次检查 EMAILLIST 表,即就性能而言,代价高昂。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于我处理过的每个 RDBMS 来说,处理这个问题的最有效方法是使用外连接来处理它:
一般来说,我认为您会发现将数据库查询放入循环中的任何情况都将是,嗯,次优。 :)
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. :)
有三种方法可以处理这个
LEFT JOIN
NOT EXISTS
NOT IN
There are three ways to handle this
LEFT JOIN
NOT EXISTs
NOT IN