消除完成速度非常慢的 Do.Until 循环



我在这里需要重要的帮助。 我有一个已经存在了 7 年多的 VB6 应用程序,我有几个客户使用它作为他们的会计解决方案。现在我的问题是,一个特定的客户端已经变得如此之大,数据库大小为 15GB,账本数据库表超过 400 万行,客户大小超过 100,000,等等。

我在那个应用程序中有一个代码,在检查一些参数后,客户会被按统一费率借记,对于数据库较小或平均的客户,它工作正常,但对于这个特定的客户,此代码有时甚至无法在 6 小时内完成,它成为一个严重的头痛,我需要帮助!

代码看起来像这样...

Select * from table Customers order by account number
do until rst.eof
select * from table purchase where account number = rst!account number
if found 
A1 = "YES"
else
A1 = "NO"
A2 = new charges on form / 100
A3 = rst!Balance + A2
select count(*) from table receipts where account number = rst!account number and month = currentmonth and year = current year
if count > 10 then
goto VBSTOP
else
CONTINUE
select * from table seller where account number = rst!account number
if found 
A4 = "YES"
else
A4 = "NO"
insert into table ledger (Account number, Account name, A1, A3, A4, Charges)
insert into table Charges (Account number, Account name, Charges)
insert into table Receipts (Account number, Account name, Charges, Receipt Number)
update table purchase (update purchaseno = purchaseno + 1 where account number = rst!account number
rst.MoveNext
loop

.......

对于具有非常大的数据库的客户来说,这段代码需要 6 个多小时才能完成,我真诚地寻求帮助来解决我的 DO....直到循环头痛。有没有办法优化此代码并消除 do till 语句或使其运行得更快?

我会感谢我心底里的每一个帮助。

几个建议:

  • 不要选择"选择 *",而是仅选择所需的列。这将减小记录集的大小。
  • 确保 where 子句中使用的所有列都已编制索引 (帐户,月,年)。
  • 将 3 个插入和更新转换为单个存储过程 叫。
  • 在检查是否存在记录的情况下,请尝试 "选择 TOP 1",然后选择主键的列名。
  • 在调试中运行它以查看哪个步骤花费的时间最长。

我没有适合您的代码。我本来会发表评论,但我还没有获得这种特权。

最新更新