> ## Documentation Index
> Fetch the complete documentation index at: https://docs.metabind.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Query Optimization

> Tips for efficient asset queries

The API automatically selects the most efficient index based on your query parameters. Understanding these patterns helps you write performant queries.

## Index Usage

<AccordionGroup>
  <Accordion title="Tag Filtering">
    Queries filtered by tag use a dedicated tag index and are highly efficient. Use tag filters when possible for best performance.

    ```json theme={null}
    {
      "filter": {
        "tags": { "any": ["hero", "product"] }
      }
    }
    ```
  </Accordion>

  <Accordion title="Date-based Sorting">
    Queries with date-based sorting use the table's primary index. This is the default and most efficient for recent assets.

    ```json theme={null}
    {
      "sort": [{ "field": "updatedAt", "order": "desc" }]
    }
    ```
  </Accordion>

  <Accordion title="Complex Queries">
    Queries combining multiple filters may use different execution strategies internally. The API automatically selects the most efficient index based on your query parameters.
  </Accordion>
</AccordionGroup>

## Best Practices

### Use Specific Filters First

More selective filters should be primary. Tag and type filters are highly optimized:

```json theme={null}
{
  "filter": {
    "tags": { "any": ["hero"] },
    "type": { "eq": "image/jpeg" },
    "status": { "eq": "active" }
  }
}
```

### Limit Result Size

Always use pagination and reasonable limits:

```json theme={null}
{
  "filter": { ... },
  "limit": 50,
  "page": 1
}
```

### Avoid Wildcards at Start

Pattern matching with leading wildcards (`%name`) is slower than trailing wildcards (`name%`):

```json theme={null}
// Faster
{ "name": { "like": "hero-%" } }

// Slower
{ "name": { "like": "%-hero" } }
```

### Use Date Ranges

When filtering by date, provide both bounds when possible:

```json theme={null}
{
  "filter": {
    "updatedAt": {
      "gte": "2024-01-01T00:00:00Z",
      "lte": "2024-03-31T23:59:59Z"
    }
  }
}
```
