27,579
社区成员
发帖
与我相关
我的任务
分享
--1
select distinct col from tb
--2
select col from tb group by col
select distinct col,col2,col3,col4........from tb
--study
------------------------------Version------------------------------------------
--Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
------------------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[date] datetime)
insert [tb] select rand()*10000,getdate()
go 1000000
select distinct id from tb
/*
(10000 行受影响)
SQL Server 执行时间:
CPU 时间 = 780 毫秒,占用时间 = 634 毫秒。
*/
select id from tb group by id
/*
(10000 行受影响)
SQL Server 执行时间:
CPU 时间 = 796 毫秒,占用时间 = 641 毫秒。
*/
--建立聚集索引
create clustered index idtbID ON tb(id)
select distinct id from tb
/*
(10000 行受影响)
SQL Server 执行时间:
CPU 时间 = 172 毫秒,占用时间 = 1495 毫秒。
*/
select id from tb group by id
/*
(10000 行受影响)
SQL Server 执行时间:
CPU 时间 = 172 毫秒,占用时间 = 1489 毫秒。
*/