我有两个表通过公共ID列链接,如下所示:
CREATE TABLE IF NOT EXISTS names (
uid BIGSERIAL,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (uid)
);
CREATE TABLE IF NOT EXISTS texts (
name_uid BIGINT NOT NULL REFERENCES names,
timestamp TIMESTAMP NOT NULL,
some_value TEXT NULL
);
在这里我们有一些数据可供使用:
INSERT INTO names VALUES ( 0, '1/a' );
INSERT INTO names VALUES ( 1, '1/b' );
INSERT INTO names VALUES ( 2, '2/c' );
INSERT INTO names VALUES ( 3, '3/d' );
INSERT INTO names VALUES ( 4, '3/e' );
INSERT INTO names VALUES ( 5, '3/f' );
INSERT INTO texts VALUES ( 0, '2018-01-01 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 1, '2018-01-02 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 2, '2018-02-01 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 2, '2018-02-02 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 3, '2018-03-01 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 3, '2018-06-01 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 4, '2018-06-02 00:00:00', 'text...' );
INSERT INTO texts VALUES ( 5, '2018-06-03 00:00:00', 'text...' );
我现在需要的是应用以下逻辑规则
这可以通过以下查询来实现:
SELECT substring(names.name, '[^/]+' ) AS name_prefix, COALESCE( sum( text_counts.count ), 0) AS counter, max(text_timestamps.timestamp) AS timestamp
FROM names
LEFT JOIN (
SELECT texts.name_uid, count(*)
FROM texts
WHERE texts.timestamp > '2018-05-01 00:00:00'
GROUP BY texts.name_uid
) text_counts ON text_counts.name_uid = names.uid
LEFT JOIN(
SELECT texts.name_uid, max(texts.timestamp) AS timestamp
FROM texts
GROUP BY texts.name_uid
) text_timestamps ON text_timestamps.name_uid = names.uid
WHERE names.name SIMILAR TO '1%|3%'
GROUP BY name_prefix
但是,此查询速度很慢。所以我试图提出一个更好的解决方案,但到目前为止失败了。我得到的是:
SELECT name_info.name_prefix, count(*) AS counter, max(timestamp) AS timestamp
FROM texts
RIGHT JOIN (
SELECT names.uid, substring(names.name, '[^/]+' ) AS name_prefix
FROM names
WHERE names.name SIMILAR TO '1%|3%'
) name_info ON name_info.uid = texts.name_uid
WHERE texts.timestamp > '2018-05-01 00:00:00'
GROUP BY name_info.name_prefix
与第一种解决方案相比,这是非常快的。问题是,现在结果中缺少计数为零的行。
我现在的问题是如何创建一个提供接近查询2的性能的查询,但是在结果中包含计数为零的行
一些上下文信息:我正在使用PostgreSQL 10,表文本的行数比表名大一百万倍。实际上,文本甚至在现实世界中被分区,但我决定在此处跳过这个例子。
答案 0 :(得分:1)
由于WHERE
子句中的时间戳条件,第二个查询中的右连接就像内连接一样。删除条件并使用count(*)
聚合FILTER
:
SELECT
name_info.name_prefix,
count(*) FILTER (WHERE texts.timestamp > '2018-05-01 00:00:00') AS counter,
max(timestamp) AS timestamp
FROM texts
RIGHT JOIN (
SELECT names.uid, substring(names.name, '[^/]+' ) AS name_prefix
FROM names
WHERE names.name SIMILAR TO '1%|3%'
) name_info ON name_info.uid = texts.name_uid
GROUP BY name_info.name_prefix;
您还可以尝试两阶段分组,例如:
select
name_prefix,
sum(counter) as counter,
max(timestamp) as timestamp
from (
select
substring(name, '[^/]+' ) as name_prefix,
sum((timestamp > '2018-05-01 00:00:00')::int) as counter,
max(timestamp) as timestamp
from texts
join names on name_uid = uid
where name similar to '1%|3%'
group by uid
) s
group by name_prefix