提高AdoQuery的速度

来源:互联网 发布:淘宝上怎么推广产品 编辑:程序博客网 时间:2024/04/28 23:49

用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 := 1000; // 影响不是很大
  AdoQuery1.Open;
  while not AdoQuery1.Eof do
  begin
    AdoQuery1.Next;
  end;
  AdoQuery1.EnableControls; // 恢复,与DisableControls配对调用。
  Time := GetTickCount - Time;
  Label1.Caption := IntToStr(Time);
end;

 
原创粉丝点击