Search code examples
sql-serverperformancelazarus

improving ms sql insert performance with lazarus


i have a simple insert statement with 3 datafields. A Tag_ID which is not the primary key and not auto increment, a timestamp which saves an easy DateTime Now as a string and a float value which contains a simple devide calculation.

the sql server is actually local but later it will be on another machine not in the local network. Now i get 25,8 sec for 10.000 entries.. how can i improve this?

my code looks like this:

procedure TForm1.testMssql(Datensaetze: integer);
var
  i: integer;
  before,after,result: real;
begin
  before := GetTickCount;
  for i:= 0 to Datensaetze do
  begin
    try
          query.DataBase := conn;
          query.UsePrimaryKeyAsKey:=false;
          query.SQL.Text := 'insert into speedTest(TagID,timestamp,Value) values(:tag_id,:timestamp, :value)';
          query.Params.ParamByName('tag_id').AsInteger := i ;
          query.Params.ParamByName('timestamp').AsString := DateTimeToStr(Now);
          query.Params.ParamByName('value').AsFloat := ((i*2) / 55);
          query.ExecSQL;
          SQLTransaction1.Commit;
    except
      on E: Exception do
         ShowMessage(E.Message);
    end;
  end;
  after := GetTickCount;

  result := (after - before)/1000;
  Memo1.Text := FloatToStr(result);
end;  

Solution

  • Bulk Insert

    http://msdn.microsoft.com/de-de/library/ms188365.aspx