最近做关键字排名,用下面的过程,当数据量到20万时候速度很慢,求高手帮忙优化,谢谢

evafly920 2005-08-05 11:56:12
最近做关键字排名,用下面的过程,当数据量到20万时候速度很慢,求高手帮忙优化,谢谢
Select * from A_Info as A
Inner Join B_Info As B On A.CorpID=B.CorpID

Where A.ParentID=0 and A.IsOpen=1 and A.IsAllow=1
and A.CorpID In (Select CorpID from B_Info where isAllow=1
and keyword=@Keyword and DateDiff(d,GetDate(),AddDate)<=ValidDay )

and A.AID In (Select Max(AID) from A_Info Group By CorpID)
Order By B.OrderRows Desc
效果如下
http://www.cn5135.com/Product/QueryResult.aspx?Keyword=%E6%89%8B%E6%9C%BA
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
最有可能大量浪费时间的是最后一行的表达式的设计错误。

其次是B.VAlidDay-B.AddDate,这个计算完全可以在表中预先计算出来并具有索引。

最后就是把其它几个字段加上索引。
  • 打赏
  • 举报
回复
上面丢了一行,所以看错了。假设你的CorpID是主见/外键,下面这个可能是对的:

select * from A_info as A
inner join B_Info As B On A.CorpID=B.CorpID
where A.ParentID=0 and A.IsOpen=1 and A.IsAllow=1
and B.isAllow=1 and B.keyword=@Keyword and B.VAlidDay-B.AddDate>getdate()
and A.AID In (Select Max(AID) from A_Info Group By CorpID)
tiaoci 2005-08-05
  • 打赏
  • 举报
回复
A.AID = @maxid

如果这个条件过滤得内容多的话,最好提到前面
tiaoci 2005-08-05
  • 打赏
  • 举报
回复
应当和这个等价

Select * from A_Info as A
Inner Join B_Info As B On A.CorpID=B.CorpID
Where A.ParentID=0 and A.IsOpen=1 and A.IsAllow=1
and B.IsAllow = 1 And B.keyword = @KeyWord
and DateDiff(d, GetDate(), AddDate) <= B.ValidDay
and A.AID In @maxid
Order By B.OrderRows Desc

剩下的要加快,就要考虑加索引了,比方给 A.AID加上索引,等等

  • 打赏
  • 举报
回复
* 不合理。而且能够正常工作吗?SQL Server应该报告你CorpID重名才对。

select * from A_info as A
inner join
where A.ParentID=0 and A.IsOpen=1 and A.IsAllow=1
and exists(select * from B_Info
where isAllow=1 and keyword=@Keyword and VAlidDay-AddDate>getdate()
and CorpID=A.CorpID
)
and A.AID In (Select Max(AID) from A_Info Group By CorpID)

1. 上面的*必须改为明确的字段列表。
2. 确保A_Info.AID,A_Info.ParentID,B_info.CorpID,B_Info.keyword能够利用索引。
3. 为 VAlidDay-AddDate 建立一个计算列(或者用触发器计算),并确保这个列上有索引,
则替换后的SQL会加快。
4. 最后一行的In表达式感觉你没有找准业务逻辑要求。你查询之所以很慢,可能跟这个逻辑
设计错误有关。
tiaoci 2005-08-05
  • 打赏
  • 举报
回复
A.CorpID In (Select CorpID from B_Info where isAllow=1
and keyword=@Keyword and DateDiff(d,GetDate(),AddDate)<=ValidDay )

这个 可以 直接在 外部过滤, 因为存在 A.CorpId = B.CorpId

所以和直接在外层查询中过滤是等效的, 但是会更快
tiaoci 2005-08-05
  • 打赏
  • 举报
回复
A.AID In (Select Max(AID) from A_Info Group By CorpID)

这个如果先查出 MaxId 然后用 A.AID = maxId 可能会快点吧
applelf2100 2005-08-05
  • 打赏
  • 举报
回复
你把*号换成数据库的字段名称,应该会提高速度

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧