我有一个data
类型的列,json
其中包含这样的 JSON 文档:
{
"name": "foo",
"tags": ["foo", "bar"]
}
Run Code Online (Sandbox Code Playgroud)
我想将嵌套tags
数组转换为连接字符串 ( 'foo, bar'
)。array_to_string()
从理论上讲,使用该函数很容易做到这一点。但是,此功能不接受json
输入。所以我想知道如何将这个 JSON 数组变成 Postgres 数组(类型text[]
)?
我在 Ubuntu 上使用 PostgreSQL 9.1。VACUUM ANALYZE
仍然推荐预定,还是 autovacuum 足以满足所有需求?
如果答案是“视情况而定”,那么:
我问是因为预定的时间VACUUM ANALYZE
会影响我的报告。它运行了 5 个多小时,本周我不得不杀死它两次,因为它影响了常规的数据库导入。check_postgres
不会报告数据库有任何显着膨胀,所以这不是真正的问题。
从文档中,autovacuum 也应该处理事务 ID 环绕。问题是:我还需要一个VACUUM ANALYZE
吗?
在多对多关系上使用 join 时,结果会拆分为多行。我想做的是将连接的右侧转换为数组,因此结果是一行。
包含 3 个表的示例:
CREATE TABLE items (
id serial primary key,
title text
);
CREATE TABLE tags (
id serial primary key,
title text
);
CREATE TABLE items_tags (
item_id int references items(id),
tag_id int references tags(id),
primary key (item_id, tag_id)
);
Run Code Online (Sandbox Code Playgroud)
选择带有标签的项目时,我可以这样做:
SELECT i.id, i.title, i.title
FROM items i
INNER JOIN items_tags it
ON it.item_id = i.id
INNER JOIN tags t
ON t.id = it.tag_id;
Run Code Online (Sandbox Code Playgroud)
结果将显示为:
(1, "item n1", "sport")
(1, "item n1", "soccer")
(2, "item …
Run Code Online (Sandbox Code Playgroud) I have the following tables (taken from the Sakila database):
I am selecting a particular film. For this film, I also want all actors participating in that film. I have two queries for this: one with a LEFT JOIN
and one with a LEFT JOIN LATERAL
.
select film.film_id, film.title, a.actors
from film
left join
(
select film_actor.film_id, array_agg(first_name) as actors
from actor
inner …
Run Code Online (Sandbox Code Playgroud) postgresql performance join execution-plan postgresql-10 postgresql-performance
我的问题的小提琴可以在https://dbfiddle.uk/?rdbms=postgres_10&fiddle=3cd9335fa07565960c1837aa65143685上找到。
我有一个简单的表格布局:
class
person: belongs to a class
Run Code Online (Sandbox Code Playgroud)
我想选择所有班级,对于每个班级,我想要按降序排列的所属人员的前两个人员标识符。
我通过以下查询解决了这个问题:
select c.identifier, array_agg(p.identifier order by p.name desc) as persons
from class as c
left join lateral (
select p.identifier, p.name
from person as p
where p.class_identifier = c.identifier
order by p.name desc
limit 2
) as p
on true
group by c.identifier
order by c.identifier
Run Code Online (Sandbox Code Playgroud)
注意:我可以在SELECT
子句中使用相关子查询,但作为学习过程的一部分,我试图避免这种情况。
如您所见,我order by p.name desc
在两个地方申请:
有没有办法避免这种情况?我的坚持:
首先,显然我不能删除order by
子查询中的 ,因为这会给出一个不符合我上述要求的查询。
其次,我认为order by
聚合函数中的 不能被遗漏,因为子查询的行顺序不一定保留在聚合函数中?
我应该重写查询吗?
简介: 我有一个简单的数据库模式,但即使只有几十条记录,基本查询的性能也已经成为一个问题。
数据库:PostgreSQL 9.6
简化架构:
CREATE TABLE article (
id bigint PRIMARY KEY,
title text NOT NULL,
score int NOT NULL
);
CREATE TABLE tag (
id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE article_tag (
article_id bigint NOT NULL REFERENCES article (id),
tag_id bigint NOT NULL REFERENCES tag (id),
PRIMARY KEY (article_id, tag_id)
);
CREATE INDEX ON article (score);
Run Code Online (Sandbox Code Playgroud)
生产数据信息:
所有表都是读/写的。写入量低,每几分钟左右只有一个新记录。
大概记录数:
每篇文章平均 5 个标签。
问题 …
我(key, value)
将成对存储在两个不同的表中 -company_settings
和branch_settings
. Akey
可以不存在于表中,也可以存在于其中一个或两个表中。
如果key
两个表中都存在,我想使用value
存储在branch_settings
表中的。否则,如果可用,我想回退到value
存储在company_settings
表中。
company_settings
-------------------------
| key | value
-------------------------
| key.A | 4 |
-------------------------
| key.B | 5 |
-------------------------
branch_settings
-------------------------
| key | value
-------------------------
| key.A | 1 |
-------------------------
Run Code Online (Sandbox Code Playgroud)
因此,如果我查询key.A
结果应该是1
因为key.A
两个表中都存在,并且我想为branch_settings
表中的记录提供更高的优先级。
如果我查询key.B
结果应该是5
因为key.B
只存在于company_settings
表中。
当特定图书馆中没有书籍数据时,就会出现问题。考虑以下工作场景。
桌子 library
--------------------------------
| id | name | owner |
--------------------------------
| 1 | ABC | A |
| 2 | DEF | D |
| 3 | GHI | G |
--------------------------------
Run Code Online (Sandbox Code Playgroud)
桌子 books
--------------------------------
| id | title | library |
--------------------------------
| a | xxx | 1 |
| b | yyy | 1 |
| c | zzz | 2 |
--------------------------------
Run Code Online (Sandbox Code Playgroud)
现在,当我进行如下查询时:
SELECT library.name, array_agg(b.title) AS book_list FROM library,
(SELECT title FROM books WHERE …
Run Code Online (Sandbox Code Playgroud) 我有诸如role1
、role2
、role3
等列。它们都是布尔值。
我想在此表上创建一个视图,该视图具有类型为 的角色列text[]
。如果有列TRUE, FALSE, TRUE
,则视图将包含["role1", "role3"]
.
有什么好的方法可以做到这一点,并且不会爆炸成大量的CASE WHEN
\xc2\xb4s 吗?澄清一下,我可以使用 O(n) CASE WHEN
,但不能使用 O(2^n) ,这是目前似乎需要的。:)
我必须更新一个bigint[]
名为permissao_ver
.
示例值:'{1,2,3,4,5,11,44,56,75,11}'
.
伪代码:
UPDATE callcenter.pausa
SET permissao_ver = '{"(SELECT cod_grupo FROM crm.usuariosgrupos
WHERE habilitar = 1)"}'::bigint[]
WHERE habilitado = 1 AND permissao_ver is null
Run Code Online (Sandbox Code Playgroud)
我需要获取上面选择的代码将以与bigint[]
列匹配的格式返回。喜欢:'{XX, XX, XX, X, XX, X}'
。
postgresql ×10
array ×5
join ×3
aggregate ×2
performance ×2
coalesce ×1
etl ×1
json ×1
order-by ×1
paging ×1
sqlalchemy ×1
subquery ×1
unpivot ×1
update ×1
vacuum ×1