我在文档中找不到这个问题的明确答案.如果列是数组类型,是否会对所有输入的值进行单独索引?
我创建了一个包含一int[]列的简单表,并在其上放置了一个唯一索引.我注意到我无法添加相同的整数数组,这使我相信索引是数组项的组合,而不是每个项的索引.
INSERT INTO "Test"."Test" VALUES ('{10, 15, 20}');
INSERT INTO "Test"."Test" VALUES ('{10, 20, 30}');
SELECT * FROM "Test"."Test" WHERE 20 = ANY ("Column1");
Run Code Online (Sandbox Code Playgroud)
索引是否有助于此查询?
我们有一个遗留数据库模式,有一些有趣的设计决策.直到最近,我们才支持Oracle和SQL Server,但我们正在尝试添加对PostgreSQL的支持,这引发了一个有趣的问题.我搜索了Stack Overflow和其他互联网,我不相信这种特殊情况是重复的.
对于唯一约束中的可空列,Oracle和SQL Server的行为都相同,这实际上是在执行唯一检查时忽略NULL列.
假设我有以下表格和约束:
CREATE TABLE EXAMPLE
(
ID TEXT NOT NULL PRIMARY KEY,
FIELD1 TEXT NULL,
FIELD2 TEXT NULL,
FIELD3 TEXT NULL,
FIELD4 TEXT NULL,
FIELD5 TEXT NULL,
...
);
CREATE UNIQUE INDEX EXAMPLE_INDEX ON EXAMPLE
(
FIELD1 ASC,
FIELD2 ASC,
FIELD3 ASC,
FIELD4 ASC,
FIELD5 ASC
);
Run Code Online (Sandbox Code Playgroud)
在Oracle和SQL Server上,保留任何可为空的列NULL将导致仅对非空列执行唯一性检查.所以以下插入只能执行一次:
INSERT INTO EXAMPLE VALUES ('1','FIELD1_DATA', NULL, NULL, NULL, NULL );
INSERT INTO EXAMPLE VALUES ('2','FIELD1_DATA','FIELD2_DATA', NULL, NULL,'FIELD5_DATA');
-- These will succeed when they should violate …Run Code Online (Sandbox Code Playgroud) 向Postgres表添加唯一约束时是否意味着该索引也已添加到该表中?
这意味着,如果我UNIQUE在text列上添加约束,那么该文本列现在是否具有索引,或者是否必须单独添加索引?
postgresql indexing database-design constraints unique-constraint
我core_message在 Postgres 中有一个表,有数百万行看起来像这样(简化):
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Colonne ? Type ? Collationnement ? NULL-able ? Par défaut ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? id ? integer ? ? not null ? nextval('core_message_id_seq'::regclass) ?
? mmsi ? integer ? ? not null ? ?
? time ? timestamp with time zone ? ? not null ? ?
? point ? geography(Point,4326) ? ? ? ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Index:
"core_message_pkey" PRIMARY KEY, btree (id)
"core_message_uniq_mmsi_time" UNIQUE CONSTRAINT, btree (mmsi, "time")
"core_messag_mmsi_b36d69_idx" btree (mmsi, "time" …Run Code Online (Sandbox Code Playgroud) sql postgresql indexing query-optimization greatest-n-per-group
在 Postgres 10 中我声明了以下内容:
create table test_abc (
pk integer not null,
id integer not NULL,
id2 integer not null,
PRIMARY KEY (pk)
);
CREATE UNIQUE INDEX test_abc_ids ON test_abc(id,id2);
Run Code Online (Sandbox Code Playgroud)
然后是第二个表,其中 FK 引用第一个表:
create table test_def (
id integer not null,
abc_id integer,
abc_id2 integer,
PRIMARY KEY (id),
FOREIGN KEY (abc_id,abc_id2) references test_abc(id,id2)
);
Run Code Online (Sandbox Code Playgroud)
现在考虑此查询的输出:
SELECT unique_constraint_catalog, unique_constraint_schema, unique_constraint_name
FROM information_schema.referential_constraints r
WHERE r.constraint_name = 'test_def_abc_id_fkey'
----------------------
NULL NULL NULL
Run Code Online (Sandbox Code Playgroud)
所有unique_constraint_*列都有空值。
从Postgres 文档看来这些元列应该包含
包含外键约束引用的唯一或主键约束的 [object] 的名称(始终是当前数据库) …
sql postgresql foreign-keys information-schema database-metadata
由于哈希值比冗长的文本小,在我看来,为了确保列的唯一性,它们可能比b树更受欢迎。
仅出于确保唯一性的目的,是否有任何理由在PG 10中不符合以下条件?
CREATE TABLE test (
path ltree,
EXCLUDE USING HASH ((path::text) WITH =)
);
Run Code Online (Sandbox Code Playgroud)
我认为哈希冲突是在内部处理的。否则将毫无用处。
我将使用GiST索引来增强查询。
postgresql ×6
indexing ×4
sql ×3
arrays ×1
constraints ×1
foreign-keys ×1
ltree ×1
null ×1
unique-index ×1