优化 PostgreSQL 中的查询

发布于 2024-11-02 02:21:52 字数 654 浏览 0 评论 0原文

可能的重复:
优化 PostgreSQL 中的查询

SELECT count(*) 
FROM contacts_lists 
     JOIN plain_contacts 
          ON contacts_lists.contact_id = plain_contacts.contact_id 
     JOIN contacts 
          ON contacts.id = plain_contacts.contact_id 
WHERE plain_contacts.has_email 
      AND NOT contacts.email_bad 
      AND NOT contacts.email_unsub 
      AND contacts_lists.list_id =67339

在此处 contacts_lists.contact_idcontact_lists.list_id 已编入索引 如何优化这个查询?

Possible Duplicate:
optimize Query in PostgreSQL

SELECT count(*) 
FROM contacts_lists 
     JOIN plain_contacts 
          ON contacts_lists.contact_id = plain_contacts.contact_id 
     JOIN contacts 
          ON contacts.id = plain_contacts.contact_id 
WHERE plain_contacts.has_email 
      AND NOT contacts.email_bad 
      AND NOT contacts.email_unsub 
      AND contacts_lists.list_id =67339

In here contacts_lists.contact_id and contacts_lists.list_id are indexed
how to optimize this query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吃颗糖壮壮胆 2024-11-09 02:21:52

您可能希望对 contacts.id 或 plain_contacts.contact_id 建立索引以加快连接速度。这两个字段之一实际上应该是主键。

如果这还不够,您可能需要重构数据库。为什么首先要有 contact 和 plain_contacts 表?

You probably want to index either contacts.id or plain_contacts.contact_id to speed up the join. One of the two fields should actually be a primary key.

If this is not sufficient, you will probably need to refactor the database. Why is there contacts and plain_contacts tables to begin with?

鼻尖触碰 2024-11-09 02:21:52

由于您只想包含在连接表中设置了一些标志的行,因此我会将这些语句移至连接子句中:

SELECT count(*) 
FROM contacts_lists 
     JOIN plain_contacts 
          ON contacts_lists.contact_id = plain_contacts.contact_id 
          AND NOT plain_contacts.has_email
     JOIN contacts 
          ON contacts.id = plain_contacts.contact_id 
          AND NOT contacts.email_unsub 
          AND NOT contacts.email_bad 
WHERE contacts_lists.list_id =67339

我不确定这是否会对性能产生很大影响,但值得一试。为了获得最佳性能,您可能还应该在连接表上建立索引,如下所示:

plain_contacts: contact_id, has_email
contacts: id, email_unsub, email_bad

Since you only want to inlude rows that has some flags set in the joined tables, I would move that statements into the join clause:

SELECT count(*) 
FROM contacts_lists 
     JOIN plain_contacts 
          ON contacts_lists.contact_id = plain_contacts.contact_id 
          AND NOT plain_contacts.has_email
     JOIN contacts 
          ON contacts.id = plain_contacts.contact_id 
          AND NOT contacts.email_unsub 
          AND NOT contacts.email_bad 
WHERE contacts_lists.list_id =67339

I'm not sure if this would make a great impact on performance, but worth a try. You should probably have indexes on the joined tables as well for optimal performance, like this:

plain_contacts: contact_id, has_email
contacts: id, email_unsub, email_bad
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文