↓ Skip to main content
An ink and watercolour drawing of three wooden boxes in a row on a workbench. The left box has its lid lifted a little. Beside the middle box, one wooden block sits on its open paper wrapping. The right box is empty, and its eight blocks stand in a line next to a slip with eight tally marks.

notExist, isEmpty or count: Asking Whether a Row Exists

Maximo Automation Scripts - This article is part of a series.
Part 16: This Article
Many scripts ask one question before they act: is there any row? A set gives three ways to ask, and they cost very different amounts. This article compares notExist(), isEmpty() and count(), and shows which one fits which question.

We checked how the three methods behave in Manage in the MAS 9.2 series, and we ran them on our test system. Neither IBM page names notExist() or isEmpty(). IBM’s best-practice pages only warn about count().

Three methods, three costs
#

MethodWhat it doesBuilds a record
notExist()answers from memory when the set already holds records; otherwise runs one short EXISTS query that stops at the first matchno
isEmpty()reads the first row and builds its recordyes, the first one
count()runs a count query, unless the set has already read all its rowsno

The return values point in opposite directions. notExist() and isEmpty() return true when there is no row. count() returns a number.

notExist()
#

notExist() is the cheap one. When the set holds no records in memory, it asks the database whether any row matches, and the database stops at the first match. No record is built, and no initialization logic runs.

It has one more shortcut. On a relationship whose where clause binds a value that is empty, such as the asset of a work order without an asset, it returns true without any query at all.

isEmpty()
#

isEmpty() asks for the first record of the set. To answer, Manage reads the first row and builds the record, and every initialization routine of that object runs for it. On objects with heavy initialization logic, that cost comes with every call.

If more rows match, the database statement that read the first one stays open on the set until the set is closed.

count()
#

count() runs a count query each time it is called, as long as the set has not read all its rows. After isEmpty(), for example, the set has read one row, so count() still runs its query. Once a set has read every row, count() returns that number with no query.

The number also includes new records that the script has added but not saved.

What the three methods count
#

New records count for all three methods. We ran them on our test system on a set that matched no rows, and then on the same set after one add(), with nothing saved:

SetnotExist()isEmpty()count()
where clause 1=2TrueTrue0
the same set after add()FalseFalse1

So all three answer for the set as the script sees it, saved or not.

Which one to use
#

Which method answers the question Which method answers the question
Which method answers the question
  • Any row at all: if not s.notExist():. This is the common case, and the cheapest answer.
  • The first record: call moveFirst() and test the result for None. That is the same work as isEmpty(), and you keep the record.
  • The number: call count() once and keep the value in a variable.
  • Every record: loop with moveFirst() and moveNext() until None. The loop needs no count.

count() in a loop
#

The classic mistake calls count() in every pass of a loop:

i = 0
while i < s.count():
    process(s.getMbo(i))
    i += 1

Until the set has read every row, each pass runs another count query. Count once, or loop until the set returns None:

rec = s.moveFirst()
while rec is not None:
    process(rec)
    rec = s.moveNext()

process stands for your own code.

What IBM’s pages get wrong here
#

TopicIBM’s pagesWhat Manage does
count()fires an SQL statement each timeonly while the set has not read all its rows; it also counts unsaved new records
The count() sampleslog "..." + cntadding a number to a text fails in Python with a TypeError; use str(cnt)
The cheap checknot namednotExist() answers with one short query and builds no record

Both copies of the count() samples also use typographic quotes, which Python does not accept.

Article 33 of this series collects the corrections that matter most.

What comes next
#

Article 17 covers what a script costs: how often it runs, and what it must clean up.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 16: This Article