How to optimize DAML JSON API Query

I am using DAML with fabric as a ledger.

Now when I want to retrieve some data from the ledger I am using :-

Endpoint - /v1/query

Body -

    "templateIds": [
    "query": {
        "data": "data"

To retrieve this particular contract it takes 5.28 seconds.
This is a lot, are there any steps that I can follow by which it can be reduced.

1 Like

The first and most important step is to enable the SQL query store as described in HTTP JSON API Service — Daml SDK 1.15.0 documentation. The in-memory backend does not perform any caching and I recommend against using it for production usage.

From there on there are a few factors to consider:

  1. The total number of active contracts that you queried for since you created the database since that’s the set of contracts that we have to store.
  2. The number of active contracts of the given template (exampleTemplateId in your example) that are visible to the set of parties in your token.
  3. The fraction of that for which the query holds true.

Currently, the database queries are not heavily optimized but we are actively working on that first focusing on Oracle but we will translate the improvements to the PostgreSQL backend as well.

However even without those optimizations, I’d expect that you can get much faster than 5.x seconds if we’re talking < 1 million active contracts in total.


Hi @cocreature, Thanks we used this optimization technique and it worked.
But, likely in production for us it would be more than 1 million contracts, so can you tell me any tentative date till when the optimization for postgreSQL happen?

1 Like

I can’t give you a precise timeline but I definitely expect that they land by the end of the year at the latest possibly earlier.
That said, I recommend testing if you cannot already meet your performance requirements on the current implementation. It may already be fast enough.

1 Like