简体   繁体   中英

postgres: select rows where foreign key count less than value

I've inherited a particularly slow performing query but I'm unclear of the best path to maintain the functionality and reduce the query cost.

A pared down version of the query looks like so:

select * from api_event where COALESCE(
                (SELECT count(*) FROM api_ticket WHERE
                event_id = api_event.id),
                0
            ) < api_event.ticket_max AND COALESCE(
                (SELECT count(*) FROM api_ticket WHERE
                api_ticket.user_id = 45187 AND event_id = api_event.id
                and api_ticket.status != 'x'),
                0
            ) < api_event.ticket_max_per_user;

Runing Explain/Analyze on that seems to tell me that this requires a sequential scan on the api_event table:

Seq Scan on api_event  (cost=0.00..69597.99 rows=448 width=243) (actual     time=0.059..230.981 rows=1351 loops=1)
Filter: ((COALESCE((SubPlan 1), 0::bigint) < ticket_max) AND     (COALESCE((SubPlan 2), 0::bigint) < ticket_max_per_user))
Rows Removed by Filter: 2647

Any suggestions on how I can improve this?

Rewriting the query as an explicit join will probably help:

select e.*
from api_event e left join
     (select t.event_id, count(*) as cnt,
             sum(case when t.user_id = 45187 and t.status <> 'x' then 1 else 0 
                 end) as special_cnt
      from api_ticket t
      group by t.event_id
     ) t
     on e.id = t.event_id
where coalesce(t.cnt, 0) < e.ticket_max and
      coalesce(special_cnt, 0) < e.ticket_max_per_user;

这是一个杂乱的子查询,最近我通过避免使用杂乱的子查询with基于基础的查询一起使用来提高了某些查询的性能,这在Oracle中非常快,我希望它对Postgres有所帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM