开发者

Delphi KBMMemtable filter speed problem

Hi I am using KBmmemtable in a small project and come across a small speed issue i cannot seem to fix.

Basically I have a field in the table which has a boolean value, the table has a开发者_如何学编程bout 100 records in it. If I itterate though the records in the table setting the value of the field to true it does it very quickly, however if I set a filter on the table and then itterate through the filtered records it takes about 10 times longer even though there could only be 10 records to iteerate through.

Anyone got any ideas

The code I am using is

DM1.DS1.Enabled := False;
  with DM1.DS1.DataSet do begin
    First;
    while not Eof do begin
      edit;
      Fields[18].AsBoolean := TickState;
//      FieldByName('Selected').AsBoolean := TickState;
      post;
      next;
    end;
  end;
DM1.DS1.Enabled := true;

I do have an index on the field, I have also tried it without an index

thanks

colin


There is a way to use a filter on a kbmMemTable and make it work really fast...

Set kbmmem.Filtered:=true;

and dont use the Filter property, instead use the OnFilter Event...

procedure Tform1.kbmmemFilterRecord(DataSet: TDataSet;
  var Accept: Boolean);
begin
  Accept:=Fields[18].AsBoolean;
  // when you iter your table you would see only thouse rows having "true" 
  //  on the field "Selected"
end;

and yes dont forget to DisableControls before the while...

  with kbmMem do
  try
    DisableControls;
    Filtered:=true;
    First;
    while not eof do
    begin
      // do your stuff here
      Next;
    end;
  finally
    EnableControls;
  end;


This is a suggestion for the loop, it should not take any longer than with an unfiltered kbmMemTable:

with kbmMemTable do
begin
  First;
  while not EOF do
  begin
    //do something, but don't change the position of the record-pointer!
    //if you do some writing to the record, be sure to  
    // enable "AutoReposition" in your kbmMemTable
    Next;
  end;
end;

Disabling the DataSource is not such a good option. Every Component attached to the DataSource is then "empty" and must be refreshed. You get a lot of problems if you use recursion or more than one "disabling" of a DataSource. Same, when you enable the DataSet. With DisableControls you signal all the attached components, that they must not update data. With EnableControls, this condition is ended and the controls are refreshed. Another advantage is, that there is a counter incremented with every DisableControls and decremented with every EnableControls. So you can call this multiple times (for example in an recursion) and only the last call of EnableControls finally enables the controls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜