T-SQL like charindex patindex 性能比较
事实上在网上可以找到很多这方面的资料,在这边就不多说了~主要观点在性能方面还是比较倾向于charindex,下面就测试下:
测试环境:共50批次,每批次50000数据,测试总共250万数据.
一、like搜索如下:
declare @count int
declare @batch int
declare @totalBatch int
declare @patternArr varchar(1000)
declare @pattern varchar(200)
declare @times int
declare @timediff datetime set @timediff = GETDATE()
set @times = 0
set @count = 50000
set @batch = 1
set @totalBatch = (select max(PostID) from club_Posts) / @count
set @patternArr = '\[contest\]%,%\[flv\]%' print '执行开始!'
while @batch < @totalBatch
begin declare @next int
set @next=1
while @next<=dbo.Get_StrArrayLength(@patternArr,',')
begin
set @pattern = dbo.Get_StrArrayStrOfIndex(@patternArr,',',@next)
update club_Posts set IsAudit=0 where PostID in
(
select PostID from club_Posts with(nolock) where IsActive=1 and (IsAudit=0 or IsAudit is null) and Body like @pattern {escape'\'} and PostID between (@batch-1)*@count and @batch*@count
)
set @times = @times + @@ROWCOUNT
set @next=@next+1
end
set @batch = @batch +1
if @batch > 1 break
end
select '执行完成!'+'成功执行'+cast(@times as varchar(20))+'次' as Success, '用时:' +cast(DATEDIFF(MS, @timediff, GETDATE()) as varchar(30)) +'毫秒' as OverTime
测试结果:--执行完成!成功执行4453次 用时:47233毫秒
declare @count int
declare @batch int
declare @totalBatch int
declare @patternArr varchar(1000)
declare @pattern varchar(200)
declare @times int
declare @timediff datetime set @timediff = GETDATE()
set @times = 0
set @count = 50000
set @batch = 1
set @totalBatch = (select max(PostID) from club_Posts) / @count
--set @patternArr = '\[contest\]%,%\[flv\]%'
set @patternArr = '[contest],[flv]' print '执行开始!'
while @batch < @totalBatch
begin declare @next int
set @next=1
while @next<=dbo.Get_StrArrayLength(@patternArr,',')
begin
set @pattern = dbo.Get_StrArrayStrOfIndex(@patternArr,',',@next)
-- update club_Posts set IsAudit=0 where PostID in
-- (
-- select PostID from club_Posts with(nolock) where IsActive=1 and (IsAudit=0 or IsAudit is null) and Body like @pattern {escape'\'} and PostID between (@batch-1)*@count and @batch*@count
select PostID from club_Posts with(nolock) where IsActive=1 and (IsAudit=0 or IsAudit is null) and charindex(@pattern,Body) > 0 and PostID between (@batch-1)*@count and @batch*@count
-- )
set @times = @times + @@ROWCOUNT
set @next=@next+1
end
set @batch = @batch +1
if @batch > 50 break
end
select '执行完成!'+'成功执行'+cast(@times as varchar(20))+'次' as Success, '用时:' +cast(DATEDIFF(MS, @timediff, GETDATE()) as varchar(30)) +'毫秒' as OverTime
--执行完成!成功执行4511次 用时:67410毫秒
结果还是有点让我吃惊啊,虽然上面有点小差异,而且网上还是有很多观点。
下面我再测试50万数据看看:
--执行完成!成功执行3次 用时:11203毫秒 用时:10610毫秒 (like)
-- 用时:10470毫秒 (patindex)
--执行完成!成功执行24次 用时:15226毫秒 用时:15850毫秒 (charindex)
都差不多,不过patindex和charindex稍微优一点,不过差别好像不在。
T-SQL like charindex patindex 性能比较的更多相关文章
- SQL Server添加MDW性能监控报表(转载)
10.2 Data Collector与MDW Data Collection功能是SQL SERVER 2005版本提供的数据库监控报表的功能,通过定时地对数据库的语句运行情况,服务器各种资源的监控 ...
- 新 Azure SQL 数据库服务等级的性能
4 月 24 日,我们发布了 SQL Database 基本级(预览版)和标准级(预览版)新服务等级的预览版以及新的业务连续性功能.在本博客文章中,我们将深入探究 SQL Database 中新等级的 ...
- SQL Server-聚焦存储过程性能优化、数据压缩和页压缩提高IO性能(一)
前言 关于SQL Server基础系列尚未结束,还剩下最后一点内容未写,后面会继续.有园友询问我什么时候开始写SQL Server性能系列,估计还得等一段时间,最近工作也比较忙,但是会陆陆续续的更新S ...
- 【原创】分布式之数据库和缓存双写一致性方案解析(三) 前端面试送命题(二)-callback,promise,generator,async-await JS的进阶技巧 前端面试送命题(一)-JS三座大山 Nodejs的运行原理-科普篇 优化设计提高sql类数据库的性能 简单理解token机制
[原创]分布式之数据库和缓存双写一致性方案解析(三) 正文 博主本来觉得,<分布式之数据库和缓存双写一致性方案解析>,一文已经十分清晰.然而这一两天,有人在微信上私聊我,觉得应该要采用 ...
- windows系统与SQL SERVER 2008数据库服务性能监控分析简要
软件系统性能测试体系流程介绍之windows系统与SQL SERVER 2008数据库服务性能监控分析简要 目前大部分测试人员对操作系统资源.中间件.数据库等性能监控分析都是各自分析各自的监控指标方式 ...
- SQL中Charindex和Oracle中对应的函数Instr
转:http://blog.csdn.net/zhuyu19911016520/article/details/8568640 sql :charindex('字符串',字段)>0 charin ...
- sql server charindex函数和patindex函数详解(转)
charindex和patindex函数常常用来在一段字符中搜索字符或字符串.假如被搜索的字符中包含有要搜索的字符,那么这两个函数返回一个非零的整数,这个整数是要搜索的字符在被搜索的字符中的开始位数. ...
- sql中charindex
假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.我将告诉你这两个函数是如何运转的,解 ...
- Sql中CHARINDEX用法
CHARINDEX作用 写SQL语句我们经常需要判断一个字符串中是否包含另一个字符串,但是SQL SERVER中并没有像C#提供了Contains函数,不过SQL SERVER中提供了一个叫CHAEI ...
随机推荐
- ECONOMETRICS CHAPTER2
♣回归函数/条件期望函数 Regression Function 前面已说过,回归分析是研究一个因变量对解释变量的依赖关系,将这种关系表示为函数形式就是回归函数,说明因变量的均值是如何随着解释变量的变 ...
- Dalvik源码阅读笔记(一)
dalvik 虚拟机启动入口在 JNI_CreateJavaVM(), 在进行完 JNIEnv 等环境设置后,调用 dvmStartup() 函数进行真正的 DVM 初始化. jint JNI_Cre ...
- php curl 传输是url中带有空格的处理方法
在crul中,应该用%20代替空格,否则,空格以后的数据将get不到.
- Unity 3D-Canvas画布的三种模式
Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- 2017.4.9 函数式编程FP
函数编程(简称FP)不只代指Haskell Scala等之类的语言,还表示一种编程范式,和面向对象的编程方式一样,是编程思维,软件思考方式,也称面向函数编程. 编程的本质是组合,组合的本质是范畴Cat ...
- (7)MySQL的事务
什么是事物: 作用:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像 ...
- CH4701 天使玩偶
题意 4701 天使玩偶 0x40「数据结构进阶」例题 描述 题目PDF 样例输入 2 3 1 1 2 3 2 1 2 1 3 3 2 4 2 样例输出 1 2 来源 石家庄二中Violet 3杯省选 ...
- bash scripts收集
只保留代码中的头文件声明 #! /bin/sh echo "leave only INCluding declaration in c files" find $1 -name ...
- day 55 jQuery 之事件 绑定等
属性选择器: [attribute] [attribute=value]// 属性等于 列入 $("input[value='male']").prop('chekced') 注意 ...
- oracle单词
OEM Blackouts n. 黑朦:灯火管制(blackout的复数)Projection n. 投射:规划:突出:发射:推测premium adj. 高价的:优质的 ############## ...