我创建了一个表,该表用于存储与不同类型资源有关的信息,该表描述为每个外键打印250个索引,这是否正常?
该表仅包含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
...
然后,每个外键的输出都带有数百个索引。
答案 0 :(得分:1)
不,那是不正常的。
必须由某些人或某些软件创建索引。
删除它们中的所有一个,因为它们会占用空间并且会使数据修改变得非常缓慢。
答案 1 :(得分:1)
下次您可以将所有约束放入表的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