我正在尝试查询包含100M行的表上的聚合数据:
CREATE TABLE analytics (
event varchar(40),
data varchar(256),
id varchar(32),
date timestamp with time zone
);
我希望能够为特定ID运行选择一系列数据(或聚合):
SELECT COUNT(*) FROM analytics
WHERE id = '01234567890123456789012345678912'
AND date >= '2016-01-01'
AND date < '2016-02-01';
上面的查询计算了大约500k行并且在没有索引的情况下在80s内运行。我想让它更快,理想情况下将它们降低到&lt; 10秒。我尝试了几个不同的指数:
/* increases runtime to 400s */
CREATE INDEX analytics_id_index ON analytics (id);
/* increases runtime to 160s */
CREATE INDEX analytics_id_date_index ON analytics (id, date);
/* no difference: queries still take 80s */
CREATE INDEX date_as_date_id_index ON analytics (DATE(date AT TIME ZONE 'UTC'), id);
/* queries take 14m (!) */
CREATE INDEX analytics_id_index ON analytics (id);
CREATE INDEX analytics_date_index ON analytics (date);
我的问题是:
谢谢!