Query Performance
Optimize your GraphQL queries for better performance.
Performance Checklistβ
1. Use Indexed Fields for Filteringβ
Indexed fields:
term_id(primary keys)creator_idtypecreated_ataccount_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