我想请求建议和一些帮助,以使我的代码更有效。我正在处理大量的表格和数据(约100万条记录) 现在我使用Firedac查询和它的简单帖子。我的代码运行正常,但运行从一开始就需要很长时间。 (两个多小时) 我必须按字段更新参数。数据库是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创建一个块并一起插入,而不是逐个插入
我的其他代码看起来像..
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-s之后我想执行它们
有人可以为我提供更好的解决方案或建议吗?谢谢你的答案!
答案 0 :(得分:1)
你的循环是错误的方式:
while not qry_sample eof do
begin
qry_sample.edit;
for C := 0 to qry_sample.fields.count -1 do
begin
end;
qry_sample.post;
end;
qry_sample.next;
你循环遍历查询的每一行,然后循环遍历每个字段。
但是查询中的字段元数据每行都保持不变
像这样拉开两个环:
type
TFieldData = record
//Whatever data you want to collect about the fields
end;
var
Fields: array of TFieldData;
begin
SetLength(Fields, qry_sample.Field.Count);
for c:= 0 to qry_sample.Fields.Count -1 do begin
//Store the relevant data in the fields array
Fields[c].Fieldname:= qry_sample.Fields[i].Fieldname;
end; {for fields}
while not(qry_sample.eof) do begin
//Do stuff
end; {while}
qry_sample.post;
通过拉取行元数据的集合以及从循环中发布更新,你应该加速很多事情。
仍然不够快