# Groovy script performance for StorageTx \`findAssets\` in Maven repo

**URL:** <https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363>\
**Category:** Sonatype Nexus Repository\
**Tags:** groovy, maven\
**Created:** [September 26, 2022, 3:06am UTC](https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363 "2022-09-26T03:06:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbender](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rbender](https://community.sonatype.com/u/rbender)\
**Post date:** [September 26, 2022, 3:06am UTC](https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363/1 "2022-09-26T03:06:33Z")

</div>

We are using a custom Groovy script to retrieve and filter assets from a Maven repository. We are trying to optimize the performance for the script and noticed the storage query’s execution is the culprit, regularly taking 1.5-2+ seconds to complete.

```nohighlight
def repo = repository.repositoryManager.get("my-repo-name")
StorageFacet storageFacet = repo.facet(StorageFacet)
StorageTx tx = storageFacet.txSupplier().get()

try {
    tx.begin()

    Query.Builder query = Query.builder()
        .where("component.group").eq(request.groupId)
        .and("component.name").eq(request.artifactId)
        .and("blob_created > ").param(request.startDate)

    log.info("before query")
    Iterable<Asset> results = tx.findAssets(query.build(), [repo])
    log.info("after query")
} finally {
    tx.close()
}

```

We are wondering if performance is related to our usage of `component.group` and `component.name`; are these non-indexed fields?

Our custom script is primarily so we can retrieve only “recent” artifacts through a client-provided `startDate` value (we didn’t see a similar way to accomplish with the [Search API](https://help.sonatype.com/repomanager3/integrations/rest-and-integration-api/search-api#SearchAPI-SearchAssets); no way to sort by date). If there is a different way to accomplish, we are interested to know!

We are using Nexus OSS with fairly standard install config. We have artifact purging for the repository for ~6 months.

Thanks!

---

<div class="post-metadata">

**Author:** ![mmartz](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mmartz](https://community.sonatype.com/u/mmartz)\
**Post date:** [September 26, 2022, 1:29pm UTC](https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363/2 "2022-09-26T13:29:58Z")

</div>

It’s likely the `blob_created` field that is causing slowness. I don’t see an index against that field. You’ll probably get better performance out of a search query against elasticsearch. It’s not documented in the Search API, but it looks like you might be able to pass in `lastBlobUpdated` to the search API. Since it’s not documented I can’t guarantee it won’t break in a future release, but searching via `<host>/service/rest/v1/search?sort=lastBlobUpdated&direction=desc` or `<host>/service/rest/v1/search/assets?sort=lastBlobUpdated&direction=desc` seems to put the most recent components first.

---

<div class="post-metadata">

**Author:** ![rbender](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rbender](https://community.sonatype.com/u/rbender)\
**Post date:** [October 1, 2022, 3:29pm UTC](https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363/3 "2022-10-01T15:29:53Z")

</div>

Thanks for the response, @mmartz!

Is there a way to limit the results, either by number or assets/components created/modified after a certain date?

I would try using only the first X results returned, but from what I can tell a search request seems to execute the whole search on the first API call, even if results are split across multiple pages. Subsequent requests to retrieve pages seem immediate where the first request seems to take longer.

Thanks!

---

<div class="post-metadata">

**Author:** ![mmartz](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mmartz](https://community.sonatype.com/u/mmartz)\
**Post date:** [October 3, 2022, 2:55am UTC](https://community.sonatype.com/t/groovy-script-performance-for-storagetx-findassets-in-maven-repo/9363/4 "2022-10-03T02:55:08Z")

</div>

I don’t believe there is any way to pass in a limit on the query.
