SQL 问题请教

hyrongg 2009-06-02 02:50:09
SQL Server 2005
一个5百万的表,表结构如下
employees(idx bigint identity(1,1),name varchar(30),hiredate date,dept int)

两种方式效率那个比较快?
1: select top 1 * from employees where name like '%G%'
2: select top 1 * from (select * from employees where hiredate>dateadd(month,-3,getdate()))
tmp where name like '%G%'

注:select * from employees where hiredate>dateadd(month,-3,getdate())能够过滤掉很多数据,
结果可能只有几千条

看执行计划怎么没什么分别;

另外在SQL Server2005里面如何察看一个SQL 运行精确到多少毫秒的?
记得以前2000里面有,2005找半天没找到。


...全文
119 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
JonasFeng 2009-06-02
  • 打赏
  • 举报
回复
索引查询。

尽量不用IN
  • 打赏
  • 举报
回复
[Quote=引用楼主 hyrongg 的帖子:]
一个5百万的表,表结构如下
employees(idx bigint identity(1,1),name varchar(30),hiredate date,dept int)

两种方式效率那个比较快?
1: select top 1 * from employees where name like '%G%'
2: select top 1 * from (select * from employees where hiredate>dateadd(month,-3,getdate()))
tmp where name like '%G%'

[/Quote]


这两个sql的效率, 不能一概而论.
1.则需要看name中有关%G%中排序的问题.没有看到在name上有index,会做table scan, 如果第一次就scan出来有包含G的name, 结束sql,那就效率就很高,相反如果到最后在找到那么效率就很低.
2.利用子查询可以过滤掉一部分数据,对过滤之后queyr时依然和1一样.

鄙人认为单纯的比较这两条sql的效率没有意义, 但是平均效率都很差.
ws_hgo 2009-06-02
  • 打赏
  • 举报
回复
想到一块去啦
ws_hgo 2009-06-02
  • 打赏
  • 举报
回复
用这个
select top 1 * from employees where charindex(N'G',name)>0
ChinaJiaBing 2009-06-02
  • 打赏
  • 举报
回复
看看这样..

select top 1 * from employees where charindex('G',name)>0
--小F-- 2009-06-02
  • 打赏
  • 举报
回复
看看 不过效率上来说 in 最慢 exists快点
csdyyr 2009-06-02
  • 打赏
  • 举报
回复
set statistics time on
select top 1 * from employees where name like '%G%'
set statistics time off

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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