【发布时间】:2015-10-30 21:19:21
【问题描述】:
在我的网络应用中,我有一个简单的表格,每次增长超过 1200 万行。
+-----+-----+------+-------+--------+
| id | dtt | cus | event | server |
-------------------------------------
我正在使用此查询按客户统计今天的事件数
SELECT COUNT(*) FROM events
WHERE dtt AT TIME ZONE 'America/Santiago' >=date(now() AT TIME ZONE 'America/Santiago') + interval '1s'
AND cus=2
我的网络应用程序的性能非常糟糕:22702 毫秒。
"Aggregate (cost=685814.54..685814.55 rows=1 width=0) (actual time=21773.451..21773.452 rows=1 loops=1)"
" -> Seq Scan on events (cost=0.00..675644.52 rows=4068008 width=0) (actual time=10277.508..21732.548 rows=409808 loops=1)"
" Filter: ((cus = 2) AND (timezone('America/Santiago'::text, dtt) >= (date(timezone('America/Santiago'::text, now())) + '00:00:01'::interval)))"
" Rows Removed by Filter: 12077798"
"Planning time: 0.127 ms"
"Execution time: 21773.509 ms"
我创建了下一个索引:
CREATE INDEX events_dtt_idx
ON events
USING btree
(dtt);
CREATE INDEX events_id_desc
ON events
USING btree
(id DESC NULLS LAST);
CREATE INDEX events_cus_idx
ON events
USING btree
(cus);
CREATE INDEX events_id_idx
ON events
USING btree
(id);
使用 Postgresql 9.4,Linux x64
我该如何改进呢?提前致谢。
【问题讨论】:
-
如果你要在
dtt AT TIME ZONE 'America/Santiago'上创建索引呢? -
功能索引应该可以工作 - 唯一的风险是略微超出估计。
标签: postgresql