SQL ServerSQLSERVER2008中CTE的Split与CLR的性于

    
之前已有相同首POST凡有关用CTE实现Split,这种措施就较传统的方迅速了。今天咱们虽这个措施和CLR实现的Split做比较。在CLR兑现Split函数的确非常粗略,dotnet
framework本身就发出之function了。
    我们新建一个DataBase
project,然后起一个UserDefinedFunctions,Code像这样:

   1:      /// <summary>

   2:      /// SQLs the array.

   3:      /// </summary>

   4:      /// <param name="str">The STR.</param>

   5:      /// <param name="delimiter">The delimiter.</param>

   6:      /// <returns></returns>

   7:      /// 1/8/2010  2:41 PM   author: v-pliu

   8:      [SqlFunction(Name = "CLR_Split",

   9:      FillRowMethodName = "FillRow",

  10:      TableDefinition = "id nvarchar(10)")]

  11:   

  12:      public static IEnumerable SqlArray(SqlString str, SqlChars delimiter)

  13:      {

  14:          if (delimiter.Length == 0)

  15:              return new string[1] { str.Value };

  16:          return str.Value.Split(delimiter[0]);

  17:      }

  18:   

  19:      /// <summary>

  20:      /// Fills the row.

  21:      /// </summary>

  22:      /// <param name="row">The row.</param>

  23:      /// <param name="str">The STR.</param>

  24:      /// 1/8/2010  2:41 PM   author: v-pliu

  25:      public static void FillRow(object row, out SqlString str)

  26:      {

  27:          str = new SqlString((string)row);

  28:      }

下一场Bulid,Deploy一切OK继,在SSMS中施行以下测试T-sql:

   1:  DECLARE @array VARCHAR(max)

   2:  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'

   3:  SELECT id FROM dbo.CLR_Split(@array,',')

 

咱们来拘禁她的Client Statistic:

SQL Server 1

进而我们实践测试T-sql使用同样的array:

   1:  DECLARE @array VARCHAR(max)

   2:  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'

   3:  SELECT item FROM strToTable(@array,',')

CTE实现的Split function的Client statistic:

SQL Server 2

经过对比,你得窥见CLR的performance略高于CTE方式,原因在于CLR方式有Cache功能,并且将一个犬牙交错的演算放到程序里比DataBase里更加迅速。

 

乃还得参见:

Split string in SQL Server 2005+ CLR vs.
T-SQL

 

Author:Petter Liu    http://wintersun.cnblogs.com

盼就篇POST对君有赞助。

相关文章