Context
Querying views with a large number of instances (>1 million), we frequently encounter query timeout issues. This has become a critical bottleneck affecting application performance and user experience.
To mitigate this, we introduced on the app layer a pre-query caching strategy:
- Before sending a query to Cognite, we aggregate the number of instance spaces for a given view using the endpoint /models/instances/aggregate.
- This result is stored in a cache layer.
- When a query is initiated, we check if the user included a space filter.
- If not, we append the known relevant spaces from the cache to the query filter.
This approach has significantly reduced timeouts across our applications. However, it introduces new challenges:
- One request per view is still needed to fetch associated spaces.
- Cache invalidation must be managed periodically, especially as user capabilities may change.
- This workaround does not help with timeouts in the CDF UI or Infield tools, where we cannot control the query behavior in the same way.
Proposed Solution 1: Data Model Metadata Configuration
We propose enhancing the data model configuration by allowing metadata to define scoped instance spaces. This way, views can automatically infer relevant spaces during queries.
Example Data Model
externalId = "RadixSOL"
space = "RDX-COR-ALL-DMD"
version = "1_0_0"
type User {
name: String
birthday: String
relatedTo: [Asset]
}
type Asset
@import(
dataModel: {
externalId: "CogniteCore"
version: "v1"
space: "cdf_core"
}
) {
name: String
description: String
}
Desired Configuration
We want to specify that views within RDX-COR-ALL-DMD should be queried using:
- A fixed set of instance spaces, e.g., [ "RDX-USA-ALL-DAT", "RDX-BRA-ALL-DAT" ]
- Or, using a prefix pattern, e.g., RDX-*, to dynamically resolve all matching spaces.
This would provide a declarative, maintainable, and scalable approach to improve query performance across tools and APIs.
Proposed Solution 2: Instance Space Inspection Endpoint
If extending metadata is too complex, we propose an alternative:
Introduce an API endpoint like:
GET /models/datamodels/inspectSpaces
This endpoint would return a mapping of each view in the data model to the instance spaces it spans, eliminating the need for repetitive aggregation requests. Ideally, this endpoint would not consider the spaces that the user has access to, but all the spaces related to each view in a data model.
This would:
- Reduce the overhead on the aggregate endpoint.
- Simplify client logic.
- Provide a reusable utility for performance optimization in both UI and backend services.
Check the
documentation
Ask the
Community
Take a look
at
Academy
Cognite
Status
Page
Contact
Cognite Support