FindFirst vs. Count=0

Just a curiosity about which I can't find any discussion.

Any thoughts on which of the following two it is "generally" superior, if checking to see whether any records match a filter.

record.filter;
if not record.findfirst then
<do something>


record.filter;
if record.count > 0 then
<do something>



I would assume that for reasonably well-refined filters on tables with lots of fields that doing the count is faster than retrieving a record (less data being transferred), but I could imagine that for poorly defined filters, it might take more work to count all the records than to just retrieve the first hit (longer query on SQL Server).

Any "rules of thumb"?

Answers

  • Depends on what you need. If you need any field values, and you only need one record, you use FINDFIRST/FINDLAST. If you don't need any values, but you just need to know whether there are records in the filter, you use ISEMPTY.
    Rec.SETFILTER(field,value);
    IF Rec.ISEMPTY THEN
      DoSomething;
    
    Or, conversely:
    Rec.SETFILTER(field,value);
    IF NOT Rec.ISEMPTY THEN
      DoSomething;
    
    You would never use COUNT = 0
  • Reason why COUNT is especially bad in this situation... is let's say instead of zero you found 1,000,000. It has to count every single record.
    Rule of thumb:
    "If you do NOT really care how many then do NOT use COUNT"
    "If you only care if there are any then use ISEMPTY"
    "If you only care if there are any AND you want to do something with the first record, then use FINDFIRST"
  • OK, thanks. I know I'd seen IsEmpty, but had forgotten about its existence. That makes more sense.