Dict.CN 在线词典, 英语学习, 在线翻译

上帝的问题

/*
原数据
 Id  Name  Addr Email 
 1   Roboth  bj    Roboth@163.com
 2   Xinsoft sh    Xinsoft@163.com
 3   LTP     gz    LTP@163.com
 4   Roboth hd   Roboth1@163.com
得到
 Id  Name  Addr Email  
 2   Xinsoft sh    Xinsoft@163.com
 3   LTP     gz    LTP@163.com
 4   Roboth hd   Roboth1@163.com
*/
DECLARE @t table
(
Id int,
[Name] varchar(200),
Addr varchar(200),
Email varchar(200)
)

Insert into @t
select 1,'Roboth','bj','Roboth@163.com'
Union all
select 2,'Xinsoft','sh','Xinsoft@163.com'
Union all
select 3,'LTP','gz','LTP@163.com'
Union all
select 4,'Roboth','hd','Roboth1@163.com'

 

 


--method1
select * from @t A
where
1>(
select COUNT(1) from  @t B where  A.[Name]=B.[Name] and A.ID<B.ID
)
--method2
select * from @t A
where
not exists (
select (1) from  @t B where  A.[Name]=B.[Name] and A.ID<B.ID
)
--Method3
select * from @t
where id in
(
select MAX(ID) from @t
group by [Name]
)
--Method4
select A.* from @t A
Inner join
(select MAX(ID) as id from @t
group by [Name]) B
on A.id=B.id

posted @ 2007-11-24 15:37 Robot·H 阅读(171) 评论(1)  编辑 收藏 所属分类: Sqlserver

  回复  引用  查看    
#1楼 [楼主]2007-11-26 16:01 | Robot·H      
交谈中请勿轻信汇款、中奖消息,勿轻易拨打陌生电话。

Robot·H (283984419) 15:35:07
楷模
Robot·H (283984419) 15:35:12
你知道性能的差异吗?
楷模(188202629) 15:35:16

楷模(188202629) 15:35:32
C#知识我一塌糊涂
Robot·H (283984419) 15:36:09
我指的是sqlserver上的性能
楷模(188202629) 15:36:48
不了解~~~~性能方面属于~~~~~未入门级、
Robot·H (283984419) 15:48:12
3500条数据用时
method1:12s
method2:12s
methoid3:45s
method4:3s
Robot·H (283984419) 15:48:25
你看出来有什么区别吗?
楷模(188202629) 15:48:47
看出来了
Robot·H (283984419) 15:48:48
第一,第二其实是一样的方法。表达方式不一样而已
Robot·H (283984419) 15:48:51
1<
Robot·H (283984419) 15:48:53
not exits
Robot·H (283984419) 15:49:01
第三种最耗时
楷模(188202629) 15:49:51
我没有看到你的4种方法具体是什么
Robot·H (283984419) 15:50:09
http://www.cnblogs.com/RobotH/archive/2007/11/24/971094.html
上帝之神(低调)(390688433) 15:50:29
低调路过
楷模(188202629) 15:50:58
先看下
Robot·H (283984419) 15:53:55

Robot·H (283984419) 15:54:33
可以看出最耗时的方法耗在排序上!
上帝之神(低调)(390688433) 15:55:07
这是什么
上帝之神(低调)(390688433) 15:55:10
可以这样分析
Robot·H (283984419) 15:55:23
一般不建议用in这样的where条件。他没有用到索引!
Robot·H (283984419) 15:55:30
还有不要用distinct
上帝之神(低调)(390688433) 15:55:39
听说守
上帝之神(低调)(390688433) 15:55:41

Robot·H (283984419) 15:55:51
恩!
上帝之神(低调)(390688433) 15:55:52
Robot·H,你是用什么工具分析的
Robot·H (283984419) 15:56:13
Sqlserver自带的执行计划!
上帝之神(低调)(390688433) 15:56:30

Robot·H (283984419) 15:57:04
所以有个结果:
表面上简单的sql语句,执行效率不一定最高!
上帝之神(低调)(390688433) 15:57:21
当然
Robot·H (283984419) 15:57:39
那楷模最喜欢的方法其实就是简单而已
Robot·H (283984419) 15:57:47
不堪一击
Robot·H (283984419) 15:57:55

楷模(188202629) 15:58:25

Robot·H (283984419) 16:00:43
楷模以为的想当然。
Robot·H (283984419) 16:00:47
嘻嘻、、、、、、、、、、

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: