日期:2014-05-17 浏览次数:20496 次
SELECT * FROM TEST WHERE 主键 NOT IN ( SELECT 主键 FROM TEST WHERE ColumnA='A' AND ColumnB='B' )
SELECT * FROM TEST WHERE NOT (ColumnA='A' AND ColumnB='B')
SELECT * FROM TEST WHERE ((ColumnA='A' AND ColumnB<>'B') OR (ColumnA<>'A' AND ColumnB<>'B') OR (ColumnA<>'A' AND ColumnB='B') )
select * from TEST as a where not exists (select 1 from TEST WHERE ColumnA='A' AND ColumnB='B' and 主键=a.主键)
------解决方案--------------------
--最好的解决方法 declare @StartTime datetime set @StartTime = getdate() [你的SQL 语句] select datediff(ms,@StartTime,getdate()) --时间最小,语句最优
------解决方案--------------------
同意2楼,因为带exit量词的相关子查询只关心内层查询是否有返回值,并不需要查询具体值,因此效率可能会比不相关子查询高
------解决方案--------------------
not exists其实也会做表扫描,它是算为“不可参数化”的样式。
------解决方案--------------------
--用于生成示例数据,这里是100W条数据 if OBJECT_ID('Test') is not null drop table Test create table Test(ID [bigint] identity primary key, ColumnA char(1) NULL, ColumnB char(1) NULL) INSERT INTO dbo.Test SELECT case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end, case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end DECLARE @i AS INT, @rc AS INT,@max int; SET @rc = 1; SET @max = 1000000; set @i = ceiling(log10(@max)/LOG10(2))-1; WHILE @rc <= @i BEGIN INSERT INTO dbo.Test SELECT case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end, case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end FROM dbo.Test; SET @rc = @rc +1 ; END select @max = @max - @@IDENTITY INSERT INTO dbo.Test SELECT top(@max) case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end, case when abs(checksum(newid()))%(3) = 0 then char(65+abs(checksum(newid()))%(26)) when abs(checksum(newid()))%(2) = 0 then char(97+abs(checksum(newid()))%(26)) end FROM dbo.Test
------解决方案--------------------