sql - postgres跨模式查询使用UNION ALL

标签 sql postgresql schema sharding

问题描述:

我在 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要解决,但是,这是另一个服务,还有其他敏捷方法吗?

最佳答案

您可以使用inheritance postgresql feature

-- create schema for base table and table itself
CREATE SCHEMA s;
CREATE SEQUENCE demos_id_seq START 1;

CREATE TABLE s.demos
(
  id bigint NOT NULL DEFAULT nextval('demos_id_seq'),
  title character varying(255) NOT NULL,
  table_schema text NOT NULL, 
  CONSTRAINT demos_pkey PRIMARY KEY (id)
);

-- create descendant table 1
CREATE SCHEMA s1;
CREATE TABLE s1.demos (
  table_schema text NOT NULL DEFAULT 's1'
) INHERITS (s.demos);

-- create descendant table 1
CREATE SCHEMA s2;
CREATE TABLE s2.demos (
  table_schema text NOT NULL DEFAULT 's2'
) INHERITS (s.demos);

-- create descendant table 1
CREATE SCHEMA s3;
CREATE TABLE s3.demos (
  table_schema text NOT NULL DEFAULT 's3'
) INHERITS (s.demos);

-- can add table to s1000.demos

-- insert some data for test queries
insert into s1.demos (title) VALUES ('s1.1'), ('s1.2'), ('s1.3'), ('s1.4'), ('s1.5');
insert into s2.demos (title) VALUES ('s2.1'), ('s2.2'), ('s2.3'), ('s2.4'), ('s2.5');
insert into s3.demos (title) VALUES ('s3.1'), ('s3.2'), ('s3.3'), ('s3.4'), ('s3.5');

-- query for 3th top row from each descendant table
SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (PARTITION BY table_schema ORDER BY id asc) AS r,
    t.*
  FROM
    s.demos t
) x WHERE x.r <= 3;

此查询返回此输出:

     r | id | title | table_schema 
---+----+-------+--------------
 1 |  1 | s1.1  | s1
 2 |  2 | s1.2  | s1
 3 |  3 | s1.3  | s1
 1 |  6 | s2.1  | s2
 2 |  7 | s2.2  | s2
 3 |  8 | s2.3  | s2
 1 | 11 | s3.1  | s3
 2 | 12 | s3.2  | s3
 3 | 13 | s3.3  | s3
(9 rows)

关于sql - postgres跨模式查询使用UNION ALL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811093/

相关文章:

sql - 关于 "reverse LIKEs"的MySQL问题

sql - 使用 NOT IN 和 NULL 处理查询

php - 使用 php 爆炸数组作为 WHERE 的 MySQL 查询

ruby - 最佳批量大小 PostgreSQL 更新

sql - 如何在 postgresql 中存储和搜索连字符/非连字符的 url 名称?

为多个供应商发现 sql 数据库模式的 C++ 库?

mysql - 检查 where 子句中的不同条件出现错误

sql - 在postgresql中灵活游泳

database - Scala slick 检查 DDL 表和数据库模式表是否具有相同的形状

xml - 对 XML 架构中的时间属性应用限制