Performance – find() vs. exist() method

Someone asked me what is the need to have an exist() method when we have a find() method already declared on a table? Logically, exist() checks if a record with a given key exists and find also nearly does the same thing, it fetches the actual record? So why the need for an exist() method? Well, the answer is performance.

The find() method always returns a buffer of the table on which it is called whereas if you look at the syntax of the exist() method, it only returns the RecId. Selecting only a single field versus the entire table takes less time.

On my test machine, calling the exist() method on CustTable with 2000 records finished in 0.0136 milliseconds whereas find() took 0.1388 milliseconds. The rule of thumb is, querying on any indexed field will always be faster than a non-indexed field. So, even if the exist() method on CustTable was selecting AccountNum or PartyId, it would return in 0.0136 m/s only.

Performance degrades only when you mix fields, so selecting (RecId, Name) will slow down to 0.1388 and that is as good as the find() method.

So the bottom line is that exist() methods exist because there are faster than find() methods when large number of records are being queried and helps improve performance.

Comments

Jan B. Kjeldsen said…
Did you consider caching?

In the case of CustTable caching is enabled and both the find and exists methods will use caching as it meets the caching criteria.

It does not explain your benchmark data (unless you only ran it once).
Your data set is may also be to small.

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

The field with ID '0' does not exist in table - Cause and resolution.