Tengo una Query en PostgreSQL que en local me ejecutaba perfectamente pero al ejectutarla en producción ha resultado ser horriblemente lenta.
Tanto en desarrollo como en producción ejecuté VACUUM ANALYZE;
por si hubiese sido el problema pero todo sigue igual.
¿Qué puede estar sucendiendo y/o como se podría solucionar?
Dejo datos de estas consultas si alguien ve en ellas la causa:
Query:
explain analyze SELECT "usuario"."id", [...]
FROM "usuario" INNER JOIN "pago" ON ( "usuario"."id" = "pago"."customer_id" )
WHERE ("usuario"."estado" = 0 AND "pago"."id" IS NOT NULL AND NOT ("usuario"."id" IN (SELECT U1."usuario_id" AS Col1 FROM "compra" U1 WHERE U1."id" IS NOT NULL)) AND NOT ("usuario"."id" IN (SELECT U1."user_id" AS Col1 FROM "llamada" U1 WHERE U1."result_call" >= 20)))
Producción:
PostgreSQL 9.3.10
https://explain.depesz.com/s/vH3
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Merge Join (cost=40816.55..17773313.78 rows=4686 width=425) (actual time=111460.456..1015832.289 rows=1 loops=1)
Merge Cond: ("usuario".id = pago.customer_id)
-> Index Scan using "usuario_pkey" on "usuario" (cost=21.35..17998731.10 rows=2206 width=425) (actual time=1635.214..1015665.631 rows=57 loops=1)
Filter: ((NOT (hashed SubPlan 2)) AND (estado = 0) AND (NOT (SubPlan 1)))
Rows Removed by Filter: 12394
SubPlan 2
-> Seq Scan on "llamada" u1_1 (cost=0.00..20.38 rows=277 width=4) (actual time=0.002..0.002 rows=0 loops=1)
Filter: (result_call >= 20)
SubPlan 1
-> Materialize (cost=0.00..2769.55 rows=50037 width=4) (actual time=0.012..58.055 rows=23514 loops=8835)
-> Seq Scan on pago u1 (cost=0.00..2323.37 rows=50037 width=4) (actual time=0.014..165.809 rows=50057 loops=1)
Filter: (id IS NOT NULL)
-> Index Scan using pago_customer_id on pago (cost=0.29..2915.78 rows=26413 width=4) (actual time=0.014..98.417 rows=26543 loops=1)
Filter: (id IS NOT NULL)
Total runtime: 1015836.596 ms
(15 rows)
Entorno de desarrollo:
PostgreSQL 9.5.1
https://explain.depesz.com/s/J6uY
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=3312.44..4085.27 rows=3727 width=427) (actual time=35.212..38.552 rows=1 loops=1)
Hash Cond: (pago.customer_id = "usuario".id)
-> Seq Scan on pago (cost=0.00..658.23 rows=20623 width=4) (actual time=0.019..2.477 rows=20623 loops=1)
Filter: (id IS NOT NULL)
-> Hash (cost=3290.46..3290.46 rows=1758 width=427) (actual time=34.655..34.655 rows=43 loops=1)
Buckets: 2048 Batches: 1 Memory Usage: 28kB
-> Seq Scan on "usuario" (cost=2003.21..3290.46 rows=1758 width=427) (actual time=30.532..34.623 rows=43 loops=1)
Filter: ((NOT (hashed SubPlan 1)) AND (NOT (hashed SubPlan 2)) AND (estado = 0))
Rows Removed by Filter: 9686
SubPlan 1
-> Seq Scan on pago u1 (cost=0.00..1893.71 rows=43371 width=4) (actual time=0.020..15.883 rows=43371 loops=1)
Filter: (id IS NOT NULL)
SubPlan 2
-> Seq Scan on "llamada" u1_1 (cost=0.00..1.06 rows=2 width=4) (actual time=0.007..0.007 rows=0 loops=1)
Filter: (result_call >= 20)
Rows Removed by Filter: 5
Planning time: 5.674 ms
Execution time: 47.179 ms
NOT(usuario IN (SELECT u FROM TABLE_X) )
porNOT EXISTS
.