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#
| Method | What it does | Builds a record |
|---|---|---|
notExist() | answers from memory when the set already holds records; otherwise runs one short EXISTS query that stops at the first match | no |
isEmpty() | reads the first row and builds its record | yes, the first one |
count() | runs a count query, unless the set has already read all its rows | no |
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:
| Set | notExist() | isEmpty() | count() |
|---|---|---|---|
where clause 1=2 | True | True | 0 |
the same set after add() | False | False | 1 |
So all three answer for the set as the script sees it, saved or not.
Which one to use#
- 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 forNone. That is the same work asisEmpty(), and you keep the record. - The number: call
count()once and keep the value in a variable. - Every record: loop with
moveFirst()andmoveNext()untilNone. 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 += 1Until 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#
| Topic | IBM’s pages | What Manage does |
|---|---|---|
count() | fires an SQL statement each time | only while the set has not read all its rows; it also counts unsaved new records |
The count() samples | log "..." + cnt | adding a number to a text fails in Python with a TypeError; use str(cnt) |
| The cheap check | not named | notExist() 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:
- Automation scripts in the IBM Maximo Manage documentation.
- All about Maximo scripting, IBM’s scripting guide on GitHub.
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.
