去年无聊的时候想到想玩一下根据ASCII表的排列顺序将字符串内的数值往前或者后移N个位,顺便看一下是T-SQL性能好还是用C#写CLR函数处理得快。结果是在50万行以下其实两者差距很小,当然这是在我的笔记本上测试的结果(I5双核四线程+8G内存+5600转的机械硬盘)。在100万行以上的时候用T-SQL基于集合的方式就体现出优势了,用了大概1分多钟就处理完100万行,而CLR用了4分多钟还没处理完。这里贴上当时的T-SQL代码。

if OBJECT_ID('tempdb..#tt') is not null
drop table #tt if OBJECT_ID('tempdb..#t') is not null
drop table #t if OBJECT_ID('tempdb..#ttt') is not null
drop table #ttt if OBJECT_ID('tempdb..#tttt') is not null
drop table #tttt select dbo.tblNumbers.id, abs(CHECKSUM(NEWID()))%62 as fld1,
abs(CHECKSUM(NEWID()))%62 as fld2,
abs(CHECKSUM(NEWID()))%62 as fld3,
abs(CHECKSUM(NEWID()))%62 as fld4,
abs(CHECKSUM(NEWID()))%62 as fld5,
abs(CHECKSUM(NEWID()))%62 as fld6,
abs(CHECKSUM(NEWID()))%62 as fld7,
abs(CHECKSUM(NEWID()))%62 as fld8,
abs(CHECKSUM(NEWID()))%62 as fld9,
abs(CHECKSUM(NEWID()))%62 as fld10,
abs(CHECKSUM(NEWID()))%62 as fld11,
abs(CHECKSUM(NEWID()))%62 as fld12,
abs(CHECKSUM(NEWID()))%62 as fld13,
abs(CHECKSUM(NEWID()))%62 as fld14,
abs(CHECKSUM(NEWID()))%62 as fld15,
abs(CHECKSUM(NEWID()))%62 as fld16 into #tt
from dbo.tblNumbers
WHERE ID<= 100000; select [str], ROW_NUMBER() over(order by CHECKSUM(NEWID()))-1 as rn into #t
from (select CHAR(ID+64) as [str] from dbo.tblNumbers where ID <= 26 union all
select CHAR(ID+96) as [str] from dbo.tblNumbers where ID <= 26 union all
select cast(ID-1 as varchar) as [str] from dbo.tblNumbers where ID <= 10) as t select tt.ID, left(t1.str + t2.str + t3.str + t4.str + t5.str + t6.str + t7.str + t8.str +
t9.str + t10.str + t11.str + t12.str + t13.str + t14.str + t15.str + t16.str + REPLICATE(' ',1000),1000) as result
into #tttt
from #tt as tt join
#t t1 on t1.rn = tt.fld1 join
#t t2 on t2.rn = tt.fld2 join
#t t3 on t3.rn = tt.fld3 join
#t t4 on t4.rn = tt.fld4 join
#t t5 on t5.rn = tt.fld5 join
#t t6 on t6.rn = tt.fld6 join
#t t7 on t7.rn = tt.fld7 join
#t t8 on t8.rn = tt.fld8 join
#t t9 on t9.rn = tt.fld9 join
#t t10 on t10.rn = tt.fld10 join
#t t11 on t11.rn = tt.fld11 join
#t t12 on t12.rn = tt.fld12 join
#t t13 on t13.rn = tt.fld13 join
#t t14 on t14.rn = tt.fld14 join
#t t15 on t15.rn = tt.fld15 join
#t t16 on t16.rn = tt.fld16 checkpoint
DBCC DROPCLEANBUFFERS
DBCC FREESYSTEMCACHE('all') select dbo.ufn_MoveCharacterBackOrForwardByNPos(result,'forward',1), result from #tttt
select dbo.ufn_clr_MoveCharacterBackOrForwardByNPos(result,'forward',1), result from #tttt select dbo.ufn_MoveCharacterBackOrForwardByNPos('oOv3lVLo4W3B44L7','forward',1)
select dbo.ufn_clr_MoveCharacterBackOrForwardByNPos('oOv3lVLo4W3B44L7','forward',1) declare @int as int = 1;
declare @cmd as varchar(max) = '';
declare @cmdd as varchar(max) = '';
declare @comand as varchar(max) = '';
declare @comandd as varchar(max) = ''; --select ISNULL(NULLIF(0,0),26) --select @@VERSION
while @int <= 101
begin
set @cmd = @cmd + 'substring(result,'+CAST(@int as varchar)+',1) as fld' +CAST(@int as varchar) + ','
set @cmdd = @cmdd + 'case when ASCII(fld' +CAST(@int as varchar) + ') between 65 and 90 then CHAR(ISNULL(NULLIF((ASCII(fld' +CAST(@int as varchar) + ')-64+1)%26,0),26)+64)
when ASCII(fld' +CAST(@int as varchar) + ') between 97 and 122 then CHAR(ISNULL(NULLIF((ASCII(fld' +CAST(@int as varchar) + ')-64+1)%26,0),26)+64)
when fld' +CAST(@int as varchar) + ' like ''[0-9]'' then CHAR(ISNULL(NULLIF((ASCII(fld' +CAST(@int as varchar) + ')-47+1)%10,0),10)+47)
else fld' +CAST(@int as varchar) + ' end +'; if LEN(@cmd) > 7950
begin
set @comand = @comand + cast(@cmd as varchar(max))
set @cmd = '';
end if LEN(@cmdd) > 7950
begin
--print len(@cmdd)
set @comandd = @comandd + cast(@cmdd as varchar(max))
set @cmdd = '';
end set @int = @int + 1
end
--select @cmdd
if LEN(@cmd) > 0
set @comand = 'select rtrim(result) AS result,' + @comand + cast(left(@cmd,len(@cmd)-1) as varchar(max)) + '
from #tttt as a'
--select @cmdd, LEN(@cmdd)
--select @comandd, LEN(@cmdd)
if LEN(@cmdd) > 0
set @comandd = 'select rtrim(' + @comandd + cast(left(@cmdd,len(@cmdd)-1) as varchar(max)) + ') as final_str, result
from (' + @comand + ') as t' --select @comandd
EXEC( @comandd) --select * from #ttt CREATE TYPE udt_TwoCharacterColumn8000Long
AS TABLE (
col1 VARCHAR(8000) NULL,
col2 VARCHAR(8000) NULL
) alter PROCEDURE usp_UDTtest
@t as udt_TwoCharacterColumn8000Long readonly
as
select * from @t declare @a as udt_TwoCharacterColumn8000Long insert into @a
select 'abc', 'def' exec usp_UDTtest @a

SQL Server ->> Move characters in string N position(s) forward/backward based on ASCII table(根据ASCII表的排列顺序将字符串内的数值往前或者后移N个位)的更多相关文章

  1. SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法

    create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...

  2. sql server alter column 由于一个或多个对象访问此列,ALTER TABLE ALTER COLUMN 失败

    今天在修改一个字段类型,由原来的 varchar(500) 增加到 varchar(1000) 则对应的SQL 语句,执行后如图错误 结果查下来是因为其中一个视图建成了索引视图, 解决办法:先将该视图 ...

  3. SQL Server 不清空数据,修改数据库字段、结构,阻止保存要求重新创建表的更改

    当数据库有数据修改数据库字段时,默认是阻止的! 工具---选项---设计器---阻止保存要求重新创建表的更改(取消钩)

  4. 转:Move all SQL Server system databases at one time

    Problem One task that you may need to do as a DBA is to move the system databases from one location ...

  5. Microsoft SQL Server Trace Flags

    Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...

  6. Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]

    http://sqlserverbuilds.blogspot.jp/   What version of SQL Server do I have? This unofficial build ch ...

  7. Consuming JSON Strings in SQL Server

    https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/ Consuming JS ...

  8. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...

  9. 转载--SQL Server 2005的XQuery介绍

    原文地址: http://bbs.51cto.com/thread-458009-1-1.html   引用: 摘要 本文介绍了SQL Server 2005能够支持的XQuery的各方面特性如FLW ...

随机推荐

  1. list和tuple

    1. 列表--list 列表用[]表示 l=[1,2,3,'zs'] print(l) 输出:[1,2,3,'zs'] 1.利用下标获取列表元素,下标从0开始 print(l[3]) 输出:zs pr ...

  2. Python第一天学习成果

    1.变成可执行程序,在开头需加#!/usr/bin/env python找解释器 2.Python不支持常量,但建议用全大写就表明是常量 3.Unicode:2字节 ASCII:1字节 UTF-8(可 ...

  3. 【DP】【单调队列】洛谷 P2216 [HAOI2007]理想的正方形 题解

        算是单调队列的复习吧,不是很难 题目描述 有一个$a\times b$的整数组成的矩阵,现请你从中找出一个$n\times n$的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 ...

  4. bzoj3098 Hash Killer II 生日共计

    题目传送门 题目大意: 让你构造一个字符串,使字符串在题目给出的哈希条件下统计出错. 思路:生日攻击,结论题,尚未理解. #include<bits/stdc++.h> #define C ...

  5. Oracle Pipelined Table Functions简介

    转自: http://www.linuxidc.com/Linux/2011-05/35797.htm //概况   //基本上,当你希望一个PLSQL(或者java或者c)程序作为数据源,而不是表, ...

  6. Linux中断分层--工作队列

    1. 工作队列是一种将任务推后执行的方式,它把推后的任务交由一个内核线程去执行.这样中断的下半部会在进程上下文执行,他允许重新调度甚至睡眠.每个被推后的任务叫做“工作”,由这些工作组成的队列称为工作队 ...

  7. PIE SDK地图鹰眼图

    鹰眼图,是GIS的一个基本功能,在鹰眼图上可以像从空中俯视一样查看地图框中所显示的地图在整个图中的位置,是对全局地图的一种概述表达,能够起到很好的空间提示和导航的作用.网上有很多Arcengine 二 ...

  8. FileZilla的使用(包括Server和Client两个程序)

    1.安装 FileZillaServer和FileZillaClient,到官网去下载 https://filezilla-project.org/ 2.启动 FileZillaServer 它会提示 ...

  9. 负载均衡服务器中存在大量的TIME_WAIT怎么解决

    首先需要明白什么是TIME_WAIT.TIME_WAIT是在tcp断开连接时进行四次回收的时候,主动断开端在收到被动关闭端的FIN包并发送ACK包给被动关闭后进入的状态.这个状态默认情况下是2倍的MS ...

  10. Java入门系列-06-运算符

    这篇文章为你搞懂2个问题 java 中的常用运算符有哪些?如何使用? 这些运算符的运算优先级是怎样的? 算数运算符 明显是做数学运算的,包括以下符号: + 加法运算 敲一敲: public class ...