关于数据库的优化

nattystyle 2010-02-02 11:20:13
http://blog.csdn.net/tmri_vio/archive/2008/11/05/3221695.aspx

中间写到:

5.in 和 not in 也要慎用,否则会导致全表扫描,如:
select id from t where num in(1,2,3)

不太理解这话的意思,如果不用in我该如何实现此功能呢?

现在我要执行的语句为:

select * from [LOOKYX_EMS].[dbo].[t_dataGather] where [ga_updatetime] in
(
select min([ga_updatetime]) from [LOOKYX_EMS].[dbo].[t_dataGather]
group by convert(char(10),[ga_updatetime],120)
)

请帮忙优化下
...全文
73 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2010-02-02
  • 打赏
  • 举报
回复
select * from [LOOKYX_EMS].[dbo].[t_dataGather] t 
where
exists(
select min([ga_updatetime]) from [LOOKYX_EMS].[dbo].[t_dataGather]
group by convert(char(10),[ga_updatetime],120) and [ga_updatetime]=t.[ga_updatetime]
)
黄_瓜 2010-02-02
  • 打赏
  • 举报
回复
select id from t where num in(1,2,3)

select id from t where num =1 or num =2 or num=3
这两条 优化器会自己优化的,效率应该一样
lwg211 2010-02-02
  • 打赏
  • 举报
回复
3楼的是不是不太实用?

如果是select id from t where num in(1,2,3)
SQL code
select id from t where num =1 or num =2 or num=3


如果这个in里面的太多,写起来会比较麻烦,呵呵。

我是初学者,所以比较喜欢用in,但确实效率低下
zhousq00 2010-02-02
  • 打赏
  • 举报
回复
如果是在程序中执行,可以考虑分开执行
先执行
select min([ga_updatetime]) from [LOOKYX_EMS].[dbo].[t_dataGather] 
group by convert(char(10),[ga_updatetime],120)

再把查出的结果带到下一个查询中。

如果是select id from t where num in(1,2,3)

select id from t where num =1 or num =2 or num=3
--小F-- 2010-02-02
  • 打赏
  • 举报
回复
select
*
from
[LOOKYX_EMS].[dbo].[t_dataGather] t
where
exists( select min([ga_updatetime]) from [LOOKYX_EMS].[dbo].[t_dataGather]
group by convert(char(10),[ga_updatetime],120) and [ga_updatetime]=t.[ga_updatetime]
)

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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