Skip to main content

Query Performance

Optimize your GraphQL queries for better performance.

Performance Checklist​

1. Use Indexed Fields for Filtering​

Indexed fields:

  • term_id (primary keys)
  • creator_id
  • type
  • created_at
  • account_id
# GOOD: Filtering by indexed fields
query GetAtoms($type: atom_type!, $since: timestamptz!) {
atoms(
where: {
type: { _eq: $type }
created_at: { _gte: $since }
}
) {
term_id
label
}
}

2. Request Only Needed Fields​

# GOOD: Minimal field selection
query GetAtoms {
atoms(limit: 10) {
term_id
label
}
}

3. Use Aggregates Instead of Fetching All Nodes​

# GOOD: Use aggregates for counts
query CountPositions($accountId: String!) {
positions_aggregate(where: { account_id: { _eq: $accountId } }) {
aggregate {
count
}
}
}

4. Limit Nested Queries​

# GOOD: Limit depth and breadth
query GetAtomWithTriples($id: String!) {
atom(term_id: $id) {
label
as_subject_triples(limit: 10) {
predicate { label }
object { label }
}
}
}

5. Use Primary Key Lookups When Possible​

# GOOD: Direct primary key lookup
query GetAtom($id: String!) {
atom(term_id: $id) {
term_id
label
}
}

6. Paginate Large Result Sets​

# GOOD: Always use limit
query GetAtoms($limit: Int!, $offset: Int!) {
atoms(
limit: $limit
offset: $offset
order_by: { created_at: desc }
) {
term_id
label
}
}

7. Use Pre-Computed Tables​

# GOOD: Use time-series tables
query GetDailyStats($termId: String!, $curveId: numeric!) {
share_price_change_stats_daily(
where: {
term_id: { _eq: $termId }
curve_id: { _eq: $curveId }
}
limit: 30
) {
bucket
difference
}
}

8. Leverage Database Functions​

# GOOD: Use backend functions
query GetFollowing($address: String!) {
following(args: { address: $address }) {
id
label
}
}

Monitoring​

  • Profile query execution time
  • Monitor response payload sizes
  • Track cache hit rates
  • Watch for N+1 query patterns