Fastest way to find a tag folder and return its path?

v8.1.28

Been reading thru the forum history on system.tag.browse() and not sure where we are these days performance-wise.

I have ~3,800 tags in a folder hierarchy with about 4-5 levels. At the 4th level there are lots of folders defined with unique device names, e.g. 12345678_DEVICE_NAME. I want to search for the device ID, e.g. "12345678" and return the full path to the folder containing that name.

The script below is part of a custom parameter binding on a view component in a flex repeater, so when the page loads the script may be called by 10-20 instances at once. Execution times are starting around 100ms and increasing to over 1,400ms for the final instance.

	filter = {}
	filter['name'] = "*" + equipmentID + "*"
	filter['tagType'] = "Folder"
	filter['recursive'] = "True"

	start = system.date.now()

	#search recursively for the folder with the name containing the equipment ID
	results = system.tag.browse(folder, filter).results

	logger.trace("%s ms to run system.tag.browse() for %s" %(system.date.millisBetween(system.date.now(), start), folder))

I'm concerned that as the number of flex repeater instances grows, and the size of our tag db grows, and the number of active clients grows, that this could get too slow.

Is there a better way?

Don't run the script for each instance, instead run it once when generating them and dispatch the results through view parameters.

something like

tags = system.tag.browse(root_path, filters)
instances = [{'path': tag['fullPath']} for tag in tags]

If there are more things you need to pass as parameters, you'll just need to figure out how to match the path with the appropriate instance.

1 Like

Yeah, thought about that. The parent view builds the flex repeater instances with a DB query. We'd have to run the query first to retrieve all the equipment IDs, send that list in a transform script to system.tag.browse(), then merge the results with the rest of the query to feed the flex repeater.

Also don't use this for timing operations, use Java.lang.System.nanoTime(). This will get you a precise nanosecond time and is exclusively used for timing code

E.g.

from Java.lang.System import nanoTime
start = nanoTime()
# do stuff
...
end = nanoTime()
duration = end-start # nanoseconds
1 Like

Thanks for the bonus tip. I'd seen something like that elsewhere and couldn't remember the details, so I intentionally left my timing code in there for someone like you to come along and set me straight! :wink:

1 Like