database - Postgresql:内部连接需要 70 秒

标签 database performance postgresql postgresql-performance

我有两个表 -

表 A:1MM 行, AsOfDate, Id, BId(表B的外键)

表 B:5 万行, Id、标志、ValidFrom、ValidTo

表 A 在 2011/01/01 和 2011/12/31 之间每天包含 100 个 BId 的多条记录。 表 B 包含 100 个投标的多个非重叠(在 validfrom 和 validto 之间)记录。

连接的任务是返回在给定的 AsOfDate 为 BId 激活的标志。

select 
    a.AsOfDate, b.Flag 
from 
    A a inner Join B b on 
        a.BId = b.BId and b.ValidFrom <= a.AsOfDate and b.ValidTo >= a.AsOfDate
where
    a.AsOfDate >= 20110101 and a.AsOfDate <= 20111231

在具有 64Gb 内存的超高端服务器 (+3Ghz) 上,此查询需要大约 70 秒。

我在测试时对每个字段组合都有索引 - 但无济于事。

索引:a.AsOfDate, a.AsOfDate+a.bId, a.bid 索引:b.bid, b.bid+b.validfrom

还尝试了下面建议的范围查询(62 秒)

在 VM 中运行的免费版 Sql Server 上的相同查询需要约 1 秒才能完成。

有什么想法吗?

Postgres 9.2

查询计划

QUERY PLAN                                       
---------------------------------------------------------------------------------------
Aggregate  (cost=8274298.83..8274298.84 rows=1 width=0)
->  Hash Join  (cost=1692.25..8137039.36 rows=54903787 width=0)
    Hash Cond: (a.bid = b.bid)
     Join Filter: ((b.validfrom <= a.asofdate) AND (b.validto >= a.asofdate))
     ->  Seq Scan on "A" a  (cost=0.00..37727.00 rows=986467 width=12)
           Filter: ((asofdate > 20110101) AND (asofdate < 20111231))
     ->  Hash  (cost=821.00..821.00 rows=50100 width=12)
           ->  Seq Scan on "B" b  (cost=0.00..821.00 rows=50100 width=12)

参见 http://explain.depesz.com/s/1c5用于分析输出

here is the query plan from sqlserver for the same query

最佳答案

考虑使用 postgresql 9.2 中可用的范围类型:

create index on a using gist(int4range(asofdate, asofdate, '[]'));
create index on b using gist(int4range(validfrom, validto, '[]'));

您可以像这样查询匹配范围内的日期:

select * from a
where int4range(asofdate,asofdate,'[]') && int4range(20110101, 20111231, '[]');

对于 b 中的行与 a 中的记录重叠,如下所示:

select *
from b
    join a on int4range(b.validfrom,b.validto,'[]') @> a.asofdate
where a.id = 1

(&&表示“重叠”,@>表示“包含”,'[]'表示创建一个包含的范围两个端点)

关于database - Postgresql:内部连接需要 70 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13088407/

相关文章:

sql - 必须在存储过程中声明标量变量 "@bit"作为输出参数

python - Django 树表的复杂分区和排序

sql - 插入多行,其中一列来自另一个表

performance - EC2/EBS 上的 PostgreSQL 性能

MySQL 删除字符串中特定字符之后的部分

MySQL 转储所有数据库并在导入时创建(或重新创建)它们?

database - 如果 postgres 的性能低下,我应该选择哪个数据库

c++ - 将仅 1 行代码不同的两个函数分组

c# - 循环中的性能差异

python - 合并 mysql 表中的字符串,无需额外空格