索引

用法

view: my_view {
  derived_table: {
    indexes: ["order_id"]
    ...
  }
}
层次结构
indexes

- 或 -

indexes
默认值

接受
PDT 或汇总表中的一个或多个列的名称

特殊规则
indexes 仅在特定方言中受支持

定义

借助 indexes 参数,您可以将索引应用于永久性派生表 (PDT) 或汇总表的列。如果您添加多个列,Looker 会为您指定的每个列创建一个索引,而不会创建一个多列索引。如果查询中缺少 indexes 参数,Looker 会警告您添加 indexes 参数以提高查询性能。如需详细了解如何为永久性派生表编制索引,请参阅 Looker 中的派生表文档页面。

如需查看支持 indexes 的方言列表,请参阅本页面的indexes 的方言支持部分。

indexes 参数仅适用于永久性表,例如 PDT 和汇总表。对于没有持久性策略的派生表,不支持 indexes

此外,对于使用 create_processsql_create 定义的派生表,不支持 indexes 参数。

如果您将 indexes 与 Redshift 搭配使用,则会创建交错排序键。您还可以使用 sortkeys 创建常规排序键,但不能同时使用这两种方法。可以使用 distribution 创建分布键。

一般来说,索引应应用于主键以及日期或时间列。

示例

对于传统数据库(例如 MySQL 或 Postgres),请创建 customer_order_facts 永久性派生表。当 order_datagroup datagroup 被触发时,PDT 应重建,并且在 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"]
  }
}

对于传统数据库,请创建一个基于 SQL 查询的customer_order_facts永久性派生表,并在 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"]
  }
}

对于传统数据库,请创建一个 customer_day_facts 派生表,并在 customer_iddate 上添加索引:

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"]
  }
}

对于 Redshift 数据库,请创建一个派生表 customer_day_facts,其中包含由 customer_iddate 构建的交织排序键:

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"]
  }
}

针对 indexes 的方言支持

能否使用 indexes 取决于 Looker 连接所使用的数据库方言。如果您使用的是传统数据库(例如 MySQL 或 Postgres)以外的数据库,则您的数据库可能不支持 indexes 参数。如果出现这种情况,Looker 会向您发出警告。您可以将 indexes 参数替换为适合您的数据库连接的参数。如需详细了解此类参数,请参阅查看参数文档页面。

在最新版 Looker 中,以下方言支持 indexes

方言 是否支持?
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