Optimizing Retrieving of Nodes from Iterator after Query
Hi Everyone,
We are in a situation where we have to perform up to a 1000 queries per request looking for asset nodes. The queries we use are similar to this one:
SELECT * FROM [dam:Asset] As s WHERE ISDESCENDANTNODE ([/content/dam/msi-dam]) AND s.[jcr:content/metadata/cq:productReference] IN ("/etc/commerce/products/msi/smo/smot-wqsav-hon10mm")
Using an index the query execution seems to be quick (1-2ms). Here's what the query performance monitor shows for these queries:
Query execution time: 2 ms
Get nodes time: 992 ms
Result node count time: 2421 ms
Number of nodes in result: 2
When the time comes to work with the nodes returned as result the things get slow. Here's an example of our code:
QueryResult result = query.execute();
@SuppressWarnings("unchecked")
Iterator<Node> iterator = result.getNodes();
while (iterator.hasNext()) {
Node assetNode = iterator.next();
String path = assetNode.getPath();
if (!assetsUrls.contains(path)) {
assetsUrls.add(path);
}
}
The two lines of code is what slows down the whole process to a little more that 1s for each iteration of this code. This eventually will result in spending around 1200s for the whole process when we need to have 1000 queries. We understand that there are some limitations when accessing the repository this way but are really trying to find a way to optimize the process. In another functionality we build, we were able have one long query for all the 1000 things we are searching for but we cannot implement it here unless there's a way to retrieve the number of resulting nodes without getting the nodes themselves. This might be very helpful, as well.
My questions here would be, is there a way to optimize this whole process and also is there a way to retrieve the number of resulting nodes without accessing them?
We are using AEM 6.3.2.1
Thank you very much for your help in advance,
Bobby