我们在ETL过程中使用CLR函数来集中特定的数据转换和数据检查逻辑.这些功能是相当基本的,不需要数据访问,并且具有允许并行性的确定性.
例如:
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, SystemDataAccess = SystemDataAccessKind.None, IsPrecise = true)]
public static bool check_smallint(string input)
{
string teststring;
try
{
teststring = input.Trim(' ').Replace('-', '0');
if (teststring.Length == 0)
{
teststring = "0";
}
Convert.ToInt16(teststring);
}
catch (NullReferenceException)
{
return true;
}
catch (FormatException)
{
return false;
}
catch (OverflowException)
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
除性能外,这种方法很好.查询的速度已大大减慢,这会导致处理大型数据集(数百万行甚至更多)时出现问题.
到目前为止,我们没有找到真正了解SQL CLR架构的人,但我们收到的一个建议是,它可能是由为每个函数调用创建新连接或分配内存的开销引起的.所以解决方案可能是连接/内存池.
请不要提出不同的解决方案,我们已经在考虑它们,比如内联sql,或者完全不同的方法.在许多情况下,标准的sql函数没有选项,因为没有错误提升.
PS.我们正在使用SQL 2008R2.