Index 设计

最近更新时间: 2024-06-12 15:06:00

  1. TDSQL PG提供的index类型: B-tree, Hash, GiST (GeneralizedSearch Tree), SP-GiST (space-partitioned GiST), GIN (Generalized InvertedIndex), BRIN (Block Range Index),目前不建议使用Hash,通常情况下使用B-tree;

  2. 建议create 或 drop index 时,加 CONCURRENTLY参数,这是个好习惯,达到与写入数据并发的效果;

  3. 建议对于频繁update, delete的包含于index 定义中的column的table, 用create index CONCURRENTLY , dropindex CONCURRENTLY 的方式进行维护其对应index;

  4. 建议用unique index 代替unique constraints,便于后续维护;

  5. 建议对where 中带多个字段and条件的高频 query,参考数据分布情况,建多个字段的联合index;

  6. 建议对固定条件的(一般有特定业务含义)且选择比好(数据占比低)的query,建带where的Partial Indexes;

    select * from test where status=1 and col=?; -- 其中status=1为固定的条件。

    create index on test (col) where status=1;

    7.建议对经常使用表达式作为查询条件的query,可以使用表达式或函数索引加速query;

    select * from test where exp(xxx);

    create index on test ( exp(xxx) );

    8.建议不要建过多index,一般不要超过6个,核心table(产品,订单)可适当增加index个数。