SQLSERVER2008中CTE的Split与CLR的性能比较
1: /// <summary>
/// SQLs the array.
/// </summary>
/// <param name="str">The STR.</param>
/// <param name="delimiter">The delimiter.</param>
/// <returns></returns>
/// 1/8/2010 2:41 PM author: v-pliu
[SqlFunction(Name = "CLR_Split",
FillRowMethodName = "FillRow",
TableDefinition = "id nvarchar(10)")]
public static IEnumerable SqlArray(SqlString str, SqlChars delimiter)
{
if (delimiter.Length == 0)
return new string[1] { str.Value };
return str.Value.Split(delimiter[0]);
}
/// <summary>
/// Fills the row.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="str">The STR.</param>
/// 1/8/2010 2:41 PM author: v-pliu
public static void FillRow(object row, out SqlString str)
{
str = new SqlString((string)row);
}
然后Bulid,Deploy一切OK后,在SSMS中执行以下测试T-sql:
DECLARE @array VARCHAR(max)
SET @array = '39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22,14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34,47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51' SELECT id FROM dbo.CLR_Split(@array,',')
我们来看它的Client Statistic:
接着我们执行测试T-sql使用相同的array:
DECLARE @array VARCHAR(max)
SET @array = '39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22,14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34,47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51'
SELECT item FROM strToTable(@array,',')
CTE实现的Split function的Client statistic:
通过对比,你可以发现CLR的performance略高于CTE方式,原因在于CLR方式有Cache功能,并且把一个复杂的运算放到程序里比DataBase里更加高效。
您还可以参考:
Split string in SQL Server 2005+ CLR vs. T-SQL
Author:Petter Liu
相关文章
SQLSERVER2008中CTE的Split与CLR的性能比较
之前曾有一篇POST是关于用CTE实现Split,这种方法已经比传统的方法高效了。今天我们就这个方法与CLR实现的Split做比较。在CLR实现Split函数的确很简单,dotnet framework本身就有这个function了。2011-10-10SQL Server 2008+ Reporting Services (SSRS)使用USER登录问题
这篇文章主要介绍了SQL Server 2008+ Reporting Services (SSRS)使用USER登录问题的解决办法,十分的实用,有需要的小伙伴可以参考下。2015-06-06安装SQL Server 2008时 总是不断要求重启电脑的解决办法
本篇文章是对安装SQL Server 2008时,总是不断要求重启电脑的解决办法进行了详细的分析介绍,需要的朋友参考下2013-06-06win2008 r2安装SQL SERVER 2008 R2 不能打开1433端口设置方法
这篇文章主要介绍了win2008 r2安装SQL SERVER 2008 R2 不能打开1433端口设置方法,需要的朋友可以参考下2017-01-01图文详解Windows Server2012 R2中安装SQL Server2008
这篇文章主要以图文结合的方式向大家推荐Windows Server2012 R2中安装SQL Server2008的详细过程,感兴趣的小伙伴们可以参考一下2015-11-11
最新评论