对多列唯一索引的Postgres查询需要比预期更长的时间

时间:2015-04-16 22:58:33

标签: postgresql postgresql-performance

我有一个包含2列的大表:

name::text
url::text

我在两列都有索引,我在(name, url上有另一个唯一索引。当我执行

select count(*) from t where name = 'foo'

select count(*) from t where url = 'bar'

分别需要14秒和27毫秒。但是当我执行合并查询时

select count(*) from t where url = 'bar' and name = 'foo'

需要84秒。它不应该花费大约27毫秒或者我错过了什么?

编辑:

以下是解释的输出:

Aggregate  (cost=5.77..5.78 rows=1 width=0)
    ->  Index Scan using ix_t_name on t  (cost=0.56..5.76 rows=1 width=0)
        Index Cond: ((name)::text = 'foo'::text)
        Filter: ((url)::text = 'bar'::text)"

编辑:

以下是explain analyze

的输出
Aggregate  (cost=5.77..5.78 rows=1 width=0) (actual time=402957.433..402957.433 rows=1 loops=1)
  ->  Index Scan using ix_t_name on t  (cost=0.56..5.76 rows=1 width=0) (actual time=1.635..402957.429 rows=1 loops=1)
        Index Cond: ((name)::text = 'foo'::text)
        Filter: ((url)::text = 'bar'::text)
        Rows Removed by Filter: 286544
Total runtime: 402957.490 ms

0 个答案:

没有答案