Best way to process MboSet

Today we are talking about how, in code (Java and Autoscript), the best way to process a MboSet. There are lots of ways to process them but some are better than others. (All code is shown in Java but can easily be converted to Jython.)

Let’s look at this sample code. This is a pretty standard/common way to process a MboSet:

MboSetRemote mboSet = getMboSet("ASSET");
   for (int i=0; i< mboSet.count(); i++)
   {
      MboRemote curMbo = mboSet.getMbo(i);
      ...
   }

Now, this code will happily process the Asset table but it turns out this code is the slowest way to do it. The reason is the use of mboSet.count(). When you execute this, Maximo basically runs the SQL statement: SELECT COUNT(*) FROM ASSET. This happens every time the line of code is executed. So if there are 100,000 records in the ASSET table, then this code runs SELECT COUNT(*) FROM ASSET 100,000 times. If you have 500,000 records in ASSET then, it would have to be executed 500,000 times. You can see where this is not the fasted approach. A better approach would be to use a variable and execute the mboSet.count() just once. That could look like this:

MboSetRemote mboSet = getMboSet("ASSET");
   int mboSetCount = mboSet.count();
   for (int i=0; i< mboSetCount; i++)
   {
  	MboRemote curMbo = mboSet.getMbo(i);
  	...
   }

This is much better as no matter how many records there are in the ASSET table, we only execute the mboSet.count() once. However, it turns out that we don’t even need the mboSet.count() (and its SELECT COUNT(*) FROM ASSET). If your goal is it just process the records in the MboSet, then this is the best approach:

MboSetRemote mboSet = getMboSet("ASSET");
   for (MboRemote curMbo = mboSet.moveFirst(); curMbo != null; curMbo=mboSet.moveNext())
   {
   	...
   }

Here we have not only eliminated the MboSet.count(), but also the extra variables we used to process the MboSet.

Here is what the best code looks like in Jython:

mboSet = mbo.getMboSet("ASSET")
curMbo = mboSet.moveFirst()
while curMbo:
    ...
    curMbo = mboSet.moveNext()

Cheers and happy coding.