通过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;
代码:
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;
解决方案 »
- 请教:如何定位一条记录,然后以它为第一条记录,循环把余下记录的某字段的值取出来
- 升星,兼恭贺Delphi2009发布,散分
- 高分求助,在线急等,有关自动生成控件的响应函数的编写(不够再加)
- 我用windows.beep(100,200)为什么编译器告诉我[Error] Undeclared identifier: 'windows'
- 十万火急的数据库连接问题!!!HELP
- 关于控件的使用
- ●●轻松取分,别犹豫了,小问题,进来吧●●
- 关于TRY语句的问题
- delphi中有没有想c语言中的memset语句
- 8Bit位图如何转换为8bit的JPEG
- delphi+sql+combobox 想做模糊查询,效果不好,请高人指正。谢谢。
- 对字符串length的问题
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#结帖
换个方法吧想办法用存储过程来做吧
2 给常用字段YC_DATA_ID建索引或组合索引
3 考虑分割表
4 用存储过程
for I := 0 to gyclist.Count - 1 do
你在循环里不断更新数据库效率确实很慢,考虑写个函数或存储过程一次执行
你这要求也太笼统了
谁知道这句 wp := gyclist.Items[I];赋的什么内容批量update不是简单一句话能搞定的,而是通过分析数据库得到的
比如你更新的字段和数据库其他表中某些内容有关,或者存在某些规律,那么就可以考虑通过查询限定结果范围,或者自定义函数等等,然后再用update比如update 时加入级联查询,然后再给查询字段创建索引
测试通过的代码:
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;