Solved

Refresh/requery table after update through SQL

Posted on 2006-07-14
17
1,017 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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