0

I have a cq5 component that needs to query a given path for a couple of other component types like this:

    String query = "select * from nt:unstructured where jcr:path like '/content/some/path/%' and ( contains(sling:resourceType, 'resourceType1') or contains(sling:resourceType, 'resourceType2')) ";
    Iterator<Resource> resources = resourceResolver.findResources( query,"sql");

Unfortunately if it is working through a path with a lot of content the page times out. Is there any way to optimize a function like this or tips on improving performance?

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
Ben Newman
  • 33
  • 1
  • 3

2 Answers2

1

1. Use some more specific JCR type than nt:unstructured.

I guess you are looking for page nodes, so try cq:Page or (even better) cq:PageContent.

2. Denormalize your data.

If I understand your query correctly, it should return pages containing resource1 or resource2. Rather than using contains() predicate, which is very costly and prevents JCR from using index, mark pages containing these resources with an additional attribute. Eg., set jcr:content/containsResource1 and jcr:content/containsResource2 properties appropriate and then use them in your query:

select * from cq:PageContent where (containsResource1 is not null or containsResource2 is not null) and jcr:path like '/content/some/path/%'

You could use EventHandler or SlingPostProcessor to set the properties automatically when the resource1 or resource2 is added.

Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
1

I have added "jackrabbit" and "jcr" tags to your question - I'm not an expert in JCR queries but one of those experts might want to comment on the query statement that you are using and if and how that can be optimized.

That being said, your "page times out" statement seems to imply that it's the client browser that is timing out as it does not receive data for too long. I would first check (with a debugger or log statements) if it's really the findResources call that takes too long, or if it's code that runs after that that's the culprit.

If findResources is slow you'll need to optimize the query or redesign your code to make it asynchronous, for example have the client code first get the HTML page and then get the query results via asynchronous calls.

If code that runs after findResources causes the timeout, you might redesign it to start sending data to the browser as soon as possible, and flush the output regularly to avoid timeouts. But if you're finding lots of results that might take too long for the user anyway and a more asynchronous behavior would then be needed as well.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24