sql - 索引所有作为表外键的列

标签 sql postgresql database-indexes

                              Table "public.policy"
 Column |  Type   | Collation | Nullable |                Default
--------+---------+-----------+----------+----------------------------------------
 pol_id | bigint  |           | not null | nextval('policy_pol_id_seq'::regclass)
 top_id | integer |           |          |
 idn_id | integer |           |          |
 rsc_id | integer |           |          |
 act_id | integer |           |          |
 tup_id | integer |           |          |

我的表结构是这样的,除了 pol_id 之外的所有列都是外键。可以在任何列上搜索策略,但它将与 top_id 一起使用。例如;搜索具有 top_id =1 和 act_id =2 的策略。 我怎样才能更好地索引这个表?可以只索引 top_id 还是可以索引所有列。 这是一张交易表。此表中的记录最多可达 100 万到 2。

最佳答案

您的描述建议四个索引:

  • top_id, idn_id
  • top_id, rsc_idrcs_id, top_id
  • top_id, act_idact_id, top_id
  • top_id, tup_idtup_id, top_id

这与数据库无关。 Oracle 在索引上实现了一种称为“跳过扫描”的方法。这将允许您减少索引的数量。

是否真的需要这些索引取决于 top_id 的选择性。如果只有少数匹配项,则可能不需要额外的每秒索引键。

关于sql - 索引所有作为表外键的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51324430/

相关文章:

database - PostgresQL/pgAdmin4/转储服务器版本不匹配

postgresql - 在 PostgreSQL 中,如何选择具有范围内任何元素的 jsonb 数组的行?

php - 是否可以对 MySQL 进行混合覆盖/追加批量写入?

mysql - 组合键 VS 主键 + 非唯一索引

sql - 如何在 SELECT (T-SQL) 中使用变量

android - 如何在mysql-android中插入图像

c# - LINQ to SQL - 调用内置聚合函数(即 STDEV)

sql - 如果表存在则执行 select 语句

java - Hibernate 仅保留第一个嵌套实体

mysql - 重复更新不适用于唯一索引