Question
· Aug 14

Iterating ^$GLOBAL is very slow

I use the following code to loop trough all globals : 

set name=""
for
{
   set name=$order(^$GLOBAL(name))
   quit:name=""
   write name,!
}

However this is really slow : about 1second per node. 

I ran debugger, and it seems most of the time is spend in "%SYS.GD Visible" function. There is a comment that says : 
"Sees if globals in dataset are visible to our namespace"
This function loop on a global named  ^mtemp which contains lot of records (10K or more).
I have cleared global, hoping it has been filled by another process but it's not (it seems to be filled by %SYS.GD)

Why is it so slow, Is there a way to make this faster ?

Product version: IRIS 2021.2
$ZV: IRIS for Windows (x86-64) 2021.1 (Build 215U) Wed Jun 9 2021 09:39:22 EDT
Discussion (5)3
Log in or sign up to continue

^$GLOBAL likely does not cache the list of the globals in the current namespace

So each time you do $Order(^$GLOBAL(name)) it gets the list and looks which global is next after the one in the name. That's fine because globals might appear and disappear between the calls.

If you'd like to loop through the globals -- use merge, and then loop through the temp variable a

merge a = ^$GLOBAL("")

Note, that your loop is much faster if you run it in the database, not namespace, as here ther are no possible mappings to account for

USER>s z1=$zh,n="" f  { s n = $O(^$Global(n))  q:n="" } w $zh-z1
3.302887
USER>zn "^^..\user"

...iris\mgr\user\>s z1=$zh,n="" f  { s n = $O(^$Global(n))  q:n="" } w $zh-z1
.098734