sortkeys

Usage

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

- or -

sortkeys
Default Value
None

Accepts
The names of one or more columns in a PDT or an aggregate table

Special Rules
sortkeys is supported only on specific dialects

Definition

The sortkeys parameter lets you specify one or more columns of a persistent derived table (PDT) or an aggregate table 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 section for the list of dialects that support sortkeys.

You can also create an interleaved sort key by using indexes 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, such as PDTs and aggregate tables. sortkeys is not supported for derived tables without a persistence strategy.

In addition, the sortkeys parameter is not supported for derived tables that are defined using create_process or sql_create.

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.

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 that has a sortkey on date and rebuilds when the datagroup 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; sortkeys will not work with these databases.

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