sql - 具有多个连接到同一个表的慢 postgres 查询

标签 sql postgresql postgresql-performance

以下查询对我来说运行得非常慢:

    SELECT r.comp,b.comp,n.comp
      FROM paths AS p
INNER JOIN comps AS r ON (p.root=r.id)
INNER JOIN comps AS b ON (p.base=b.id)
INNER JOIN comps AS n ON (p.name=n.id);

运行 EXPLAIN (BUFFERS,ANALYZE) 得到以下结果: http://explain.depesz.com/s/iKG

是否为每个别名的 comps 表(重新)构建哈希?我可以做些什么来加快速度吗?注意:自己运行两个单独的查询来连接数据会更快。

Postgres 版本:9.1.9 机器:Ubuntu 12.04 8 | 4 核至强 2.5Ghz | 8GB 内存

archiving=> \d+ comps
                                     Table "public.comps"
 Column |  Type  |                     Modifiers                      | Storage  | Description 
--------+--------+----------------------------------------------------+----------+-------------
 id     | bigint | not null default nextval('comps_id_seq'::regclass) | plain    | 
 comp   | text   | not null                                           | extended | 
Indexes:
    "comps_pkey" PRIMARY KEY, btree (id)
    "comps_comp_key" UNIQUE CONSTRAINT, btree (comp)
    "comps_comp_idx" btree (comp)
    "comps_id_idx" btree (id)
Has OIDs: no

最佳答案

PostgreSQL 没有对 seljoin 进行任何特殊优化 - 所以这是预期的行为。

关于sql - 具有多个连接到同一个表的慢 postgres 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17999218/

相关文章:

SQL - 将行标记为每组的 0 值

mysql - 为什么这个 update-with-join mysql 查询这么慢?

php - Yii2 无法连接到 postgresql

sql - Postgres - 大数据库多次更新大量行

mysql - 仅当行包含值小于 1 的列时才获取行

mysql - 两个 where "date"子句有不同的列

database - 将序列化数组转换为 PSQL 数组,错误

optimization - 性能优化 - Postgres

postgresql - 大表查询的第一次调用出奇慢

performance - PostgreSQL 查询耗时过长