Solved

Refresh/requery table after update through SQL

Posted on 2006-07-14
17
1,032 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
17 Comments
 
LVL 25

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 25

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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