I have this query in my postgresql db
UPDATE main_table m
SET new_prod=false
FROM reseller_product p
WHERE p.distributor_id=896
AND p.sku = m.sku
AND p.reseller_id=8;
And it takes too long to execute. I have indexes on sku column in both tables. After using EXPLAIN command I saw that the planer used Hash Join. It seems to me that Merge Join would be faster here. Am I wrong ? Or maybe the planer used Merge Join because query has filters like "p.distributor_id=896"?
PS sku type is character varying(50)
"Update (cost=80414.35..228437.71 rows=848460 width=212) (actual time=1582872.917..1582872.917 rows=0 loops=1)" " -> Hash Join (cost=80414.35..228437.71 rows=848460 width=212) (actual time=6906.044..23677.829 rows=706328 loops=1)" " Hash Cond: ((p.sku)::text = (m.sku)::text)" " -> Append (cost=0.00..96319.53 rows=1067877 width=20) (actual time=0.909..7426.880 rows=808287 loops=1)" " -> Index Scan using res_prod_reseller_id on reseller_product p (cost=0.00..8.74 rows=1 width=20) (actual time=0.729..0.729 rows=0 loops=1)" " Index Cond: (reseller_id = 8)" " Filter: (distributor_id = 896)" " -> Seq Scan on reseller_product_8 p (cost=0.00..96310.79 rows=1067876 width=20) (actual time=0.168..3850.121 rows=808287 loops=1)" " Filter: ((distributor_id = 896) AND (reseller_id = 8))" " -> Hash (cost=45779.60..45779.60 rows=848460 width=202) (actual time=6897.658..6897.658 rows=709948 loops=1)" " Buckets: 1024 Batches: 256 Memory Usage: 417kB" " -> Seq Scan on main_table m (cost=0.00..45779.60 rows=848460 width=202) (actual time=0.926..2770.997 rows=709948 loops=1)"
EXPLAIN
plan – Akash