作为 Postgres 的新手用户,ad我的 PostgreSQL 9.5 (x64) 数据库中有一个 87 行的表。除了其他列之外,它还有两列“开始”和“结束”,其日期时间范围如下:
ID Start End
1 2003-06-07 00:00:00 2004-09-30 23:59:59
Run Code Online (Sandbox Code Playgroud)
我需要将范围拆分为存储在数据库中的单独行(从开始年份到间隔的最后一年)中的一年窗口,如下所示:
ID Start
1_2003 2003-06-07 00:00:00 2003-12-31 23:59:59
1_2004 2004-01-01 00:00:00 2004-09-30 23:59:59
Run Code Online (Sandbox Code Playgroud)
使用运算符||'_'||和Extract()函数,我可以将 ID 与年份连接起来。此外,这个问题解决了如何分割的间隔周,这个节目怎么好几天做相同的,但他们没有解决如何在专门年分裂的间隔。
我避免了这个问题,因为我不想采用基于存储过程的方法。我知道generate_series()从开始和停止参数返回一个系列,但实际上我正在努力打破一年的最后一天的间隔,然后从下一行的第一天重新开始。如果有人能指导我做到这一点,我将不胜感激?
我有一个表出席如下:
create table attendance (id int, name varchar(30), status varchar(10), date date)
Run Code Online (Sandbox Code Playgroud)
该表有以下记录:
insert into attendance values (1,'John','absent','2016-03-01');
insert into attendance values (1,'John','absent','2016-03-02');
insert into attendance values (1,'John','absent','2016-03-03');
insert into attendance values (2,'Sam','present','2016-03-04');
insert into attendance values (3,'Sam','absent','2016-03-05');
insert into attendance values (1,'John','absent','2016-03-06');
insert into attendance values (1,'John','absent','2016-03-07');
insert into attendance values (1,'John','absent','2016-03-08');
insert into attendance values (1,'John','present','2016-03-09');
insert into attendance values (1,'John','absent','2016-03-10');
insert into attendance values (1,'John','absent','2016-03-11');
insert into attendance values (1,'John','present','2016-03-12');
insert into attendance values (1,'John','absent','2016-03-13');
Run Code Online (Sandbox Code Playgroud)
现在我要计算一个人连续三个缺席记录的次数。
结果应如下所示: …
给定一个vp具有列timestamp类型的表bigint和一个btree索引timestamp,为什么 Postgres 会忽略索引并在timestamp与浮点值比较时运行 seq 扫描,当索引扫描会产生相同的结果时?
SELECT * FROM vp WHERE vp.timestamp > 1470752584需要48 毫秒:
在 vp 上使用 vp_ts_idx 进行索引扫描(cost=0.57..257.87 rows=2381 width=57)(实际时间=0.014..38.669 rows=80323 loops=1) 索引条件:(“时间戳”> 1470752584) 总运行时间:48.322 毫秒
SELECT * FROM vp WHERE vp.timestamp > 1470752584.1需要103 秒,因为它忽略vp_ts_idx并执行整个表的 seq 扫描:
vp 上的 Seq Scan (cost=0.00..7378353.16 rows=95403915 width=57) (实际时间=62625.420..103122.701 rows=98240 loops=1)
过滤器:(("timestamp")::numeric > 1470752584.1)
过滤器删除的行:285945491
总运行时间:103134.333 毫秒
背景:相比于最近的车辆位置查询timestamp用EXTRACT(EPOCH …
postgresql performance execution-plan postgresql-9.3 query-performance
我有需要wal_keep_segments在我们的主服务器上增加。我可以即时执行此操作还是需要重新启动?
PostgreSQL 文档列出了许多提供当前连接状态的系统信息函数,例如inet_client_addr()或pg_backend_pid()。我想为包含 的表的更新创建审计跟踪application_name,但似乎没有访问它的功能。
该application_name是从可见pg_stat_activity视图,并通过客户的格式设置
username:progname@hostname
Run Code Online (Sandbox Code Playgroud)
如何从触发器访问这些信息?
我很好奇这个语句是如何在 Postgres 中更新 3 行的。在我运行它的所有其他时间,它会更新 0 或 1。有没有办法找出哪些行?
bestsales=# update keyword set revenue = random()*10 where id = cast(random()*99999 as int);
UPDATE 3
Run Code Online (Sandbox Code Playgroud)
id 是主键。
id | integer | not null default nextval('keyword_id_seq'::regclass)
"keyword_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)
我尝试将其运行为SELECT:
bestsales=# select * from keyword where id = cast(random()*99999 as int);
id | keyword | seed_id | source | search_count | country | language | volume | cpc | competition | modified_on | google_violation | revenue | bing_violation
-------+---------------------+---------+--------+--------------+---------+----------+--------+------+-------------+-------------+------------------+---------+---------------- …Run Code Online (Sandbox Code Playgroud) 在 Postgres 中,我试图USAGE将公共模式授予特定角色 -
GRANT ALL ON SCHEMA public TO MyRole;
Run Code Online (Sandbox Code Playgroud)
问题是这个命令产生了一个错误——
错误:角色“myrole”不存在
然而,这个角色确实存在。列出角色输出下表 -
Role name | Attributes | Member of
---------------+------------------------------------------------------------+-----------------
rds_user | Create role, Create DB +| {rds_superuser}
| Password valid until infinity |
MyRole | | {}
rds_superuser | Cannot login | {}
rdsadmin | Superuser, Create role, Create DB, Replication, Bypass RLS+| {}
| Password valid until infinity |
rdsrepladmin | No inheritance, Cannot login, Replication | {}
Run Code Online (Sandbox Code Playgroud)
我是 Postgres 的新手,所以我可能会遗漏一些微妙的命令,但据我所知,我已经完成了我需要做的一切。任何人都可以建议问题可能是什么以及我应该如何解决它?
如果我在同一列上创建两个(或多个)不同类型的索引,PostgreSQL 的行为如何?
就我而言,我想将 B 树索引与tsvector列上的 GIN 进行比较。我知道 GIN 专门用于tsvector. 奇怪的是,如果我创建 B 树索引,PostgreSQL 不会抱怨,但我看到查询计划器不使用它。我还可以创建 GIN 索引(不删除 B 树索引),现在规划器使用新创建的索引。该列现在有两个索引,但只使用了其中一个。
即使存在两个以上的索引,用于选择索引类型的标准是什么?为什么 PostgreSQL 不告诉我 a 上的 B 树索引tsvector是无用的,并且不会永远不会被计划者使用?
更新
GIN 索引仅用于某些检查条件,例如my_tsvector IS NOT NULL但(显然)不适用于my_tsvector @@ '...'::tsquery.
我有一个包含日期时间字段start和end. 我有一个(开始,结束)项目列表。我需要检查列表中的哪些项目与表中的数据重叠。当前查询如下所示:
select br.duration from booking, (
select tstzrange('2016-09-06 03:45:00+00', '2016-09-06 14:45:00+00') as duration
union select tstzrange('2016-09-06 14:45:00+00', '2016-09-06 15:45:00+00') as duration
-- other items from my list
) as br
where tstzrange(start, end) && br.duration
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到?如果我在表中有数百万行并将它们与列表中的数百个项目进行比较,您认为它会起作用吗?
postgresql performance gist-index range-types postgresql-performance
假设您有一个具有以下定义的表:
CREATE TABLE public.positions
(
id serial,
latitude numeric(18,12),
longitude numeric(18,12),
updated_at timestamp without time zone
)
Run Code Online (Sandbox Code Playgroud)
您在此表中有 50,000 行。现在出于测试目的,您将运行如下更新:
update positions
set updated_at = now()
where latitude between 234.12 and 235.00;
Run Code Online (Sandbox Code Playgroud)
该语句将从 50,000 行(在此特定数据集中)更新 1,000 行。
如果您在 30 个不同的线程中运行这样的查询*,MySQL innodb 将成功,而 PostgreSQL 将因大量死锁而失败。
为什么?
* 我正在比较最新版本的 MySQL innodb 与 Postgres,这是一个并发更新案例。生产案例:想象有 5000 只库存不断更新,最新价格不断可用。
postgresql ×10
performance ×2
datetime ×1
deadlock ×1
gist-index ×1
index ×1
innodb ×1
mysql ×1
operator ×1
random ×1
range-types ×1
schema ×1
update ×1