优化 PostgreSQL 中的查询
可能的重复:
优化 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_id
和 contact_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能希望对 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?
由于您只想包含在连接表中设置了一些标志的行,因此我会将这些语句移至连接子句中:
我不确定这是否会对性能产生很大影响,但值得一试。为了获得最佳性能,您可能还应该在连接表上建立索引,如下所示:
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:
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: