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
Solved

Refresh/requery table after update through SQL

Posted on 2006-07-14
17
1,007 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 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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