自动创建数百个索引

Hundreds of indexes created automatically

我创建了一个table,用于存储与不同种类资源相关的信息,table描述为每个外键打印250个索引,这种行为正常吗?

table 仅包含 50 行。

CREATE TABLE IF NOT EXISTS resource
(
    id serial PRIMARY KEY,
    kind resource_kind NOT NULL,
    author_id int REFERENCES user(id) ON DELETE SET NULL,

    ---uri text NOT NULL,
    creation_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modification_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    message_id int references message,
    track_id int references track,
    location_id int references track,
    fileupload_id int references upload

    check (
        (
            (message_id is not null)::integer +
            (track_id is not null)::integer +
            (location_id is not null)::integer +
            (fileupload_id is not null)::integer

        ) = 1
    )
);

create unique index on resource (message_id) where message_id is not null;
create unique index on resource (track_id) where track_id is not null;
create unique index on resource (location_id) where location_id is not null;
create unique index on resource (fileupload_id) where fileupload_id is not null;

这是“\d 资源”的输出

        Colonna      |            Tipo             | Ordinamento |         |                          Default                     
-------------------+-----------------------------+-------------+-----------------+------------------------------------------------
 id                | integer                     |             | not null        | nextval('resource_id_seq'::regclass)
 kind              | resource_kind     |             | not null        | 
 author_id         | integer                     |             |                 | 
 creation_time     | timestamp without time zone |             | not null        | CURRENT_TIMESTAMP
 modification_time | timestamp without time zone |             | not null        | CURRENT_TIMESTAMP
 message_id        | integer                     |             |                 | 
 track_id          | integer                     |             |                 | 
 location_id       | integer                     |             |                 | 
 fileupload_id     | integer                     |             |                 | 
Indici:
    "resource_pkey" PRIMARY KEY, btree (id)
    "resource_fileupload_id_idx" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx1" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx10" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx100" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx101" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx102" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx103" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx104" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx105" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx106" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
    "resource_fileupload_id_idx107" UNIQUE, btree (fileupload_id) WHERE fileupload_id IS NOT NULL
...

然后输出带有每个外键的数百个索引。

不,那是不正常的。

一定是某人或某些软件创建了索引。

除一个外全部删除,因为它们使用 space 并且会使数据修改慢得无法忍受。

下次您可以将所有约束放在 table 的 DDL 中,使其以 NOT EXISTS 为条件(这还将创建唯一的 名称 对于索引):


CREATE TABLE IF NOT EXISTS resource
(
    id serial PRIMARY KEY
    , kind INTEGER NOT NULL -- resource_kind NOT NULL
    , author_id INTEGER -- REFERENCES user(id) ON DELETE SET NULL

    -- , uri text NOT NULL
    , creation_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    , modification_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

    , message_id INTEGER -- references message
    , track_id INTEGER -- references track
    , location_id INTEGER -- references track
    , fileupload_id INTEGER -- references upload
        , unique (message_id)
        , unique (track_id)
        , unique (location_id)
        , unique (fileupload_id)

    , check (
        (
            (message_id is not null)::integer
            + (track_id is not null)::integer
            + (location_id is not null)::integer
            + (fileupload_id is not null)::integer

        ) = 1
    )
);
-- check it
\d resource