我的用例可以合并到单个查询中而不影响性能吗?

发布于 2024-10-10 07:06:16 字数 619 浏览 0 评论 0原文

我主要着眼于提高查询的性能,以及是否能够在单个查询中解决我的一个用例,如下所述:

涉及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路终见情 2024-10-17 07:06:16

对于我处理过的每个 RDBMS 来说,处理这个问题的最有效方法是使用外连接来处理它:

SELECT whatever
FROM employee AS e
LEFT JOIN emaillist AS em1 ON e.email1 = em1.email
LEFT JOIN emaillist AS em2 ON e.email2 = em2.email
WHERE em1.id IS NULL
    AND em2.id IS NULL

一般来说,我认为您会发现将数据库查询放入循环中的任何情况都将是,嗯,次优。 :)

The most efficient way to handle this for every rdbms I've dealt with is to handle it with outer joins:

SELECT whatever
FROM employee AS e
LEFT JOIN emaillist AS em1 ON e.email1 = em1.email
LEFT JOIN emaillist AS em2 ON e.email2 = em2.email
WHERE em1.id IS NULL
    AND em2.id IS NULL

In general, I think you'll find that any case where you put database queries into a loop will be, ummm, suboptimal. :)

海未深 2024-10-17 07:06:16

有三种方法可以处理这个

LEFT JOIN

SELECT *
FROM employee AS e
LEFT JOIN emaillist AS em 
ON e.email1 = em.email
   or e.Email2 = em.email
 WHERE
     em.email is null

NOT EXISTS

SELECT *
FROM employee AS e
WHERE
   NOT EXISTS (SELECT * 
               FROM  
                 emaillist AS em 
              WHERE e.email1 = em.email
                  or e.email2 = em.Email)

NOT IN

SELECT *
FROM employee AS e
WHERE
    e.email1 NOT IN (SELECT email
               FROM  
                 emaillist)
    and
     e.email2 NOT IN (SELECT email
               FROM  
                 emaillist)

There are three ways to handle this

LEFT JOIN

SELECT *
FROM employee AS e
LEFT JOIN emaillist AS em 
ON e.email1 = em.email
   or e.Email2 = em.email
 WHERE
     em.email is null

NOT EXISTs

SELECT *
FROM employee AS e
WHERE
   NOT EXISTS (SELECT * 
               FROM  
                 emaillist AS em 
              WHERE e.email1 = em.email
                  or e.email2 = em.Email)

NOT IN

SELECT *
FROM employee AS e
WHERE
    e.email1 NOT IN (SELECT email
               FROM  
                 emaillist)
    and
     e.email2 NOT IN (SELECT email
               FROM  
                 emaillist)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文