Page 1 of 1

Very slow locate from index field

Posted: 27.04.2011, 11:08
by Shagrat3
I have large table about 60,000 records. Sorted by ID

ID = AutoInc
Val = String

Program has procedure where get any list from this table about 3000 values. This operation very slow.

In ZEOS source, I see fetch from first row to end.
But I have also been sorted by ID, you can use the quick search

I have to do something like this:

Code: Select all

function GetLang(Id:LongWord):string;
var min, max, Ind, Ret:Int64;
begin
  min := 0;
  max := fDisignerMain.XTLang.RecordCount;

  // Parse table
  while true do begin
    Ind := (max + min) shr 1;
    if min >= max then break;

    fDisignerMain.XTLang.RecNo := Ind;
    ret := fDisignerMain.XTLangI.Value - Integer(Id);
    if ret < 0 then
      min := Ind + 1
    else
      max := Ind;
  end;

  // Test last step
  fDisignerMain.XTLang.RecNo := min;

  if fDisignerMain.XTLangI.Value <> Integer(Id) then
    result := '-Error-'
  else
    if fDisignerMain.XTLangV.IsNull then
      result := '-Error-'
    else
      result := fDisignerMain.XTLangV.Value;
end;
Can you append this operation in Table & Query component?

P.S.
I dont want make Query fror get this list, becouse my connection from Internet wery slow (I read data once wher start program)

Sory for my english

Posted: 13.05.2011, 07:45
by Shagrat3
This function use to quick locate from indexed table

Code: Select all

function FastLocate(Table:TDataSet; LocateFiled:string; LocateVal:Integer):boolean;
var min, max, Ind, Ret:Int64;
begin
  //=== Except id not indexed ===
  // if Table is TZ

  //=== Test in value ===
  result := Table[ LocateFiled ] = LocateVal;
  if result then exit;

  if Table.RecordCount < 7 then
    result := Table.Locate(LocateFiled, LocateVal, [])
  else begin
    //=== Fast locate ===
    min := 0;
    max := Table.RecordCount;

    // Parse table
    while true do begin
      Ind := (max + min) shr 1;
      if min >= max then break;

      Table.RecNo := Ind;
      ret := Table[ LocateFiled ] - LocateVal;
      if ret < 0 then
        min := Ind + 1
      else
        max := Ind;
    end;

    // Test last step
    Table.RecNo := min;

    result := Table[ LocateFiled ] = LocateVal;
  end;
end;
Example

Code: Select all

FastLocate(Table1, 'id', 9763);
May program began to work faster

I will try to make the patch to the component TZTable