SQL|SQL数据库中查找一条记录是否存在 count(推荐)
最快方法:SELECT top 1 @count=1,同时item_urlkey字段做了聚集索引或非聚集索引
如果语句执行时,不走索引的话,也可以用with(index=IX_t2_col1)指定索引,例如:select * from t2with(index=IX_t2_col1) where col1=201
GO
declare @num int
declare @beforDateTime datetime
declare @count int set @num=0;
set @beforDateTime=GETDATE()
while (@num<100000)
begin
set @count=0
SELECT top 1 @count=1 FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'
set @num=@num+1
end
print datediff(MILLISECOND,@beforDateTime,GETDATE())
其他方法1:SELECT top 1 @count=COUNT(主键)
/****** Script for SelectTopNRows command from SSMS******/
GO
declare @num int
declare @beforDateTime datetime
declare @count int set @num=0;
set @beforDateTime=GETDATE()
while (@num<100000)
begin
set @count=0
SELECT top 1 @count=COUNT(item_id) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'
set @num=@num+1
end
print datediff(MILLISECOND,@beforDateTime,GETDATE())
其他方法2:SELECT top 1 @count=COUNT(*)
/****** Script for SelectTopNRows command from SSMS******/
GO
declare @num int
declare @beforDateTime datetime
declare @count int set @num=0;
set @beforDateTime=GETDATE()
while (@num<100000)
begin
set @count=0
SELECT top 1 @count=COUNT(*) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'
set @num=@num+1
end
print datediff(MILLISECOND,@beforDateTime,GETDATE())
其他方法3:SELECT top 1 @count=COUNT(1)
/****** Script for SelectTopNRows command from SSMS******/
GO
declare @num int
declare @beforDateTime datetime
declare @count int set @num=0;
set @beforDateTime=GETDATE()
while (@num<100000)
begin
set @count=0
SELECT top 1 @count=COUNT(1) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'
set @num=@num+1
end
print datediff(MILLISECOND,@beforDateTime,GETDATE())
其中select top 1 1 形式是最快的,其他三种方法的速度都差不多。
【SQL|SQL数据库中查找一条记录是否存在 count(推荐)】
推荐阅读
- 热闹中的孤独
- Docker应用:容器间通信与Mariadb数据库主从复制
- Shell-Bash变量与运算符
- JS中的各种宽高度定义及其应用
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 深入理解Go之generate
- 异地恋中,逐渐适应一个人到底意味着什么()
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- “成长”读书社群招募