Postgresql 9.1 - 子查询计数 (*) seq 扫描 - 低性能

标签 postgresql

我的查询甚至需要很长时间才能执行;它受到整数值索引的限制和排序。当我红色时,问题出在子查询中的 count(*) - 但我没有找到解决方案

PostgreSQL 9.1

查询:

SELECT
   sms.id,
  (select count(*) 
   from sms_received, sms_recipient 
   where sms.id = sms_recipient.sms_id
     and sms_recipient.id = sms_received.sms_recipient_id ) as pocet_resp
FROM "sms" WHERE done = true
ORDER BY "sms"."id" desc limit 100;

解释分析输出:

Limit  (cost=0.00..377992.17 rows=100 width=4) (actual time=58.566..5549.074 rows=100 loops=1)
   ->  Index Scan using sms_id on sms  (cost=0.00..1701422117.01 rows=450121 width=4) (actual time=58.564..5548.913 rows=100 loops=1)
         Filter: done
         SubPlan 1
           ->  Aggregate  (cost=3778.61..3778.62 rows=1 width=0) (actual time=55.471..55.471 rows=1 loops=100)
                 ->  Hash Join  (cost=660.83..3778.59 rows=6 width=0) (actual time=55.276..55.456 rows=0 loops=100)
                       Hash Cond: (sms_received.sms_recipient_id = sms_recipient.id)
                       ->  Seq Scan on sms_received  (cost=0.00..2656.33 rows=123033 width=4) (actual time=0.002..30.758 rows=123039 loops=100)
                       ->  Hash  (cost=658.73..658.73 rows=168 width=4) (actual time=0.060..0.060 rows=27 loops=100)
                             Buckets: 1024  Batches: 1  Memory Usage: 1kB
                             ->  Bitmap Heap Scan on sms_recipient  (cost=5.92..658.73 rows=168 width=4) (actual time=0.036..0.047 rows=27 loops=100)
                                   Recheck Cond: (sms.id = sms_id)
                                   ->  Bitmap Index Scan on sms_rec_sms_id  (cost=0.00..5.87 rows=168 width=0) (actual time=0.026..0.026 rows=140 loops=100)
                                         Index Cond: (sms.id = sms_id)
 Total runtime: 5549.237 ms

最佳答案

也许这会有所帮助:

select    sms.id,
          count(*) 
from      sms
left join sms_received on sms.id            = sms_recipient.sms_id
left join sms_recipient on sms_recipient.id = sms_received.sms_recipient_id
where     sms.done = true and
          sms.id in (select id from sms order by id desc limit 100)
group by  sms.id
order by  sms.id desc

你也可以试试:

select    sms.id,
          count(*) 
from      sms
left join sms_received on sms.id            = sms_recipient.sms_id
left join sms_recipient on sms_recipient.id = sms_received.sms_recipient_id
where     sms.done = true and
group by  sms.id
order by  sms.id desc
limit     100

...但我不确定它是否会如此有效。

关于Postgresql 9.1 - 子查询计数 (*) seq 扫描 - 低性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959398/

相关文章:

java - 使用 jOOQ 更新 Postgres 日期范围

Laravel迁移给表添加字段,但是Laravel添加了双引号

sql - PostgreSQL:错误:关系列不存在

基于 postgresql 公司 id 的序列

python - "Failed building wheel for psycopg2"- MacOSX 使用 virtualenv 和 pip

mysql - 目录、模式、用户和数据库实例之间的关系

python - 使用 python tarfile 备份 postgresql 数据目录。预期/忽略哪些错误

postgresql - 使用 postgres 和 pomm orm 按错误分组

java - 用于容器化 postgresql 的 Spring boot 的正确 jdbc 是什么

java - 尝试通过 liquibase、postgresql、spring boot 创建新数据库。结果-> "databasechangelog"不存在