# Complexity

Each query hitting our Content Delivery API has a complexity cost based on

-   which type of field is present
-   how many fields are present
    
-   how many filters are present
-   how many sorting parameters are present
    
-   the page size
    

### Limits

Each DatoCMS plan comes with a maximum allowed complexity cost which is **10,000,000 by default**. Take a look at the "Plan and usage" section of your project in the [Account dashboard](https://dashboard.datocms.com/) to understand which is your complexity limit.

The Content Delivery API sets the `X-Complexity` header in the response to let you know the calculated complexity cost for your submitted query and the `X-Max-Complexity` header containing your current plan complexity limit.

If the query complexity cost above your plan limit **you'll get an error from the Content Delivery API**:

```json
{
  "errors": [
    {
      "message": "Query has complexity of xxx, which exceeds max complexity of yyy"
    }
  ]
}
```

### Base costs

Unless specified otherwise, each requested GraphQL field has a cost of 1.

Each filter argument has a cost of 250, except for [deep filtering arguments](https://www.datocms.com/docs/content-delivery-api/complexity.md#deep-filtering).

Each sorting parameter has a cost of 250.

To calculate the pagination cost, multiply the page size by the inner fields' cost. By default, the page size is 20 but you can override it using the `first` argument.

### Model root fields

#### Collection field

Root fields like `allArtists` have a base cost of 100. To this, the cost of filters, the cost of sorting, and the cost of pagination must be added.

The following query has a cost of 140: 100 (base) + 20 (implicit page size) x 2 (inner fields' cost)

```graphql
query { # it returns at max 20 records by default
  allArtists { # 100
    id # 1
    name # 1
  }
}
```

The following query has a cost of 1,175: 100 (base) + 750 (filtering) + 250 (sorting) + 25 (page size) x 3 (inner fields' cost)

```graphql
query {
  allArtists( # 100
    filter: {
      birth: {gt: "1990-01-01", lt: "2010-01-01"}, # 250 x 2 => 500
      country: {eq: "DE"} # 250
    },
    orderBy: [name_ASC], # 250
    first: 25 # explicit page size
  ) {
    id # 1
    name # 1
    age # 1
  }
}
```

#### Collection meta field

Root fields like `_allArtistsMeta` have a base cost of 1,000. To this, the cost of filters and the inner field's cost must be added.

The following query has a cost of 1,251: 1,000 (base) + 250 (filtering) + 1 (inner fields' cost)

```graphql
query {
  _allArtistsMeta( # 1,000
    filter: { country: {eq: "NL"} } # 250
  ) {
    count # 1
  }
}
```

#### Single record field

Root fields like `artist` have a base cost of 50. To this, the cost of filters, the cost of sorting, and the inner fields' cost must be added.

The following query has a complexity cost of 301: 50 (base) + 250 (filtering) + 1 (inner fields' cost)

```graphql
query {
  artist( # 50
    filter: { id: {eq: "123"} } # 250
  ) {
    name # 1
  }
}
```

#### Single instance record field

Root fields about single-instance records have a base cost of 25. To this, the inner field's cost must be added.

The following query has a cost of 27: 25 (base) + 2 (inner fields' cost)

```graphql
query {
  contactPage { # 25
    phoneNumber # 1
    emailAddress # 1
  }
}
```

#### Inverse relationships fields

Fields like `_allReferencingMovies` can be used once activated the [inverse relationships feature](https://www.datocms.com/docs/content-delivery-api/inverse-relationships.md) for the model. They have a base cost of 100. To this, the cost of filters, the cost of sorting, and the cost of pagination must be added.

The following section has a cost of 1,110: 100 (base) + 500 (filtering) + 500 (sorting) + 5 (page size) x 2 (inner fields' cost)

```graphql
...
    _allReferencingMovies( # 100
      through: {
        fields: {anyIn: [movie_artist]}, # 250
        locales: {anyIn: en} # 250
      },
      orderBy: [title_ASC, _createdAt_DESC], # 500,
      first: 5 # explicit page size
    ) {
      id  # 1
      title # 1
    }
...
```

Fields like `_allReferencingMoviesMeta` have a base cost of 1,000. To this, the cost of filters and the inner fields' cost must be added.

The following section has a cost of 1,001.

```graphql
...
    _allReferencingMoviesMeta { # 1,000
      count # 1
    }
...
```

### Model fields

The following GraphQL fields differ from the base cost (1):

-   Single asset field: 5
-   Asset gallery field: 5 x inner fields' cost
    
-   Multiple-paragraph text, when rendering Markdown in HTML: 5
-   JSON field: 5
    
-   Single link field: 10
-   Multiple links field: 5 x inner fields' cost
    
-   Modular content field: 5 x inner fields' cost
-   Structured text field
    
    -   value: 10
        
    -   blocks: 5 x inner fields' cost
        
    -   links: 5 x inner fields' cost
        
-   `children` field (available in tree collections): 5 x inner fields' cost
-   `parent` field (available in tree collections): 25
    
-   Localized field
    
    -   value: number of environment's locales x inner fields' cost
        
-   SEO field
    
    -   image: 5
        
-   `_seoMetaTags`: 5
    

The following query has a cost of 351, composed by:

-   50 (base cost of single record field)
-   250 (filtering)
    
-   5 + 1 + 5 (photo field)
-   10 + 5 x 1 + 5 x 2 (content field)
    
-   5 x 3 (movies field)
    

```graphql
query {
  artist( # 50
    filter: { id: {eq: "123"} } # 250
  ) {
    photo { #single asset field: 5
      url # 1
      blurUpThumb # 5
    }
    content { # structured text field
      value # 10
      links { # 5 x inner fields' cost
        id # 1
      }
      blocks { # 5 x inner fields' cost
        id # 1
        text # 1
      }
    }
    movies { # multiple links field: 5 x inner fields' cost
      id # 1
      title # 1
      releaseDate # 1
    }
  }
```

### Unions

A GraphQL union complexity is the max complexity between all of the possible types.

The modular content field `content` has a complexity cost of 10: 5 x 2

```graphql
query {
  artist(filter: { id: { eq: "123" }}) {
    name
    content { # 5 x max possible union's cost
      ... on MovieRecord {
        title # 1
      }
      ... on TvSerieRecord {
        title # 1
        channel # 1
      }
    }
  }
}
```

### Deep Filtering

Normally, each filter argument has a cost of 250, but when using [deep filtering](https://www.datocms.com/docs/content-delivery-api/deep-filtering.md) an additional cost of 1,000,000 is added for each type of block model defined in the filter.

The following query has a cost of 2,000,890: 100 (base) + 2,000,750 (filtering) + 20 (implicit page size) x 2 (inner fields' cost)

```graphql
query {
  allBlogPosts( # 100
    filter: {
      content: {
        any: {
          product: { # 1,000,000
            name: {
              eq: "T-Shirt" # 250
            },
            price: {
              gt: 30 # 250
            }
          }
          cta: { # 1,000,000
            title: {
              isPresent: true # 250
            }
          }
        }
      }
    }
  ) {
    id # 1
    title # 1
  }
}
```

### Upload root fields

#### Collection field

Root field `allUploads` has a base cost of 100. To this, the cost of filters, the cost of sorting, and the cost of pagination must be added.

The following query has a cost of 801: 100 (base) + 250 (filtering) + 250 (sorting) + 30 (page size) x 7 (inner fields' cost)

```graphql
query {
  allUploads( # 100
    filter: {format: {eq: "jpg"}}, # 250
    orderBy: [size_DESC], # 250
    first: 30 # explicit page size
  ) {
    id # 1
    url # 1
    blurUpThumb # 5
  }
}
```

#### Collection meta field

Root field `_allUploadsMeta` has a base cost of 1,000. To this, the cost of filters and the inner fields' cost must be added.

The following query has a cost of 1,251: 1,000 (base) + 250 (filtering) + 1 (inner fields' cost)

```graphql
query {
  _allUploadsMeta( # 1,000
    filter: {format: {eq: "jpg"}} # 250
  ) {
    count # 1
  }
}
```

#### Single upload field

Root field `upload` has a base cost of 50. To this, the cost of filters, the cost of sorting, and the inner fields' cost must be added.

The following query has a complexity cost of 308: 50 (base) + 250 (filtering) + 8 (inner fields' cost)

```graphql
query {
  upload( # 50
    filter: {id: {eq: "123"}} # 250
  ) {
    url #1
    title #1
    blurUpThumb # 5
    blurhash #1
  }
}
```

### Upload fields

The following GraphQL fields differ from the base cost (1):

-   blurUpThumb: 5
    

### Site field

Root field `_site` has a base cost of 10. To this, the inner fields' cost must be added.

The following query has a cost of 13.

```graphql
query {
  _site { # 10
    globalSeo { # 1
      siteName # 1
      titleSuffix # 1
    }
  }
}
```

## Related content in "Content Delivery API"

- [Content Delivery API Overview](https://www.datocms.com/docs/content-delivery-api.md)
- [Using the JavaScript CDA client](https://www.datocms.com/docs/content-delivery-api/your-first-request.md)
- [Authentication and permissions](https://www.datocms.com/docs/content-delivery-api/authentication.md)
- [Configuring requests: envs, drafts, strict mode, cache tags, etc.](https://www.datocms.com/docs/content-delivery-api/api-endpoints.md)
- [How to fetch records](https://www.datocms.com/docs/content-delivery-api/how-to-fetch-records.md)
- [Filtering records](https://www.datocms.com/docs/content-delivery-api/filtering-records.md)
- [Deep Filtering](https://www.datocms.com/docs/content-delivery-api/deep-filtering.md)
- [Ordering records](https://www.datocms.com/docs/content-delivery-api/ordering-records.md)
- [Pagination](https://www.datocms.com/docs/content-delivery-api/pagination.md)
- [Localization](https://www.datocms.com/docs/content-delivery-api/localization.md)
- [Direct vs. Inverse relationships](https://www.datocms.com/docs/content-delivery-api/inverse-relationships.md)
- [Hierarchical sorting (Tree-like collections)](https://www.datocms.com/docs/content-delivery-api/hierarchical-sorting.md)
- [Modular content fields](https://www.datocms.com/docs/content-delivery-api/modular-content-fields.md)
- [Structured text fields](https://www.datocms.com/docs/content-delivery-api/structured-text-fields.md)
- [Images and videos](https://www.datocms.com/docs/content-delivery-api/images-and-videos.md)
- [Filtering uploads](https://www.datocms.com/docs/content-delivery-api/filtering-uploads.md)
- [SEO and favicon](https://www.datocms.com/docs/content-delivery-api/seo-and-favicon.md)
- [Meta fields](https://www.datocms.com/docs/content-delivery-api/meta-fields.md)
- [Error codes & handling failures (CDA)](https://www.datocms.com/docs/content-delivery-api/errors.md)
- [CDA Technical Limits & Rate Limits](https://www.datocms.com/docs/content-delivery-api/technical-limits.md)
- [Complexity](https://www.datocms.com/docs/content-delivery-api/complexity.md)
- [Cache Tags](https://www.datocms.com/docs/content-delivery-api/cache-tags.md)
- [Custom Scalar Types](https://www.datocms.com/docs/content-delivery-api/custom-scalar-types.md)
- [Changelog](https://www.datocms.com/docs/content-delivery-api/changelog.md)