使用二分法实现对Sbo DBDatasource数据的快速定位查询
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://foresun.blog.51cto.com/221037/75218 |
前天有朋友说,在Sbo的Matrix中定位1万多条需要很长时间,今天写了一个使用二分法进行顺序查找的函数,大大提高了定位效率,具体提高多少?(10倍以上吧,随着数据量越大,效率显现越高,完全可能达到几十倍甚至上百倍)。不过,前提是您的数据是需要按照指定的查询字段排序了的。有兴趣的朋友可以非常容易的扩展到其它控件关联的方式。
public int FindMatchData(SAPbouiCOM.DBDataSource ds, int nStartRow, int nEndRow, string strColName, string strFindValue, Boolean bFullMatch)
{ string strValue = ""; if (nStartRow >= ds.Size)
{ nStartRow = nEndRow; nEndRow = ds.Size - 1; } if (nEndRow >= ds.Size) nEndRow = ds.Size - 1;
if (nStartRow > nEndRow) return -1;
strValue = ds.GetValue(strColName, nStartRow);
if (strValue.StartsWith(strFindValue) && !bFullMatch) return nStartRow; else if (strValue == strFindValue && bFullMatch) return nStartRow; strValue = ds.GetValue(strColName, nEndRow);
if (strValue.StartsWith(strFindValue) && !bFullMatch) return nEndRow; else if (strValue == strFindValue && bFullMatch) return nEndRow; if (nStartRow == nEndRow) return -1;
int nRow = (nEndRow - nStartRow) / 2 + nStartRow;
strValue = ds.GetValue(strColName, nRow); if (strValue.StartsWith(strFindValue) && !bFullMatch) return nRow; else if (strValue == strFindValue && bFullMatch) return nRow; else { if (strValue.CompareTo(strFindValue) > 0) nEndRow = nRow; else nStartRow = nRow; return FindMatchData(ds, nStartRow + 1, nEndRow - 1, strColName, strFindValue, bFullMatch);
} } 代码在Sbo 2005B+C#.2005+SQL Server 2000上测试通过。 本文出自 “富盛软件工作室” 博客,请务必保留此出处http://foresun.blog.51cto.com/221037/75218 本文出自 51CTO.COM技术博客 |



foresun
博客统计信息
热门文章
最新评论
友情链接