FindFirst vs. Count=0

njhansen
Member Posts: 37
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"?
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"?
0
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 = 00 -
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"0 -
OK, thanks. I know I'd seen IsEmpty, but had forgotten about its existence. That makes more sense.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions