【发布时间】:2016-03-17 23:41:06
【问题描述】:
我正在开发一个应用程序来显示用户共享的内容,并且我正在使用 PostgreSQL 数组来处理安全模型。
我们支持公共和私人内容,我有两个查询需要优化。从 PostgreSQL 文档中,我需要在索引数组列时使用 GIN 索引,但我无法让 PostgreSQL 选择它们。
这是我的数据和索引定义:
-- Table: public.usershares
-- DROP TABLE public.usershares;
CREATE TABLE public.usershares
(
id integer NOT NULL,
title text,
sharedcontent text,
shared_on timestamp without time zone,
roles text[],
claims text[],
users integer[],
private boolean,
CONSTRAINT pk_id PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.usershares
OWNER TO postgres;
-- Index: public.idx_arrays_private
-- DROP INDEX public.idx_arrays_private;
CREATE INDEX idx_arrays_private
ON public.usershares
USING gin
(roles COLLATE pg_catalog."default", claims COLLATE pg_catalog."default", users)
WHERE private = true AND claims <> '{}'::text[];
-- Index: public.idx_arrays_public
-- DROP INDEX public.idx_arrays_public;
CREATE INDEX idx_arrays_public
ON public.usershares
USING gin
(roles COLLATE pg_catalog."default", users)
WHERE private = false AND claims = '{}'::text[];
-- Index: public.idx_sharedon
-- DROP INDEX public.idx_sharedon;
CREATE INDEX idx_sharedon
ON public.usershares
USING btree
(shared_on DESC);
-- Index: public.idx_sharedon_notprivate
-- DROP INDEX public.idx_sharedon_notprivate;
CREATE INDEX idx_sharedon_notprivate
ON public.usershares
USING btree
(shared_on DESC)
WHERE private = false;
-- Index: public.idx_sharedon_private
-- DROP INDEX public.idx_sharedon_private;
CREATE INDEX idx_sharedon_private
ON public.usershares
USING btree
(shared_on DESC)
WHERE private = true;
QUERY #1:虽然我对此查询“idx_arrays_private”有部分 GIN 索引,但 PostgreSQL 使用“idx_sharedon_private”(这也是部分索引,但不包括数组列(角色、声明和用户)
select *
from usershares
where
( usershares.private = true
and usershares.claims != '{}'
and ( ( array['adminX'] && usershares.roles /* to see private content user has to belong to one of the roles */
and array['managerX'] <@ usershares.claims ) /* and have all the required claims */
or array[]::integer[] && usershares.users /* or just be member of the list of authorized users */ ) )
order by shared_on desc
limit 100
offset 100;
QUERY #2:虽然我也有此查询“idx_arrays_public”的部分 GIN 索引,但 PostgreSQL 使用“idx_sharedon_notprivate”(这也是部分索引,但不包括数组列(角色、声明和用户) )
select *
from usershares
where
( usershares.private = false
and usershares.claims = '{}'
and ( array['admin'] && usershares.roles /* to see public content user has to belong to one of the roles */
or array[1,2,3,4,5] && usershares.users /* or be a member of the list of authorized users */
) )
order by shared_on desc
limit 100
offset 100;
生成测试数据的脚本:
TRUNCATE TABLE usershares;
INSERT INTO usershares
(id,
title,
sharedcontent,
shared_on,
roles,
claims,
users,
private)
SELECT x.id,
'title #'
|| x.id,
'content #'
|| x.id,
Now(),
array['admin','registered'],
'{}',
'{}',
false
FROM Generate_series(1, 500000) AS x(id);
INSERT INTO usershares
(id,
title,
sharedcontent,
shared_on,
roles,
claims,
users,
private)
SELECT x.id,
'title #'
|| x.id,
'content #'
|| x.id,
Now(),
array['admin','registered'],
array['manager', 'director'],
array[1,2,3,4,5,6,7,8,9,10],
true
FROM Generate_series(500001, 750000) AS x(id);
INSERT INTO usershares
(id,
title,
sharedcontent,
shared_on,
roles,
claims,
users,
private)
SELECT x.id,
'title #'
|| x.id,
'content #'
|| x.id,
Now(),
array['adminX','registeredX'],
array['managerX', 'directorX'],
'{}',
true
FROM Generate_series(750001, 1000000) AS x(id);
【问题讨论】:
标签: postgresql indexing