日期:2014-05-18  浏览次数:20414 次

求sql 语句优化
各位大神,帮忙优化一下这条sql语句,速度太慢了?
select c.EName,p.Id,p.Title,p.AddTime,p.CategoryId,
(case when p.id in (select t.ProductId from Product p left join ProductCertificateItem t on p.Id=t.ProductId where t.CertificateItemId=131) then 1 else 0 end) as flag
from Product p left join Category c on p.CategoryId=c.Id
where c.status=1 and p.status=1 and p.UserId=6001

------解决方案--------------------
SQL code
select c.EName,p.Id,p.Title,p.AddTime,p.CategoryId,
case when EXISTS(select 1 from  ProductCertificateItem t WHERE p.Id=t.ProductId AND t.CertificateItemId=131) then 1 else 0 end as flag
from Product p left join Category c on p.CategoryId=c.Id
where c.status=1 and p.status=1 and p.UserId=6001