【发布时间】:2016-04-28 17:53:16
【问题描述】:
我想寻求建议和帮助,以使我的代码更有效。我正在处理大量表格和数据(大约 100 万条记录) 现在我使用 Firedac Query 和它的简单帖子。我的代码运行良好,但从一开始就运行很长时间。 (两个多小时) 我必须逐个字段更新参数。数据库是 firebird 2.5。
我的代码看起来像...
while not qry_sample eof do
begin
qry_sample.edit;
for C := 0 to qry_sample.fields.count -1 do
begin
//Here I do the parameter changes for every qry_sample.fields[C].value
//for example I add new value for all the primary keys and foreign keys
//I got the new ID-s from dictionaries
end;
qry_sample.post;
end;
qry_sample.next;
但这很慢......
我想把所有的表SQL-s做成一个block一起插入,不是一个一个
我的其他代码看起来像..
for C := 0 to qry_SQL.Fields.Count -1 do
begin
if not vFields.IsEmpty then
vFields := vFields + ',';
vFields := vFields + qry_SQL.Fields[C].FieldName;
end;
while not qry_SQL.Eof do
begin
vValues := '';
SQL := '';
for C := 0 to qry_SQL.Fields.Count -1 do
begin
vValues := vValues + ',';
vValues := vValues + chr(39)+ vartostr(qry_SQL.Fields[C].Value) +chr(39);
end;
qry_SQL.Next;
SQL := SQL + #13#10 + 'insert into ' + vSourceTableName + '(' + vFields + ') ' +
'values ('+ vValues + ');';
end;
变量是变体。在我收集了所有想要执行的 SQL 之后
有人可以为此提供更好的解决方案或建议吗?感谢您的回答!
【问题讨论】:
-
将值插入到没有索引的临时表中。完成后,将临时表中的
insert select插入到真实表中,请参阅:stackoverflow.com/questions/21115720/insert-select-in-firebird -
你在使用事务吗?即
FDConnection1.StartTransaction和FDConnection1.Commit。 -
是的,在表格发布后提交。