## Usage

```
view: my_view {
  derived_table: {
    sortkeys: ["date"]
    ...
  }
}
```

|---|---|
| Hierarchy [View File](https://docs.cloud.google.com/looker/docs/2616/reference/param-view.md.txt) [`view`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-view.md.txt) [`derived_table`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-derived-table.md.txt) `sortkeys` - or - [Model File](https://docs.cloud.google.com/looker/docs/2616/reference/param-model.md.txt) [`explore`](https://docs.cloud.google.com/looker/docs/2616/reference/param-explore-explore.md.txt) [`aggregate_table`](https://docs.cloud.google.com/looker/docs/2616/reference/param-explore-aggregate-table.md.txt) [`materialization`](https://docs.cloud.google.com/looker/docs/2616/reference/param-explore-aggregate-table.md.txt#materialization) `sortkeys` | Default Value None <br /> Accepts The names of one or more columns in a PDT or an aggregate table <br /> Special Rules `sortkeys` is supported only on [specific dialects](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-sortkeys.md.txt#dialect_support) |

## Definition

The `sortkeys` parameter lets you specify one or more columns of a [persistent derived table](https://docs.cloud.google.com/looker/docs/2616/derived-tables.md.txt#persistent-derived-tables) (PDT) or an [aggregate table](https://docs.cloud.google.com/looker/docs/2616/reference/param-explore-aggregate-table.md.txt) on which to apply a regular sort key. Use a sort key to specify the columns of a PDT that will be most frequently sorted by to make querying the data faster.
> See the [Dialect support for `sortkeys`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-sortkeys.md.txt#dialect_support) section for the list of dialects that support `sortkeys`.

You can also create an interleaved sort key by using [`indexes`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-indexes.md.txt) instead. You cannot use both at the same time, but at least one is required.

> The `sortkeys` parameter works only with tables that are [persistent](https://docs.cloud.google.com/looker/docs/2616/derived-tables.md.txt#adding_persistence), such as PDTs and aggregate tables. `sortkeys` is not supported for [derived tables](https://docs.cloud.google.com/looker/docs/2616/creating-ndts.md.txt) without a persistence strategy.
>
> In addition, the `sortkeys` parameter is not supported for derived tables that are defined using [`create_process`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-create-process.md.txt) or [`sql_create`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-sql-create.md.txt).

Generally speaking, a sort key should be applied to date or time columns in the table, and possibly to columns that will be used frequently as filters. More information can be found in the [Amazon Redshift documentation](http://docs.aws.amazon.com/redshift/latest/dg/c_best-practices-sort-key.html).

## Examples

These examples assume that you are working with a Redshift database so that you can use the `sortkeys` parameter.

Create a `customer_day_facts` persistent [native derived table](https://docs.cloud.google.com/looker/docs/2616/creating-ndts.md.txt) that has a sortkey on `date` and rebuilds when the [datagroup](https://docs.cloud.google.com/looker/docs/2616/caching-and-datagroups.md.txt#using_a_datagroup_for_a_pdt) `order_datagroup` is triggered:

    view: customer_day_facts {
      derived_table: {
        explore_source: order {
          column: customer_id { field: order.customer_id }
          column: date { field: order.order_date }
          column: num_orders { field: order.customer_order_count }
        }
        datagroup_trigger: order_datagroup
        sortkeys: ["date"]
      }
    }

Create a `customer_day_facts` derived table that is based on a SQL query and has a sortkey on `date`:

    view: customer_day_facts {
      derived_table: {
        sql:
          SELECT
            customer_id,
            DATE(order_time) AS date,
            COUNT(*) AS num_orders
          FROM
            order
          GROUP BY
            customer_id ;;
        persist_for: "24 hours"
        sortkeys: ["date"]
      }
    }

Create a `customer_day_facts` derived table that is based on a SQL query with a sortkey on `date` and `customer_id`:

    view: customer_day_facts {
      derived_table: {
        sql:
          SELECT
            customer_id,
            DATE(order_time) AS date,
            COUNT(*) AS num_orders
          FROM
            order
          GROUP BY
            customer_id ;;
        persist_for: "24 hours"
        sortkeys: ["date", "customer_id"]
      }
    }

## Dialect support for `sortkeys`

The ability to use `sortkeys` depends on the database dialect your Looker connection is using. In the latest release of Looker, the following dialects support `sortkeys`:
> Traditional SQL dialects (such as MySQL and Postgres) should use [`indexes`](https://docs.cloud.google.com/looker/docs/2616/reference/param-view-indexes.md.txt); `sortkeys` will not work with these databases.

| Dialect | Supported? |
|---|---|
| Actian Avalanche | No |
| Amazon Athena | No |
| Amazon Aurora MySQL | No |
| Amazon Redshift | Yes |
| Amazon Redshift 2.1+ | Yes |
| Amazon Redshift Serverless 2.1+ | Yes |
| Apache Druid | No |
| Apache Druid 0.13.x - 0.17.x | No |
| Apache Druid 0.18+ | No |
| Apache Hive 2.3+ | Yes |
| Apache Hive 3.1.2+ | Yes |
| Apache Spark 3+ | Yes |
| ClickHouse | No |
| Cloudera Impala 3.1+ | No |
| Cloudera Impala 3.1+ with Native Driver | No |
| Cloudera Impala with Native Driver | No |
| DataVirtuality | No |
| Databricks | No |
| Denodo 7 | No |
| Denodo 8 \& 9 | No |
| Dremio | No |
| Dremio 11+ | No |
| Exasol | No |
| Google BigQuery Legacy SQL | No |
| Google BigQuery Standard SQL | No |
| Google Cloud AlloyDB for PostgreSQL | No |
| Google Cloud PostgreSQL | No |
| Google Cloud SQL | No |
| Google Spanner | No |
| Greenplum | No |
| HyperSQL | No |
| IBM Netezza | No |
| MariaDB | No |
| Microsoft Azure PostgreSQL | No |
| Microsoft Azure SQL Database | No |
| Microsoft Azure Synapse Analytics | No |
| Microsoft SQL Server 2008+ | No |
| Microsoft SQL Server 2012+ | No |
| Microsoft SQL Server 2016 | No |
| Microsoft SQL Server 2017+ | No |
| MongoBI | No |
| MongoSQL | No |
| MySQL | No |
| MySQL 8.0.12+ | No |
| Oracle | No |
| Oracle ADWC | No |
| PostgreSQL 9.5+ | No |
| PostgreSQL pre-9.5 | No |
| PrestoDB | No |
| PrestoSQL | No |
| SAP HANA | No |
| SAP HANA 2+ | No |
| SingleStore | No |
| SingleStore 7+ | No |
| Snowflake | No |
| Teradata | No |
| Trino | No |
| Vector | No |
| Vertica | No |