Use IsEmpty method instead of Count
Count() returns the total number of records matching the current filters, which translates to a SELECT COUNT(*) query against the database. When the only question is whether any records exist, that full count is wasted work — on a table like "G/L Entry" with millions of rows, the query scans far more data than necessary.
IsEmpty() answers the same question by fetching at most one row.
When you are using SQL Server, this method is faster than using the Count Method (Record) and then testing the result for zero.
— RecordRef.IsEmpty() Method on Microsoft Learn
Replace any Count() comparison against zero with IsEmpty().
Example
Checking for no records
procedure IsCustomerTableEmpty(): Boolean
var
Customer: Record Customer;
begin
if Customer.Count() = 0 then // Use IsEmpty method instead of Count [LC0081]
exit(true);
end;Use IsEmpty() directly:
procedure IsCustomerTableEmpty(): Boolean
var
Customer: Record Customer;
begin
if Customer.IsEmpty() then
exit(true);
end;Checking for any records
procedure HasCustomers(): Boolean
var
Customer: Record Customer;
begin
if Customer.Count() > 0 then // Use IsEmpty method instead of Count [LC0081]
exit(true);
end;Use not IsEmpty():
procedure HasCustomers(): Boolean
var
Customer: Record Customer;
begin
if not Customer.IsEmpty() then
exit(true);
end;Exception
The diagnostic is not raised for temporary records. Count() on a temporary record operates on an in-memory dataset, not the database, so the performance concern does not apply.
See also
- LC0082 - Use Query or Find with Next instead of Count
- Record.IsEmpty() Method on Microsoft Learn
- Are there any records there? by Vjeko