我试图优化以下查询。我在考虑一个外连接可以做到这一点,但我无法将自己的想法包装在一起。MySQL的子查询优化 - 在不(子查询)
// ---------------------------------
// Simplified representation of data
// ---------------------------------
create table views (
user_id,
article_id
)
create table article_attributes (
article_id,
article_attribute_id
)
create table articles (
id,
title,
date
)
Views表有数千万条记录。 文章表有几十万。
我想匹配与之相关联的特定属性的所有文章,并没有被用户观看。
我都试过了,但没有很好地扩展:
select a.title, a.sid as article_id, a.total_views as times_read, a.date
from articles a
join article_attributes att on att.article_id = a.sid
where a.sid not in(
select v.article_id
from views v
join article_attributes att on att.article_id = v.article_id
where user_id = 132385
and att.article_attribute_id = 10
group by v.article_id
)
and att.article_attribute_id = 10
and a.date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 day)
order by total_views desc
limit 5
这工作得很好,但显著较慢得到更多的文章在用户浏览。任何想法或建议,将不胜感激。