[EMAIL PROTECTED] wrote:
> Thanks for all the replays.

The word you want is "reply." Plural is "replies." "Replay" is something
completely different.

> The database I am using is Advantage Database Server. Their publicity says
> that the locate will use an underlying index if one is available, the
> field is indexed. The reason I tried locate was I do not control the
> naming of the files or the entire of the file names into the table. So I
> used locate for a case insensitive search.

But as I understand it, Locate will only find one item at a time. That
*forces* you to do a new search for every file, and *that* forces you into
an O(n^2) algorithm.

> After reading the replays I now have a thread run on loading that
> turns all the pdf names in the table to lower case. This is not visible to
> the user so the second or so it takes is not important.

But your goal when you first wrote here was to make your code run faster.
Putting it in a thread just moves the time somewhere else, hoping no one
will notice anymore.

If you want it to run faster, use a better algorithm. The one I described
is O(n), which is much better than your current O(n^2) algorithm.

Here's some pseudocode.

dbstrings := TStringList.Create;
dbstrings.AddStrings(SELECT PdfName FROM Table);
dbstrings.Sorted := True;
sl_PdfFiles.Sorted := True;
insertfiles := TStringList.Create;
deletefiles := TStringList.Create;
x := 0;
y := 0;
while (x < dbstrings.Count) and (y < sl_PdfFiles.Count) do begin
  n := AnsiCompareText(dbStrings[x], sl_PdfFiles[y]);
  if n < 0 then begin
    // DB has something the FS doesn't.
    // Mark for removal from DB and advance DB index
    deletefiles.Add(dbstrings[x]);
    Inc(x);
  end else if n > 0 then begin
    // File system has something the DB doesn't.
    // Mark for insertion and advance FS index.
    insertfiles.Add(sl_PdfFiles[y]);
    Inc(y);
  end else begin
    // Names are equal. Nothing to insert or delete.
    Inc(x);
    Inc(y);
  end;
end;
for x := x to Pred(dbstrings.Count) do
  deletefiles.Add(dbstrings[x]);
for y := y to Pred(sl_PdfFiles.Count) do
  insertfiles.Add(sl_PdfFiles[y]);
DELETE FROM Table WHERE PdfName IN [ deletefiles ];
INSERT INTO Table (insertfiles);

Something else this does is avoid updating the table while you're
searching through it. With your code, as soon as you find something
missing, you add it to the table. That's one more thing for subsequent
searches to look at even though you *know* you won't have to look for that
item again. Don't put it in the table until afterward. I suspect that all
your inserts are also forcing the database to update its index, which is
probably where your bad performance is really coming from.

Use the database table as it is, note what changes you need to make to it,
and then make all those changes at once instead of making each one as you
encounter it.

-- 
Rob


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to