Query Performance Patterns

Content: Query Performance Patterns

User Intent

"How do I optimize content queries for production?"

Operation

  • SDK Method: queryContents()

  • GraphQL: queryContents query

  • Entity Type: Content

  • Common Use Cases: Performance optimization, pagination, large result sets, production patterns

Performance Overview

Query performance depends on query type, filters used, result size, and indexing. Understanding these patterns helps optimize for production.

TypeScript (Canonical)

import { Graphlit } from 'graphlit-client';
import { ContentTypes, ObservableTypes, SearchTypes } from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

// Efficient pagination
const page1 = await graphlit.queryContents({
  search: "query",
  limit: 50,
  offset: 0
});

const page2 = await graphlit.queryContents({
  search: "query",
  limit: 50,
  offset: 50
});

// Use indexed filters for speed
const fast = await graphlit.queryContents({
  search: "machine learning",
  filter: {
    types: [ContentTypes.File],           // Indexed
    creationDateRange: { from: '2024-01-01' },  // Indexed
    collections: [{ id: 'collection-id' }]      // Indexed
  },
  limit: 20  // Reasonable limit
});

// Narrow scope before querying
const scoped = await graphlit.queryContents({
  filter: {
    types: [ContentTypes.Email],
    feeds: [{ id: 'specific-feed' }],  // Narrow to one feed
    createdInLast: 'P7D'               // Recent only
  }
});

Query Performance Characteristics

Fast Queries (<50ms)

Medium Queries (50-200ms)

Slow Queries (200ms+)

Pagination Best Practices

Offset-Based Pagination

Progressive Loading

Limit Best Practices

Caching Strategies

Client-Side Result Caching

Query Fingerprinting

Indexed vs Non-Indexed Fields

Indexed (Fast)

Non-Indexed (Slower)

Optimization Patterns

Pattern 2: Use Appropriate Search Type

Pattern 3: Batch Operations

Pattern 4: Count vs Results

Pagination

page1 = await graphlit.queryContents( search="query", limit=50, offset=0 )

page2 = await graphlit.queryContents( search="query", limit=50, offset=50 )

Optimized query

fast = await graphlit.queryContents( search="machine learning", filter=ContentFilterInput( types=[ContentTypes.File], creation_date_range=DateRangeInput( from_='2024-01-01' ) ), limit=20 )

Developer Hints

Measure Query Performance

Parallelize Independent Queries

Use Reasonable Limits

Common Issues & Solutions

Issue: Queries timing out Solution: Reduce scope with filters and limits

Issue: Slow entity-based queries Solution: Expected behavior, optimize where possible

Issue: Need all results but hitting limits Solution: Use pagination

Production Example

Last updated

Was this helpful?