我想知道是否有办法在 PL/pgSQL 中声明表类型的变量来保存查询结果?例如,我如何表达如下内容:
q1 = select * from foo;
q2 = select * from bar;
for t1 in q1:
for t2 in q2:
-- do something with t1 and t2
Run Code Online (Sandbox Code Playgroud)
我查看了 return next 构造,但它似乎只能处理返回值。
我正在通过 Heroku 使用 Postgres 9.3。
我有一个表,“交通”,有 100 万条记录,每天都有很多插入和更新。我需要在不同的时间范围内跨该表执行 SUM 运算,这些调用最多可能需要 40 秒,我很想听听有关如何改进它的建议。
我在这张桌子上有以下索引:
CREATE INDEX idx_traffic_partner_only ON traffic (dt_created) WHERE campaign_id IS NULL AND uuid_self <> uuid_partner;
Run Code Online (Sandbox Code Playgroud)
这是一个示例 SELECT 语句:
SELECT SUM("clicks") AS clicks, SUM("impressions") AS impressions
FROM "traffic"
WHERE "uuid_self" != "uuid_partner"
AND "campaign_id" is NULL
AND "dt_created" >= 'Sun, 29 Mar 2015 00:00:00 +0000'
AND "dt_created" <= 'Mon, 27 Apr 2015 23:59:59 +0000'
Run Code Online (Sandbox Code Playgroud)
这是解释分析:
Aggregate (cost=21625.91..21625.92 rows=1 width=16) (actual time=41804.754..41804.754 rows=1 loops=1)
-> Index Scan using idx_traffic_partner_only on …Run Code Online (Sandbox Code Playgroud) postgresql performance index optimization postgresql-9.3 postgresql-performance
我在没有索引的 Postgres 表上有一个大型数据库导入(100GB)。
导入后,我想尽快为查询创建索引。只要索引未准备好,就不会访问表中的数据。
建立索引的最快方法是什么?我必须在 3 列(两列varchar,一列date)上建立索引。创建索引大约需要2个小时,这真的没有用。
有什么办法可以加快索引的创建吗?在数据导入之前设置索引可能不是更好,因为这会减慢导入速度?
我知道 Postgres 可以选择“不加锁”创建索引,但这不会降低性能,因为它使 Postgres 能够在创建索引时访问数据?
这是一个虚拟机。服务器的内存可以根据我的需要增加。目前我有32GB。work_mem并且maintenance_work_mem仍处于默认配置值。我没有用于数据库的 SSD。这肯定会加快速度吗?
我没有任何boolean可用于部分索引的标志或类似标志。事实上,我希望表中的每一行都被索引。
我觉得我需要json_object_agg()Postgres 9.4的功能,但我现在无法从 9.3 升级。有没有办法在 9.3 中做我想做的事?这是我的场景。我有一个click_activity看起来像的数据表
user | offer | clicks
-----|-------|--------
fred |coupons| 3
fred |cars | 1
john |coupons| 2
Run Code Online (Sandbox Code Playgroud)
但我想把它变成这个:(聚合每个用户的活动)
user | activity
-----|----------
fred | {"coupons": 3, "cars": 1}
john | {"coupons": 2}
Run Code Online (Sandbox Code Playgroud)
我认为json_object_agg()Postgres 9.4的功能可以完美地做到这一点,我只需要调用
select user, json_object_agg(offer, clicks) from click_activity group by 1
Run Code Online (Sandbox Code Playgroud)
有没有办法在 9.3 中做到这一点?谢谢!
我希望将大型 (100Mb -- 1 GB) 多通道时间序列数据导入 PostgreSQL 数据库。数据来自EDF 格式的文件,这些文件将数据分块成“记录”或“时期”,每个“记录”或“时期”通常为几秒钟。每个时期的记录将每个数据通道的信号保存为短整数的顺序数组。
我被要求将文件存储在数据库中,在最坏的情况下存储为 BLOB。鉴于此,我想研究可以让我对数据库中的数据做更多事情的选项,例如促进基于信号数据的查询。
我最初的计划是将数据存储为每个纪元记录一行。我想要权衡的是是否将实际信号数据存储为 bytea 或 smallint[](甚至 smallint[][])类型。有人可以推荐一个吗?我对存储和访问成本感兴趣。用法很可能是插入一次,偶尔读取,从不更新。如果一个更容易包装为自定义类型,以便我可以添加用于分析比较记录的函数,那就更好了。
毫无疑问,我缺乏细节,所以请随时对您希望我澄清的内容添加评论。
我balances在 PostgreSQL 9.3 中有一个表,如下所示:
CREATE TABLE balances (
user_id INT
, balance INT
, as_of_date DATE
);
INSERT INTO balances (user_id, balance, as_of_date) VALUES
(1, 100, '2016-01-03')
, (1, 50, '2016-01-02')
, (1, 10, '2016-01-01')
, (2, 200, '2016-01-01')
, (3, 30, '2016-01-03');
Run Code Online (Sandbox Code Playgroud)
它只包含用户进行交易的日期的余额。我需要它为每个用户包含一行以及给定日期范围内每个日期的余额。
我可以引用一个accounts表来获取用户的create_date:
CREATE TABLE accounts (
user_id INT
, create_date DATE
);
INSERT INTO accounts (user_id, create_date) VALUES
(1, '2015-12-01')
, (2, '2015-12-31')
, (3, '2016-01-03');
Run Code Online (Sandbox Code Playgroud)
我想要的结果是这样的:
+---------+---------+--------------------------+
| …Run Code Online (Sandbox Code Playgroud) 在托管 postgresql 数据库的 Ubuntu 机器中,我在/var/lib/postgresql/9.3/main/pg_log. 显然,几个月前的日志文件也被存储了。我手动删除了一些以摆脱我的disk full错误。每周删除旧的 postgresql 日志的可靠方法是什么?我可以做些什么postgresql.conf来自动化这个过程?
目前我有以下内容postgresql.conf:
#log_truncate_on_rotation = off # If on, an existing log file with the
# same name as the new log file will be
# truncated rather than appended to.
# But such truncation only occurs on
# time-driven rotation, not on restarts
# or size-driven rotation. Default is
# off, meaning append to existing files
# in all cases.
#log_rotation_age = 1d # …Run Code Online (Sandbox Code Playgroud) 有没有办法可视化哪些用户是 PostgreSQL / pgAdminIII 中某个角色的成员,例如
role: council_stuff
members:
Harry
Ben
Steve
Melinda
Run Code Online (Sandbox Code Playgroud) 似乎不允许向外部表添加外键约束。有没有其他方法可以做到这一点?我的两个表在远程服务器上有这些限制。
更具体的细节:
我真的只需要它作为注释,因为有些工具会查找 JOIN。
我在远程服务器上有两个表,我使用postgresql_fdw's将它们添加到本地CREATE FOREIGN TABLE。这些我在 PostgREST 中使用,自动 API 服务器使用 REFERENCES 信息进行连接。
实际上,我正在进一步增加两个CREATE MATERIALIZED VIEW以加快查找速度 - 但遗憾的是,他们也没有任何我知道的添加 REFERENCES 信息的方法?
我愿意使用其他方法在外部 API 服务器上快速(只读)查找这些表以及外键约束将在何处工作。
我有下表:
create table test (
company_id integer not null,
client_id integer not null,
client_status text,
unique (company_id, client_id)
);
insert into test values
(1, 1, 'y'), -- company1
(2, 2, null), -- company2
(3, 3, 'n'), -- company3
(4, 4, 'y'), -- company4
(4, 5, 'n'),
(5, 6, null), -- company5
(5, 7, 'n')
;
Run Code Online (Sandbox Code Playgroud)
基本上,有 5 家不同的公司,每家公司都有一个或多个客户,每个客户的状态为:“y”或“n”(也可能为空)。
我必须做的是为(company_id, client_id)至少有一个客户的状态不是“n”(“y”或 null)的所有公司选择所有对。所以对于上面的示例数据,输出应该是:
company_id;client_id
1;1
2;2
4;4
4;5
5;6
5;7
Run Code Online (Sandbox Code Playgroud)
我尝试了一些使用窗口函数的东西,但我无法弄清楚如何将所有客户端的数量与带有STATUS = 'n'.
select company_id,
count(*) over (partition …Run Code Online (Sandbox Code Playgroud) postgresql ×10
index ×2
performance ×2
blob ×1
bytea ×1
datatypes ×1
disk-space ×1
foreign-key ×1
group-by ×1
import ×1
json ×1
maintenance ×1
optimization ×1
pgadmin ×1
plpgsql ×1
remote ×1
role ×1