通过ADO更新mysql表,2600条,用了30秒。该怎么写?
代码:
procedure TForm1.Button2Click(Sender: TObject);
 var
   I: integer;
 a,b ,c,s: string;
   wp: pYC_DEFINE;
   rfloat :string;begin
  rfloat := '%.2f';
  with ADOQuery1 do
  begin
    ADOQuery1.Open;
    for I := 0 to gyclist.Count - 1 do
    begin
     ADOQuery1.sql.clear;
     wp := gyclist.Items[I];
     a:= InttoStr(wp^.m_wsite);
     b:= wp^.m_nno;
     c :=  IntToStr(wp^.m_wsite);
     ADOQuery1.SQL.Add('update as_yc_define set DATA_VALUE='''+c+''''+' where (YC_DATA_ID='''+b+''')');
     ADOQuery1.ExecSQL; //执行的这一句太慢
     end;
   end;
   ADOQuery1.Close;
end;

解决方案 »

  1.   

    创建基于 (YC_DATA_ID) 的索引。
      

  2.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖
      

  3.   

    ado sql 执行就是这速度。
      

  4.   

    你执行2600次 update肯定不行啊
    换个方法吧想办法用存储过程来做吧
      

  5.   

    1 用adocommand来执行更新,不要用adoquery
    2 给常用字段YC_DATA_ID建索引或组合索引
    3 考虑分割表
    4 用存储过程
      

  6.   

    刚才忘说了
    for I := 0 to gyclist.Count - 1 do
     你在循环里不断更新数据库效率确实很慢,考虑写个函数或存储过程一次执行
      

  7.   


    你这要求也太笼统了
     谁知道这句 wp := gyclist.Items[I];赋的什么内容批量update不是简单一句话能搞定的,而是通过分析数据库得到的
    比如你更新的字段和数据库其他表中某些内容有关,或者存在某些规律,那么就可以考虑通过查询限定结果范围,或者自定义函数等等,然后再用update比如update 时加入级联查询,然后再给查询字段创建索引
      

  8.   

    使用事务处理;2600瞬间更新:满足要求:索引和存储过程都没有多大效果:
    测试通过的代码:
    procedure TForm1.Button4Click(Sender: TObject);
    var
      I: integer;
      a, b, c, s: string;
      wp: pYC_DEFINE;
      rfloat: string;
    begin
      ADOConnection1.Open;
      if not ADOConnection1.InTransaction then
      begin
        ADOConnection1.BeginTrans; //开始事务
        try
          with ADOCommand1 do
          begin
             Connection := ADOConnection1;
            //commandtext := 'update [country] set [population]=10000 where [name]=''Venezuela'''; //正确的SQL语句
            for I := 0 to gyclist.Count - 1 do
            begin
              wp := gyclist.Items[I];
              a := InttoStr(wp^.m_wsite);
              b := wp^.m_nno;
               //c:=Format(rfloat, [wp^.m_wsite]);
              c := IntToStr(wp^.m_wsite);          commandtext := 'update as_yc_define set DATA_VALUE=''' + c + '''' + ' where (YC_DATA_ID=''' + b + ''')';
              Execute;
            end;
          end;
          ADOConnection1.CommitTrans; //提交事务
        except
          on E: Exception do
          begin
            ADOConnection1.RollbackTrans; //如有异常,事务回滚
            ShowMessage(E.Message);
          end
        end;
      end;
    end;