我有2个表(AllClients
& AllActivities
),并需要检索以下信息:需要帮助提高SQL性能(子查询与联接)
我需要在那里最近的活动已被输入不同的客户名单去年。我已经得到了下面的代码来工作,但它很痛苦地慢,因此没有用。我相信一个连接(没有子查询)会更快,但我无法弄清楚。这是我当前的SQL语句:
select distinct(AllClients.LookupCode)
from AllClients
join (select LookupCode,
max(AllActivities.EnteredDate) as EnteredDate
from AllActivities
group by LookupCode) AllActivities
on AllClients.LookupCode = AllActivities.LookupCode
where AllClients.Name = '$userName'
and AllClients.TypeCode = 'P' and AllActivities.EnteredDate < '$oneYearAgo'";
子查询看起来很好。你可以先在'EnteredDate'上添加条件,而不是在主查询中进行。 – Sebas