问题描述:Postgres的跨模型的查询使用UNION ALL
我创建的Postgres一个数据库,它有许多模式,比如:
s1,s2,s3, ..... s1000
每个模式已创建一个表如:
CREATE TABLE s1.demos
(
id bigint NOT NULL DEFAULT s1.next_demos_id(), -- 分片Id
title character varying(255) NOT NULL, -- 标题
CONSTRAINT demos_pkey PRIMARY KEY (id)
)
CREATE TABLE s2.demos
(
id bigint NOT NULL DEFAULT s2.next_demos_id(), -- 分片Id
title character varying(255) NOT NULL, -- 标题
CONSTRAINT demos_pkey PRIMARY KEY (id)
)
我执行的是SQL使用UNION ALL选择两种模式的数据,如:
(SELECT *
FROM s2.demos
ORDER BY id ASC
LIMIT 3)
UNION ALL (SELECT *
FROM s1.demos
ORDER BY id ASC
LIMIT 3)
ORDER BY id DESC
它的结果是:
psql (9.5.0)
Type "help" for help.
migrate_development=# (SELECT * FROM s2.demos ORDER BY id ASC LIMIT 3) UNION ALL (SELECT * FROM s1.demos ORDER BY id ASC LIMIT 3) ORDER BY id DESC;
id | title
----+----------------
21 | s2->1 's title
13 | s1->3 's title
12 | s1->2 's title
11 | s1->1 's title
10 | s2->3 's title
9 | s2->4 's title
(6 rows)
migrate_development=#
两个演示表的数据,如:
migrate_development=# SELECT * FROM s1.demos;
id | title
----+----------------
11 | s1->1 's title
12 | s1->2 's title
13 | s1->3 's title
14 | s1->4 's title
15 | s1->5 's title
(5 rows)
migrate_development=# SELECT * FROM s2.demos;
id | title
----+----------------
21 | s2->1 's title
22 | s2->2 's title
10 | s2->3 's title
9 | s2->4 's title
25 | s2->5 's title
(5 rows)
migrate_development=#
问题
我知道我可以通过UNION ALL得到的所有模式的数据1000架构,但我想要其他方法?
PS。我也用Sphinx解决,但,它的另一项服务,任何其他的敏捷方法
我已经使用'inherit'如:'alter table s1.demos inherit public.demos; alter table s2.demos inherit public.demos;''选择tableoid,* from public.demos' – Fish
insert,update,delete on child table direct。没有任何性能损失。但是如果在父表上插入UPDATE DELETE,并使用触发器,规则重定向行到子表,则会降低性能。 – Fish