我有一个包含日期时间字段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
我有events包含字段的表:
id
user_id
time_start
time_end
...
Run Code Online (Sandbox Code Playgroud)
并在 上有 B 树索引(time_start, time_end)。
SELECT user_id
FROM events
WHERE ((time_start <= '2021-08-24T15:30:00+00:00' AND time_end >= '2021-08-24T15:30:00+00:00') OR
(time_start <= '2021-08-24T15:59:00+00:00' AND time_end >= '2021-08-24T15:59:00+00:00'))
GROUP BY user_id);
Run Code Online (Sandbox Code Playgroud)
Group (cost=243735.42..243998.32 rows=1103 width=4) (actual time=186.533..188.244 rows=166 loops=1)
Group Key: user_id
Buffers: shared hit=224848
-> Gather Merge (cost=243735.42..243992.80 rows=2206 width=4) (actual time=186.532..188.199 rows=176 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=224848
-> Sort (cost=242735.39..242738.15 rows=1103 width=4) (actual time=184.121..184.126 rows=59 loops=3) …Run Code Online (Sandbox Code Playgroud) postgresql index range-types query-performance postgresql-performance
我在搭建 PostgreSQL 数据库时遇到问题,因为 npgsql 不能很好地处理同一列上有两个索引的表。这是我在 github 上的项目的链接。
我正在尝试查找这些索引/表,以便可以将它们从脚手架命令中排除。
例子:
CREATE TABLE public.altreproc (
apcod integer NOT NULL,
aptip character(3),
apcam character(3),
CONSTRAINT altreproc_pkey PRIMARY KEY (apcod)
);
CREATE INDEX ialtreproc1
ON public.altreproc
USING btree
(aptip COLLATE pg_catalog."default");
CREATE INDEX ialtreproc2
ON public.altreproc
USING btree
(apcam COLLATE pg_catalog."default");
CREATE INDEX ialtreproc3
ON public.altreproc
USING btree
(aptip COLLATE pg_catalog."default" DESC);
Run Code Online (Sandbox Code Playgroud)
我的问题是当一个表有 2 个不同的索引与相同的列(一个ASC和一个DESC)时;我试图在我试图搭建的数据库中查询这样的表/索引。
我的目标是找到在同一列上包含多个索引的表。
从表中获取第二高值已经解决了很多次,但我正在寻找每组中的第二高值。
鉴于此表:
+----+-----+
| A | 10 |
| A | 20 |
| A | 35 | <-- This record
| A | 42 |
| B | 12 |
| B | 21 | <-- This record
| B | 33 |
| C | 14 |
| C | 23 |
| C | 38 |
| C | 41 | <-- This record
| C | 55 |
+----+-----+
Run Code Online (Sandbox Code Playgroud)
我想获得标记的行。
伪代码:
select col_a, penultimate(col_b) …Run Code Online (Sandbox Code Playgroud) postgresql ×4
index ×2
range-types ×2
gist-index ×1
max ×1
metadata ×1
performance ×1
select ×1