indexes

Stay organized with collections Save and categorize content based on your preferences.

Usage

view: my_view {
  derived_table: {
    indexes: ["order_id"]
    ...
  }
}
Hierarchy
indexes

- or -

indexes
Default Value
None

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

Special Rules
indexes is supported only on specific dialects

Definition

The indexes parameter lets you apply indexes to the columns of a persistent derived table (PDT) or an aggregate table. When you add more than one column, Looker will create one index for each column that you specify; it does not create a single, multi-column index. If the indexes parameter is missing from the query, Looker will warn you to add an indexes parameter to improve query performance. Learn more about indexing persistent derived tables on the Derived tables in Looker documentation page.

See the Dialect support for indexes section on this page for the list of dialects that support indexes.

The indexes parameter works only with tables that are persistent, such as PDTs and aggregate tables. indexes is not supported for derived tables without a persistence strategy.

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

If you use indexes with Redshift, you will create an interleaved sort key. You can also create regular sort keys using sortkeys, but you cannot use both at the same time. Distribution keys can be created with distribution.

Generally speaking, indexes should be applied to primary keys and date or time columns.

Examples

For a traditional database (for example, MySQL or Postgres), create a customer_order_facts persistent derived table. The PDT should rebuild when the order_datagroup datagroup is triggered and will have an index on customer_id:

view: customer_order_facts {
  derived_table: {
    explore_source: order {
      column: customer_id { field: order.customer_id }
      column: lifetime_orders { field: order.lifetime_orders }
    }
    datagroup_trigger: order_datagroup
    indexes: ["customer_id"]
  }
}

For a traditional database, create a customer_order_facts persistent derived table that is based on a SQL query and applies an index on customer_id:

view: customer_order_facts {
  derived_table: {
    sql:
      SELECT
        customer_id,
        COUNT(*) AS lifetime_orders
      FROM
        order
      GROUP BY
        customer_id ;;
    persist_for: "24 hours"
    indexes: ["customer_id"]
  }
}

For a traditional database, create a customer_day_facts derived table with indexes on both customer_id and 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"
    indexes: ["customer_id", "date"]
  }
}

For a Redshift database, create a customer_day_facts derived table with an interleaved sort key built from customer_id and 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"
    indexes: ["customer_id", "date"]
  }
}

Dialect support for indexes

The ability to use indexes depends on the database dialect your Looker connection is using. If you are working with something other than a traditional database (for example, MySQL or Postgres), your database may not support the indexes parameter. Looker will warn you if this is the case. You can swap out the indexes parameter for one that is appropriate for your database connection. Learn more about such parameters on the View parameters documentation page.

In the latest release of Looker, the following dialects support indexes: