Tclientdataset 作为内存表时的奇怪问题!!!(100分)

  • 主题发起人 主题发起人 lfq
  • 开始时间 开始时间
L

lfq

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序中使用Tclientdataset作为一个临时内存表存储数据,但需要对其中的数据作不断的更新,结果发现随着更新次数的增加,更新速度越来越慢,非常奇怪,我找不到原因,请诸位看看。<br>下面是我写的一个测试程序及测试结果,该程序在delphi7 和 delphi2006 下表现一致。<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs,DBClient, StdCtrls, DB ;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;memoryDataSet: TClientDataSet;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var i:integer;b:tdatetime;<br>begin<br> with MeMoryDataset do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;//创建表结构<br> &nbsp; &nbsp; &nbsp;with FieldDefs.AddFieldDef do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataType:=ftString;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Size:=20;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Name:='S1';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with FieldDefs.AddFieldDef do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataType:=ftFloat;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Name:='f1';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with FieldDefs.AddFieldDef do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataType:=ftinteger;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Name:='i1';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;createdataset;<br> &nbsp; &nbsp; &nbsp;//打开表,并增加100个记录。<br> &nbsp; &nbsp; &nbsp;memorydataset.Open;<br> &nbsp; &nbsp; &nbsp;for I := 1 to 100 do<br> &nbsp; &nbsp; &nbsp; &nbsp;memorydataset.InsertRecord(['NAME'+inttostr(i),i*0.1,i]);<br> &nbsp; &nbsp; &nbsp;//重复变更所有记录中某一字段的值,并将完成一次的时间显示在memo中<br> &nbsp; &nbsp; &nbsp;for i:=0 to 100 do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b:=now;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with memorydataset do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;first;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while not eof &nbsp;do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;edit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fieldbyname('i1').AsInteger:=i;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;post;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.Lines.add(formatdatetime('hh:nn:SS.zzz',(now-b)));<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;end;<br>end;<br><br>end.<br><br>运行结果如下:<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.015<br>00:00:00.000<br>00:00:00.000<br>00:00:00.016<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.015<br>00:00:00.016<br>00:00:00.000<br>00:00:00.016<br>00:00:00.015<br>00:00:00.016<br>00:00:00.031<br>00:00:00.016<br>00:00:00.031<br>00:00:00.016<br>00:00:00.031<br>00:00:00.031<br>00:00:00.016<br>00:00:00.032<br>00:00:00.031<br>00:00:00.031<br>00:00:00.031<br>00:00:00.031<br>00:00:00.047<br>00:00:00.047<br>00:00:00.047<br>00:00:00.047<br>00:00:00.062<br>00:00:00.047<br>00:00:00.063<br>00:00:00.062<br>00:00:00.063<br>00:00:00.062<br>00:00:00.063<br>00:00:00.078<br>00:00:00.062<br>00:00:00.078<br>00:00:00.078<br>00:00:00.094<br>00:00:00.094<br>00:00:00.093<br>00:00:00.094<br>00:00:00.094<br>00:00:00.094<br>00:00:00.094<br>00:00:00.109<br>00:00:00.157<br>00:00:00.109<br>00:00:00.172<br>00:00:00.125<br>00:00:00.125<br>00:00:00.140<br>00:00:00.141<br>00:00:00.141<br>00:00:00.156<br>00:00:00.141<br>00:00:00.171<br>00:00:00.172<br>00:00:00.157<br>00:00:00.171<br>00:00:00.188<br>00:00:00.187<br>00:00:00.187<br>00:00:00.187<br>00:00:00.250<br>00:00:00.203<br>00:00:00.204<br>00:00:00.234<br>00:00:00.234<br>00:00:00.235<br>00:00:00.234<br>00:00:00.250<br>00:00:00.250<br>00:00:00.250<br>00:00:00.282<br>00:00:00.296<br>00:00:00.282<br>00:00:00.343<br>00:00:00.313<br>00:00:00.312<br>00:00:00.329<br>00:00:00.312<br>00:00:00.313<br>00:00:00.344<br>00:00:00.375<br>00:00:00.359<br>00:00:00.344<br>00:00:00.328<br>00:00:00.359<br>00:00:00.360<br>00:00:00.406<br>00:00:00.406<br>00:00:00.391<br>可以看出,更新速度稳定变慢,原因何在?
 
当然了,每次要申请内存空间,还要复制数据肯定会慢的啦
 
如果一直慢下去,程序就无法运行了,请问有解决办法吗?
 
基本上无法解决,因为ClientDataset本来就不是为大数据量设计的<br>TADODataSet也可以实现内存表,你可以试试
 
我找到了解决办法,就是把changelog关闭。
 
就是关闭了changelog,如果你还是一直的更新,<br>他还是会慢的,<br>如果你真的需要这么做,建议你建立一个实际的表,<br>专门用来做临时表用,但是不删除,退出程序时记得truncate就OK了,
 
我关闭了changelog后作了测试,没有发现变慢的现象。
 
把changelog关闭会不会有负作用?比如说数据恢复,回退什么的?
 

Similar threads

I
回复
0
查看
763
import
I
I
回复
0
查看
547
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部