Einzelnen Beitrag anzeigen

Monday

Registriert seit: 24. Aug 2012
103 Beiträge
 
FreePascal / Lazarus
 
#5

AW: Kleiner Geschwindigkeitstest von Insert

  Alt 18. Dez 2016, 14:36
Hey Super für eure Anregungen!


@bnreimer42
Bzgl. der Reihenfolge der Tests: Ich habe die Reihenfolge geändert, und die Ergebnisse blieben die gleichen.

@nahpets
Ja Funktion 2 funktioniert mit deinem Vorschlag tatsächlich.


@mensch72
Gibt es Vor- Nachteile von .ParamByName()...? Ich habe mir einst angewöhnt so zu schreiben und nicht mehr direkt in die Zeile, weil es einen schwerwiegenden Grund hatte. Leider weiß ich nicht mehr was der Grund dafür war.



Nun nochmal mit den neuen Ergebnissen, im Vergleich:

Code:
Benötigte Zeit % |  Funktionsname

48,57                  Test2 (Insert mit .add(Format(... siehe auch Test5 )                
17,62                  Test1 (Insert und exec... direkt hintereinander)
16,88                  Test8 (Wie Test7 + Prepare)
16,46                  Test7 (Nur ParamByName in Schleife)
0,22                   Test4 (Insert; in Transaction, außerhalb der Schleife)
0,21                   Test3 (Insert; in Transaction, innerhalb der Funktion)
0,03                   Test9 (wie 8 + Transaction)
0,01                   Test6 (Wie Test5 mit Transaction)
0,01                   Test5 (Direkt, Ohne .ParamByName.)



17,62                  Test1 (Insert und exec... direkt hintereinander)
48,57                  Test2 (Insert mit .add(Format(... siehe auch Test5 )                
0,21                   Test3 (Insert; in Transaction, innerhalb der Funktion)
0,22                   Test4 (Insert; in Transaction, außerhalb der Schleife)
0,01                   Test5 (Direkt, Ohne .ParamByName.)
0,01                   Test6 (Wie Test5 mit Transaction)
16,46                  Test7 (Nur ParamByName in Schleife)
16,88                  Test8 (Wie Test7 + Prepare)
0,03                   Test9 (wie 8 + Transaction)

Und wieder der Code:
Delphi-Quellcode:

function test1(testzahl: integer): string;
var
  a: integer;
begin
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.Text := 'Insert into daten2 (zahl) values (:zahl)';
    Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
    Form1.ZQuery1.ExecSQL;
  end;
end;



function test2(testzahl: integer): string; //nahpets
var
  a: integer;
begin

  Form1.ZQuery1.SQL.Clear;
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.add(Format('Insert into daten2 (zahl) values (%d);', [a]));
  end;
  Form1.ZQuery1.ExecSQL;

end;


function test3(testzahl: integer): string;
var
  a: integer;
begin
  Form1.ZQuery1.SQL.Text := 'begin transaction;';
  Form1.ZQuery1.ExecSQL;

  Form1.ZQuery1.SQL.Text := '';
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.Text := 'Insert into daten2 (zahl) values (:zahl);';
    Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
    Form1.ZQuery1.ExecSQL;
  end;


  Form1.ZQuery1.SQL.Text := 'Commit;';
  Form1.ZQuery1.ExecSQL;
end;




function test4(testzahl: integer): string;
var
  a: integer;
begin

  Form1.ZQuery1.SQL.Text := '';
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.Text := 'Insert into daten2 (zahl) values (:zahl);';
    Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
    Form1.ZQuery1.ExecSQL;
  end;

end;



function test5(testzahl: integer): string; //mensch72
var
  a: integer;
begin

  Form1.ZQuery1.SQL.Clear;
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.Text := 'Insert into daten2 (zahl) values (' + IntToStr(a) + ')';
  end;
  Form1.ZQuery1.ExecSQL;

end;


function test6(testzahl: integer): string;
var
  a: integer;
begin
   Form1.ZQuery1.SQL.Text := 'begin transaction;';
  Form1.ZQuery1.ExecSQL;

  Form1.ZQuery1.SQL.Clear;
  for a := 1 to testzahl do
  begin
    Form1.ZQuery1.SQL.Text := 'Insert into daten2 (zahl) values (' + IntToStr(a) + ')';
  end;
  Form1.ZQuery1.ExecSQL;

  Form1.ZQuery1.SQL.Text := 'Commit;';
  Form1.ZQuery1.ExecSQL;

end;



function test7(testzahl: integer): string;
var
  a: integer;
begin
  Form1.ZQuery1.SQL.text := 'Insert into daten2 (zahl) values (:zahl);';

  for a := 1 to testzahl do begin
      Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
      Form1.ZQuery1.ExecSQL;
  end;

end;

function test8(testzahl: integer): string;
var
  a: integer;
begin
  Form1.ZQuery1.SQL.text := 'Insert into daten2 (zahl) values (:zahl);';
  Form1.ZQuery1.Prepare;

  for a := 1 to testzahl do begin
      Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
      Form1.ZQuery1.ExecSQL;
  end;
end;



function test9(testzahl: integer): string;
var
  a: integer;
begin
  Form1.ZQuery1.SQL.Text := 'begin transaction;';
 Form1.ZQuery1.ExecSQL;

  Form1.ZQuery1.SQL.text := 'Insert into daten2 (zahl) values (:zahl);';
  Form1.ZQuery1.Prepare;

  for a := 1 to testzahl do begin
      Form1.ZQuery1.ParamByName('zahl').AsInteger := a;
      Form1.ZQuery1.ExecSQL;
  end;

  Form1.ZQuery1.SQL.Text := 'Commit;';
 Form1.ZQuery1.ExecSQL;
 
 
 
 //Starts....

  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test1(testzahl);

  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test2(testzahl);

  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test3(testzahl);



  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  Form1.ZQuery1.SQL.Text := 'begin transaction;';
  Form1.ZQuery1.ExecSQL;
  test4(testzahl);
  Form1.ZQuery1.SQL.Text := 'Commit;';
  Form1.ZQuery1.ExecSQL;


  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test5(testzahl);


  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test6(testzahl);


  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test7(testzahl);


  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test8(testzahl);


  Form1.ZQuery1.SQL.Text := 'Delete from daten2';
  Form1.ZQuery1.ExecSQL;
  test9(testzahl);
  Mit Zitat antworten Zitat