Solved

Refresh/requery table after update through SQL

Posted on 2006-07-14
17
985 Views
Last Modified: 2010-04-05
Hi,

I have following code for which I need to increase performance:

begin
  // Alle relaties deselecteren
  With DM.QAlgemeenGebruik do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' UPDATE Relaties SET Relaties.GeselecteerdVoorAfspraak = False, Relaties.GefactureerdIndicator = False ');
    SQL.Add(' WHERE Relaties.GeselecteerdVoorAfspraak = True OR Relaties.GefactureerdIndicator = True; ');
    ExecSQL;
  end;
  If VarToStr(Event.ID) <> '' then
  begin
    // Alle relaties markeren die aan de afspraak zijn toegewezen.
    With DM.QAlgemeenGebruik do
    begin
      Close;
      SQL.Clear;
      SQL.Add(' UPDATE (Relaties INNER JOIN AgendaRelaties ON Relaties.Relatienr = AgendaRelaties.Relatienr) SET Relaties.GeselecteerdVoorAfspraak = True ');
      SQL.Add(' WHERE (((AgendaRelaties.[AgendaID]) = '+ QuotedStr(VarToStr(Event.GetCustomFieldValueByName('ID2Relaties'))) +')); ');
      ExecSQL;
    end;
    // Merkeer alle relaties waarvoor voor de afspraak een factuur is aangemaakt.
    With DM.QAlgemeenGebruik do
    begin
      Close;
      SQL.Clear;
      SQL.Add(' UPDATE (Relaties INNER JOIN AgendaRelaties ON Relaties.Relatienr = AgendaRelaties.Relatienr) SET Relaties.GefactureerdIndicator = True ');
      SQL.Add(' WHERE (((AgendaRelaties.[AgendaID]) = '+ QuotedStr(VarToStr(Event.GetCustomFieldValueByName('ID2Relaties'))) +')) ');
      SQL.Add(' AND (AgendaRelaties.Gefactureerd) = True; ');
      ExecSQL;
    end;
  end;
  DM.TRelatieLookup.DisableControls;
  DM.TRelatieLookup.Requery;
  DM.TRelatieLookup.EnableControls;
end;

The queries perform pretty good, about 0,015 seconds a piece.
But  DM.TRelatieLookup.Requery; takes about 2,000 seconds.
Change this line to: DM.TRelatieLookup.Refresh; doesn't have much effect.

Is there any other way to run perform the updates by SQL and refresh the table while increasing performance?

Regards,
Stef
0
Comment
Question by:Delphiwizard
  • 7
  • 4
  • 3
  • +2
17 Comments
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 300 total points
ID: 17107913
  Hi!
Try to put Application.ProcessMessages; after each ExecSQL.

And this example is from Borland Developer Network which should refresh your tables fast. Requery refreshes faster than Refresh.

procedure TFrmListOpen.TableRefresh;
var
  SavePlace : TBookmark;
begin
  {Using a standard refresh takes too long.  This code will perform a }
  {refresh much quicker.}
  SavePlace := DM.TableOpenEvents.GetBookmark;
  DM.TableOpenEvents.Requery;
  DM.TableOpenEvents.GotoBookMark(SavePlace);
  DM.TableOpenEvents.FreeBookMark(SavePlace);
end;

Regards,
  Tomas Helgi
0
 
LVL 7

Assisted Solution

by:kfoster11
kfoster11 earned 200 total points
ID: 17108006
is there a specific reason why you are using a Table object here
    DM.TRelatieLookup???

If you changed this to a query you should get an enormous speed increase because the query uses paging where the table doesn't.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17111855
Create a stored procedure inside database, something like this:

// MSSQL syntax
CREATE PROCEDURE sp_something @EventID INT, @AgendaID VARCHAR(50)
AS
  UPDATE Relaties SET Relaties.GeselecteerdVoorAfspraak = False, Relaties.GefactureerdIndicator = False
  WHERE Relaties.GeselecteerdVoorAfspraak = True OR Relaties.GefactureerdIndicator = True
  IF @EventID <> ''
    BEGIN
      UPDATE (Relaties INNER JOIN AgendaRelaties ON Relaties.Relatienr = AgendaRelaties.Relatienr) SET Relaties.GeselecteerdVoorAfspraak = True
      WHERE (((AgendaRelaties.[AgendaID]) = '+ QuotedStr(VarToStr(Event.GetCustomFieldValueByName('ID2Relaties'))) +'))
    END
  UPDATE (Relaties INNER JOIN AgendaRelaties ON Relaties.Relatienr = AgendaRelaties.Relatienr) SET Relaties.GefactureerdIndicator = True
  WHERE (((AgendaRelaties.[AgendaID]) = @AgendaID))
     AND (AgendaRelaties.Gefactureerd) = True
GO

then change your code to something like:

  With DM.QAlgemeenGebruik do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' CALL something ' + EventID + ', ' + AgendaID);
    SQL.Add(' WHERE Relaties.GeselecteerdVoorAfspraak = True OR Relaties.GefactureerdIndicator = True; ');
    ExecSQL;
  end;
0
 

Author Comment

by:Delphiwizard
ID: 17113318
Thank you for your post Ivanov G. But as stated before, the SQL's aren't the bottleneck in performance. Refreshing the table is.
0
 

Author Comment

by:Delphiwizard
ID: 17113354
Thanks for you reply TomasHelgi.
As you can see I already use Requery instead of Refresh, because I don't need to save the current recordposition at that moment.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17113888
How much records do you have in your table. What is the type of the Cursor, ctClient maybe... ?
0
 

Author Comment

by:Delphiwizard
ID: 17113894
Number of record = 70
CursorType = ctStatic
CursorLocation = clUseClient
LockType = Optimistic
TableDirect = True
ADOConnection
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17114017
What is the database? How much time does the query takes to execute in the DB's tool for queries?

How does DM.TRelatieLookup.Close; DM.TRelatieLookup.Open; execute?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Delphiwizard
ID: 17114101
I use a MS Access database.

> The queries perform pretty good, about 0,015 seconds a piece.
> But  DM.TRelatieLookup.Requery; takes about 2,000 seconds.

DM.TRelatieLookup.Close; DM.TRelatieLookup.Open;  takes about 2,000 seconds.

There a quite a few forms that use this table for lookup (lookupcomboboxes).
0
 
LVL 7

Assisted Solution

by:kfoster11
kfoster11 earned 200 total points
ID: 17114794
Try this.  If you make a copy of DM.TRelatieLookup that is not connected to the lookupcomboboxes or any other ui controls.  Is the time the same?  this will at least tell us wether it is the Table or the controls updating that is eating up the time.
0
 

Author Comment

by:Delphiwizard
ID: 17114808
Making a copy does make a lot of difference.
Not it takes only 0,6 seconds to refresh.
0
 
LVL 7

Expert Comment

by:kfoster11
ID: 17114879
All right,

try this

with DM.TRelatieLookup do begin
  DisableControls;
  try  
      Requery
  finally
    EnableControls;
  end;
end;
0
 

Author Comment

by:Delphiwizard
ID: 17115293
Doesn't make any difference compared without EnableControls.
0
 
LVL 7

Expert Comment

by:kfoster11
ID: 17115409
step through the code.  does the requery here take the time or the EnableControls?
0
 
LVL 4

Expert Comment

by:JeePeeTee
ID: 17136791
Have seen this also in an application I setup with MS Access. Do you have any tables that have setup datasource to any of the slow performing table in your App?
0
 
LVL 24

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 300 total points
ID: 17136877
   Hi!
There is one thing you should try to do.
In the Access DB create an index on the table assosiated with DM.TRelatieLookup table object if you don't
have an index. Then set the index in DM.TRelatieLookup to that index.

Having an index on a table often (always) speeds up the refresh/requery process ;)

Regards,
  Tomas Helgi
0
 

Author Comment

by:Delphiwizard
ID: 17137513
Thank you all for your help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now