使用二分法实现对Sbo DBDatasource数据的快速定位查询

为解决Sbo Matrix中大量数据查找效率低的问题,本文介绍了一种利用二分法进行顺序查找的方法,该方法能显著提升查找速度,尤其是在数据量较大时效果更佳。此方法的前提是数据需按查询字段排序。
前天有朋友说,在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上测试通过。

本文转自foresun  51CTO博客,原文链接:http://blog.51cto.com/foresun/75218,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值