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

# query_memory_limit_exceeded

> Your analytics query consumed more memory than Unkey allows. Reduce the result set size by narrowing filters, limiting rows, or simplifying joins.

<Danger>`err:user:unprocessable_entity:query_memory_limit_exceeded`</Danger>

```json Example theme={"theme":"kanagawa-wave"}
{
  "meta": {
    "requestId": "req_4dgzrNP3Je5mU1tD"
  },
  "error": {
    "detail": "Query exceeded the maximum memory limit of 2GB",
    "status": 422,
    "title": "Unprocessable Entity",
    "type": "https://unkey.com/docs/errors/user/unprocessable_entity/query_memory_limit_exceeded"
  }
}
```

## What Happened?

Your query tried to use more than 2GB of memory! We limit memory usage to keep the analytics service stable and fast for everyone.

This typically happens when you're selecting too many rows, using large GROUP BY operations, or performing complex JOINs without enough filtering.

## How to Fix It

### 1. Use Aggregations Instead of Raw Data

Instead of fetching all rows, aggregate the data:

<CodeGroup>
  ```sql Memory Intensive - Fetches all rows theme={"theme":"kanagawa-wave"}
  SELECT *
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 7 DAY
  ```

  ```sql Memory Efficient - Aggregates data theme={"theme":"kanagawa-wave"}
  SELECT
    toStartOfHour(time) as hour,
    api_id,
    COUNT(*) as total,
    AVG(response_time) as avg_response
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 7 DAY
  GROUP BY hour, api_id
  ```
</CodeGroup>

### 2. Add More Filters

Reduce the amount of data the query needs to process:

<CodeGroup>
  ```sql Too Much Data theme={"theme":"kanagawa-wave"}
  SELECT api_id, key_id, outcome, time
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 30 DAY
  ```

  ```sql Filtered Query theme={"theme":"kanagawa-wave"}
  SELECT api_id, key_id, outcome, time
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 1 DAY
    AND api_id = 'api_123'
    AND outcome = 'VALID'
  ```
</CodeGroup>

### 3. Limit Result Size

Add a LIMIT to cap the number of rows:

```sql theme={"theme":"kanagawa-wave"}
SELECT api_id, key_id, outcome, time
FROM key_verifications_v1
WHERE time >= now() - INTERVAL 7 DAY
ORDER BY time DESC
LIMIT 10000
```

### 4. Avoid Large GROUP BY Cardinality

GROUP BY on high-cardinality columns (like `key_id`) uses a lot of memory. Instead, group by lower-cardinality columns:

<CodeGroup>
  ```sql High Memory - Millions of unique keys theme={"theme":"kanagawa-wave"}
  SELECT key_id, COUNT(*) as total
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 30 DAY
  GROUP BY key_id
  ```

  ```sql Lower Memory - Hundreds of APIs theme={"theme":"kanagawa-wave"}
  SELECT api_id, COUNT(*) as total
  FROM key_verifications_v1
  WHERE time >= now() - INTERVAL 30 DAY
  GROUP BY api_id
  ```
</CodeGroup>

## Need More Memory?

<Note>
  **Have a legitimate need for higher memory limits?** Contact our support team!

  [Reach out to support](mailto:support@unkey.com) and tell us:

  * What you're trying to analyze
  * Why the query needs more than 2GB of memory
  * An example of the query you're running

  We'll review your use case and see if we can accommodate your needs.
</Note>
