dbt indexes 配置简单说明

dbt indexes 索引配置的目的是提升系统性能,为了方便维护dbt 提供了默认空实现,对于不同db 会有不同的实现,以下简单说明下

内部macro

  • 定义

可以看到基本实现都为空,由具体的db adapter 实现

{% macro get_create_index_sql(relation, index_dict) -%}
  {{ return(adapter.dispatch('get_create_index_sql', 'dbt')(relation, index_dict)) }}
{% endmacro %}
 
{% macro default__get_create_index_sql(relation, index_dict) -%}
  {% do return(None) %}
{% endmacro %}
 
 
{% macro create_indexes(relation) -%}
  {{ adapter.dispatch('create_indexes', 'dbt')(relation) }}
{%- endmacro %}
 
{% macro default__create_indexes(relation) -%}
  {%- set _indexes = config.get('indexes', default=[]) -%}
 
  {% for _index_dict in _indexes %}
    {% set create_index_sql = get_create_index_sql(relation, _index_dict) %}
    {% if create_index_sql %}
      {% do run_query(create_index_sql) %}
    {% endif %}
  {% endfor %}
{% endmacro %}
 
 
{% macro get_drop_index_sql(relation, index_name) -%}
    {{ adapter.dispatch('get_drop_index_sql', 'dbt')(relation, index_name) }}
{%- endmacro %}
 
{% macro default__get_drop_index_sql(relation, index_name) -%}
    {{ exceptions.raise_compiler_error("`get_drop_index_sql has not been implemented for this adapter.") }}
{%- endmacro %}
 
 
{% macro get_show_indexes_sql(relation) -%}
    {{ adapter.dispatch('get_show_indexes_sql', 'dbt')(relation) }}
{%- endmacro %}
 
{% macro default__get_show_indexes_sql(relation) -%}
    {{ exceptions.raise_compiler_error("`get_show_indexes_sql has not been implemented for this adapter.") }}
{%- endmacro %}
  • pg 的处理
{% macro postgres__get_create_index_sql(relation, index_dict) -%}
  {%- set index_config = adapter.parse_index(index_dict) -%}
  {%- set comma_separated_columns = ", ".join(index_config.columns) -%}
  {%- set index_name = index_config.render(relation) -%}
 
  create {% if index_config.unique -%}
    unique
  {%- endif %} index if not exists
  "{{ index_name }}"
  on {{ relation }} {% if index_config.type -%}
    using {{ index_config.type }}
  {%- endif %}
  ({{ comma_separated_columns }})
{%- endmacro %}

参考配置

{{ config(
    materialized = 'table',
    indexes=[
      {'columns': ['column_a'], 'type': 'hash'},
      {'columns': ['column_a', 'column_b'], 'unique': True},
    ]
)}}
 
select ...

说明

了解index 的一些使用,可以提升表的查询性能,对于详细的使用可以参考具体adapter 介绍

参考资料

https://docs.getdbt.com/reference/resource-configs/postgres-configs#indexes

posted on   荣锋亮  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-07-24 graylog server 模块说明一 入口简单说明
2022-07-24 pmm 最近的一些变动
2022-07-24 coroot-pg-agent 专注性能的pg promtheus exporter
2022-07-24 jmte 模版引擎
2022-07-24 tauri 新的桌面应用开发模式
2021-07-24 解决dremio 32k 大小的问题
2020-07-24 使用vfsgen 嵌入静态资源到golang

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示