【问题标题】:Query too slow in Postgresql in table with > 12M rows在具有 > 12M 行的表中的 Postgresql 中查询太慢
【发布时间】: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


【解决方案1】:

类似:

CREATE INDEX dtt_tz_idx ON  events (DATE(dtt AT TIME ZONE 'America/Santiago'));

然后查询

SELECT COUNT(*)  FROM events
    WHERE DATE(TIMEZONE('America/Santiago'::text, dtt)) >=date(now() AT TIME ZONE 'America/Santiago') + interval '1s' 
    AND cus=2

如果不起作用,请在 psql 中尝试“\d dtt_tz_idx”,并尝试将查询中的数据类型与索引匹配。

【讨论】:

  • 那个查询得到 0 个匹配,查询有问题,原始查询得到 537596 个匹配。
  • 问题是第一个DATE()函数,如果我去掉第一个DATE函数,性能还是一样,没有任何改善。
  • 您是否检查过您的索引和查询的数据类型是否匹配?
  • 是的,但是您的答案中的问题是其他问题,查询,然后索引不起作用,我可以找到解决方案并发布答案,无论如何,谢谢。
【解决方案2】:

我终于可以解决该索引的问题了:

CREATE INDEX dtt_tz_idx ON  events (TIMEZONE('America/Santiago'::text, dtt));

感谢 sivan 和 vyegorov 的指导,现在计划是:

"Aggregate  (cost=567240.43..567240.44 rows=1 width=0) (actual time=238.440..238.440 rows=1 loops=1)"
"  ->  Bitmap Heap Scan on events  (cost=82620.28..556463.97 rows=4310584 width=0) (actual time=41.445..208.870 rows=344453 loops=1)"
"        Recheck Cond: (timezone('America/Santiago'::text, dtt) >= (date(timezone('America/Santiago'::text, now())) + '00:00:01'::interval))"
"        Filter: (cus = 2)"
"        Rows Removed by Filter: 9433"
"        Heap Blocks: exact=9426"
"        ->  Bitmap Index Scan on dtt_tz_idx  (cost=0.00..81542.63 rows=4415225 width=0) (actual time=38.866..38.866 rows=353886 loops=1)"
"              Index Cond: (timezone('America/Santiago'::text, dtt) >= (date(timezone('America/Santiago'::text, now())) + '00:00:01'::interval))"
"Planning time: 0.221 ms"
"Execution time: 238.509 ms"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多