![]() |
Datenbank: Firebird • Version: 2.5 • Zugriff über: IBDAC
SQL "Update or Insert" langsam
Hallo,
ich führe ca. 2000 "Update or Insert"-Statements mit einem TIBCQuery aus. Das dauert ca. 2,5 Sekunden und ist zu langsam. Ist das normal, dass das so lange dauert oder kann man das beschleuningen? (Zum Vergleich: Wenn ich die selben Daten ohne Datenbank in eine Ini-Datei schreibe ist das in 0,5 Sekunden erledigt.)
Code:
Transaction.StartTransaction;
for i := 0 to 2000 do begin Query.SQL.Text := 'Update or insert into foo (A, B, C) values (:a, :b, :c)'; Query.ParamByName('a').AsString := 'v1'; Query.ParamByName('b').AsString := 'v2'; Query.ParamByName('c').AsString := 'v3'; Query.Execute; end; Transaction.Commit; |
AW: SQL "Update or Insert" langsam
Wieso setzt Du innerhalb der Schleife jedesmal die SQL-Eigenschaft neu? Und fehlt da nicht ein MATCHING in Deinem Statement?
|
AW: SQL "Update or Insert" langsam
Zitat:
Siehe ![]() |
AW: SQL "Update or Insert" langsam
Zitat:
|
AW: SQL "Update or Insert" langsam
Durch die dauernde Zuweisung des Statemants wird die Komponente jedes mal ein Unprepare schicken.
Dadurch geht der Vorteil der Parameter natürlich flöten... Frank |
AW: SQL "Update or Insert" langsam
Ggf im SQL das Matching oder andere "Hilfsmittel" ergänzen. Auch wenn es dann doppeltgemoppelt ist, könnte es dem Optimizer eine Hilfestellung sein.
Ohne weiteres wirkt ja ein Update erstmal auf den gesamten Datenbestand, ist also eine fette Nummer. Wirklich flott ist wahrscheinlich eine komplette Umstellung auf: Step1: Insert into <table> (<fields>) select <values> from table where not exists .. (bzw. in richtig mit self join und is null Prüfung) Step2: Updates mit ähnlichem Vorgehen. Wäre die Frage, ob es ständig geschieht und nervt oder die bequeme Formulierung oben eine einmalige Sache ist und mit ein paar Tränchen verdrücken zu bewältigen ist. |
AW: SQL "Update or Insert" langsam
Zitat:
Delphi-Quellcode:
Transaction.StartTransaction;
Query.SQL.Text := 'Update or insert into foo (A, B, C) values (:a, :b, :c)'; for i := 0 to 2000 do begin Query.ParamByName('a').AsString := 'v1'; Query.ParamByName('b').AsString := 'v2'; Query.ParamByName('c').AsString := 'v3'; Query.Execute; end; Transaction.Commit; |
AW: SQL "Update or Insert" langsam
Zitat:
Delphi-Quellcode:
Erspart auch noch dreimal pro Runde das Auswerten von ParamByName.
Transaction.StartTransaction;
Query.SQL.Text := 'Update or insert into foo (A, B, C) values (:a, :b, :c)'; ParamA = Query.ParamByName('a'); ParamB = Query.ParamByName('b'); ParamC = Query.ParamByName('c'); for i := 0 to 2000 do begin ParamA.AsString := 'v1'; ParamB.AsString := 'v2'; ParamC.AsString := 'v3'; Query.Execute; end; Transaction.Commit; |
AW: SQL "Update or Insert" langsam
Delphi-Quellcode:
geht es nicht auch so?
Transaction.StartTransaction;
Query.SQL.Text := 'Update or insert into foo (A, B, C) values (:a, :b, :c)'; Query.Params[0].DataType := ftString; Query.Params[1].DataType := ftString; Query.Params[2].DataType := ftString; for i := 0 to 2000 do begin Query.Params[0].Value := 'v1'; Query.Params[1].Value := 'v2'; Query.Params[2].Value := 'v3'; Query.Execute; end; Transaction.Commit; Erspart das Auswerten & Überprüfen des Typs sowie das Suchen der Parameter Position. |
AW: SQL "Update or Insert" langsam
Hallo,
bei IBDAC muss man explizit preparen, um Parameter ausnutzen zu können
Delphi-Quellcode:
Und dem Link von SProske zufolge spielt auch der Primary Key eine Rolle,
Transaction.StartTransaction;
Query.SQL.Text := 'Update or insert into foo (A, B, C) values (:a, :b, :c)'; // das fehlte Query.Prepare; for i := 0 to 2000 do begin Query.ParamByName('a').AsString := 'v1'; Query.ParamByName('b').AsString := 'v2'; Query.ParamByName('c').AsString := 'v3'; Query.Execute; end; Transaction.Commit; wie sieht denn die Tabellenstruktur aus? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz