SQL数据库中查找一条记录是否存在 count(推荐)
来源:互联网 发布:简便算法公式 编辑:程序博客网 时间:2024/05/27 14:12
最快方法:SELECT top 1 @count=1,同时item_urlkey字段做了聚集索引或非聚集索引
如果语句执行时,不走索引的话,也可以用with(index=IX_t2_col1)指定索引,例如:select *from t2 with(index=IX_t2_col1)where col1=201
GOdeclare @num int declare @beforDateTime datetime declare @count int set @num=0;set @beforDateTime=GETDATE()while (@num<100000)beginset @count=0SELECT top 1 @count=1 FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'set @num=@num+1endprint datediff(MILLISECOND,@beforDateTime,GETDATE())
其他方法1:SELECT top 1 @count=COUNT(主键)
/****** Script for SelectTopNRows command from SSMS ******/GOdeclare @num int declare @beforDateTime datetime declare @count int set @num=0;set @beforDateTime=GETDATE()while (@num<100000)beginset @count=0SELECT top 1 @count=COUNT(item_id) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'set @num=@num+1endprint datediff(MILLISECOND,@beforDateTime,GETDATE())
其他方法2:SELECT top 1 @count=COUNT(*)
/****** Script for SelectTopNRows command from SSMS ******/GOdeclare @num int declare @beforDateTime datetime declare @count int set @num=0;set @beforDateTime=GETDATE()while (@num<100000)beginset @count=0SELECT top 1 @count=COUNT(*) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'set @num=@num+1endprint datediff(MILLISECOND,@beforDateTime,GETDATE())其他方法3:SELECT top 1 @count=COUNT(1)
/****** Script for SelectTopNRows command from SSMS ******/GOdeclare @num int declare @beforDateTime datetime declare @count int set @num=0;set @beforDateTime=GETDATE()while (@num<100000)beginset @count=0SELECT top 1 @count=COUNT(1) FROM [iWOMTrackData].[dbo].[SEC_Item] WHERE [Item_URLKey]='e6d71e8d1c410cd84317897be0d0b5b6'set @num=@num+1endprint datediff(MILLISECOND,@beforDateTime,GETDATE())
其中select top 1 1 形式是最快的,其他三种方法的速度都差不多。
- SQL数据库中查找一条记录是否存在 count(推荐)
- 判断SQL数据库是否存在表,是否存在记录
- SQL数据库group by的用法--从相同记录中查找任意一条记录
- MySQL查找数据库中是否存在表
- 用SQL查找Access数据库中某一表是否存在的一种方法
- SQL Server中判断数据库是否存在
- 一条sql语句判断数据是否存在
- SQL查找表是否存在
- 用SQL语句判断数据库中的记录是否存在
- 一条记录中包含多个count
- sql中查找表名和字段是否存在
- sql中存储过程中判断记录是否存在时
- Java中查找某个表是否在数据库已经存在
- 查找oracle数据库表中是否存在系统关键字
- 数据库insert and update--查询是否主键存在和插入语句用一条sql来实现。
- 判断表中是否存在记录的SQL语句
- SQL 判断表中是否存在记录的高效率方法
- 从数据库中随机抽取一条记录的SQL语句
- 51 建立链表
- JavaScript中的值类型与引用类型
- presentModalViewControllerAnimated 和 dismissModalViewControllerAnimated
- 显卡的工作原理
- 线型感知机
- SQL数据库中查找一条记录是否存在 count(推荐)
- POJ 1149 PIGS (最大流Dinic)
- 关于listview 的onItemClick事件无法得到响应
- 如何不用图片绘制图形精灵
- 让你的APP支持iPhone5
- Activity生命周期管理之三——Stopping或者Restarting一个Activity
- Android入门/利用SharedPreferences保存配置信息(十九)
- 51单片机的软件复位
- MIT review 机器产生创意?克里斯托弗·斯坦纳(CHRISTOPHER STEINER)