提高AdoQuery的速度

时间:2023-03-08 17:23:59

用TDataSet及其派生类如TAdoQuery对数据库进行查找时,

如果TDataSet类 没有与数据感知控件相连,通过调用DisableControls可以极大地提高查询速度

特别是在数据比较多的情况下。

下面一段代码查询一个45000条记录的表,

不调用DisableControls时需要 执行30到40秒,

调用DisableControls后只需要1秒到2秒。

procedure TForm1.Button2Click(Sender: TObject);
var Time : DWORD;
begin
Time := GetTickCount;
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('select * from table');
AdoQuery1.DisableControls;
AdoQuery1.CacheSize := ; // 影响不是很大
AdoQuery1.Open;
while not AdoQuery1.Eof do
begin
AdoQuery1.Next;
end;
AdoQuery1.EnableControls; // 恢复,与DisableControls配对调用。
Time := GetTickCount - Time;
Label1.Caption := IntToStr(Time);
end;