第一个时间为第二种方法的执行时间,第二个时间为第三种方法执行时间。每种方法循环了10次以确保数据准确性
关于测试程序代码在之前的文章中有提到,改一下SQL语句就可以使用了
数据结果显示第三种方法要优秀不少。至于为什么第三种方法快,我心里已经有了个大致的想法,不过因为太难表述了,就懒得浪费口水说了,大家记住结论就好了
接下来介绍第四种方法:在SQL中加载程序集,在查询时调用程序集
加载程序集的方法有些难以表述,感兴趣的朋友可以自己去查找相关资料。在此我贴出主要代码:
View Code
/// <summary> /// The variable that holds the intermediate result of the concatenation /// </summary> private StringBuilder intermediateResult;
/// <summary> /// Initialize the internal data structures /// </summary> public void Init() { this.intermediateResult = new StringBuilder(); }
/// <summary> /// Accumulate the next value, not if the value is null /// </summary> /// <param name="value"></param> public void Accumulate(SqlString value) { if (value.IsNull) { return; }
this.intermediateResult.Append(value.Value).Append(','); }
/// <summary> /// Merge the partially computed aggregate with this aggregate. /// </summary> /// <param name="other"></param> public void Merge(Concatenate other) { this.intermediateResult.Append(other.intermediateResult); }
/// <summary> /// Called at the end of aggregation, to return the results of the aggregation. /// </summary> /// <returns></returns> public SqlString Terminate() { string output = string.Empty; //delete the trailing comma, if any if (this.intermediateResult != null && this.intermediateResult.Length > 0) { output = this.intermediateResult.ToString(0, this.intermediateResult.Length - 1); }
return new SqlString(output); }
public void Read(BinaryReader r) { intermediateResult = new StringBuilder(r.ReadString()); }
public void Write(BinaryWriter w) { w.Write(this.intermediateResult.ToString()); } |
这个方法比第三种方法快得不多,大概只有5%到10%的性能提升,但是这种方法十分优雅,我窃以为这种方法是解决一对多关系转换一对一方法中最好的方法
PS:最近太懒了,都没有来写东西。罪过罪过
再PS:想吐槽一下,最近园子里几个小妹子写的生活上的杂七杂八的东西居然引起了那么多人的追捧,而真正的技术贴却是无人问津,不得不说是一种悲哀
再再PS:欢迎留言讨论,欢迎转载。不足之处望海涵