我在两个窗口里做相同的数据添加的工作,而且也是对同一个表进行操作,为什么一个快,一个慢?哪个高手帮忙看看程序到底哪里有问题呢?代码如下:添加速度 较快 的代码:
procedure TFrmTotal.N13Click(Sender: TObject);
var
  sum1, sum2, sum3, sum4: integer;
  sum: double;
  s, s1, s2, s3: string;
begin
  s1 := DBGridEh3.Columns[4].DisplayText;
  s2 := DBGridEh3.Columns[3].DisplayText;  
  with DataModule2.AqryEquipmentTotal do
  begin
    Close;
    sql.clear;
    sql.Add('select * from stmgequipment where einc=' + quotedstr(s1) + 'and process=' + quotedstr(s2));
    Open;
    if RecordCount <= 0 then
    begin
      Application.MessageBox('工序名称或设备号在基础库中不存在,请与定额管理室联系后再操作!', '系统提示', 64 + 0);
      DBGridEh3.DataSource.DataSet.Cancel;
      DBGridEh3.Options := DBGridEh3.Options + [dgRowSelect] - [dgEditing];
      exit;
    end;
  end;
  sum := 0; 
  if (Aqrystmgcountdetail.State = dsEdit) or (Aqrystmgcountdetail.State = dsInsert) then
  begin
    if DBGridEh3.DataSource.DataSet.fieldbyname('stint_time_h').AsFloat = null then
      DBGridEh3.DataSource.DataSet.fieldbyname('stint_time_h').AsFloat := 0;
    if DBGridEh3.DataSource.DataSet.fieldbyname('stint_time_m').AsFloat = null then
      DBGridEh3.DataSource.DataSet.fieldbyname('stint_time_m').AsFloat := 0;
    DBGridEh3.DataSource.DataSet.post;
    s := DBEditEh1.Text;
    with Aqrystmgcountdetail do
    begin
      locate('id', s, [loPartialKey]);
    end;
  end;
  DBGridEh3.Options := DBGridEh3.Options + [dgRowSelect] - [dgEditing];
end;添加速度 较慢 的代码:
procedure TFrmFind.N2Click(Sender: TObject);
var
  sum1, sum2, sum3, sum4: integer;
  sum               : double;
  s, s1, s2, s3     : string;
  i, count          : integer;
begin
  s1 := DBGridEh2.Columns[4].DisplayText;
  s2 := DBGridEh2.Columns[3].DisplayText;
  with DataModule2.AqryEquipmentTotal do
  begin
    Close;
    sql.clear;
    sql.Add('select * from stmgequipment where einc=' + quotedstr(s1) + 'and process=' + quotedstr(s2));
    Open;
    if RecordCount <= 0 then
    begin
      Application.MessageBox('工序名称或设备号在基础库中不存在,请与定额管理室联系后再操作!', '系统提示', 64 + 0);
      DBGridEh2.DataSource.DataSet.Cancel;
      DBGridEh2.Options := DBGridEh2.Options + [dgRowSelect] - [dgEditing];
      exit;
    end;
  end;
  sum := 0;
  if (AqryDetaiL.State = dsEdit) or (AqryDetaiL.State = dsInsert) then
  begin
    if DBGridEh2.DataSource.DataSet.fieldbyname('stint_time_h').AsFloat = null then
      DBGridEh2.DataSource.DataSet.fieldbyname('stint_time_h').AsFloat := 0;
    if DBGridEh2.DataSource.DataSet.fieldbyname('stint_time_m').AsFloat = null then
      DBGridEh2.DataSource.DataSet.fieldbyname('stint_time_m').AsFloat := 0;
    DBGridEh2.DataSource.DataSet.post;
    s := DBEditEh1.Text;
    with AqryDetaiL do
    begin
      locate('id', s, [loPartialKey]);
    end;
  end;
  DBGridEh2.Options := DBGridEh2.Options + [dgRowSelect] - [dgEditing];
end;以上两段代码基本一致,为何速度相差甚远,高手帮忙吧!

解决方案 »

  1.   

    问题应该在Aqrystmgcountdetail和AqryDetaiL这里,你查看关于索引的设置部分
    如果数据量不同,那么locate的速度是有差别的。与你插入还是保存没有太大的关系(操作同一个表)
    建议:相同代码,不同变量,可以提炼出一个过程。这样便于调试和发现问题,加强工作效率。
      

  2.   

    如何查看 索引的设置部分 呢,ADOQuery有这样的属性吗
      

  3.   

    比较一下:Aqrystmgcountdetail和AqryDetaiL这两个的属性有什么不同,有没有在添加时触发的其它操作,
    在代码上看不出来.赞同SmallHand:"建议:相同代码,不同变量,可以提炼出一个过程。这样便于调试和发现问题,加强工作效率。"
      

  4.   


    又测了一下,设置了几处断点,发现慢的过程是发生在DBGridEh3.DataSource.DataSet.post;之后,而在locate的时候是很快的,问题应该是出在了哪里呢?
      

  5.   

    解决了,问题出在了adoquery的afterpost事件上,不过还是要感谢各位高手