Home Query with join taking too long to run - Postgresql
Reply: 0

Query with join taking too long to run - Postgresql

user1826
1#
user1826 Published in April 21, 2018, 3:26 pm

I have a query that it's taking too long to run. I'm using PostgreSQL 10.3. In my tables involved in this query, I have about 3.5 million records in each. The query is:

SELECT thf.attr1, thf.attr2, thf.attr3, thf.attr4
FROM tb_one AS thf
INNER JOIN tb_two AS ths
ON ths.tb_hit_hitid = thf.tb_hit_hitid
WHERE ths.source IN ('source1', 'source2')

In these tables, I have index:

CREATE INDEX tb_two_idx_1 on tb_two (Source ASC, attr5 ASC);
CREATE INDEX tb_one_idx_1 on tb_one USING btree (attr1 ASC,attr2 ASC,attr3 ASC,attr4 ASC);
CREATE INDEX tb_one_idx_2 on tb_hit_feature (tb_hit_HitId ASC);
CREATE INDEX tb_two_idx_2 on tb_hit_source (tb_hit_HitId ASC);

This is the QUERY PLAN (explain (analyse, buffers)):

    QUERY PLAN                                                                             
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Merge Join  (cost=3.85..287880.35 rows=1771004 width=44) (actual time=0.091..3894.024 rows=1726970 loops=1)
   Merge Cond: (thf.tb_hit_hitid = ths.tb_hit_hitid)
   Buffers: shared hit=354821
   ->  Index Scan using tb_one_idx_2 on tb_one thf  (cost=0.43..124322.43 rows=3230800 width=52) (actual time=0.014..655.036 rows=1726946 loops=1)
         Buffers: shared hit=27201
   ->  Index Scan using tb_two_idx_2 on tb_two ths  (cost=0.43..139531.97 rows=1771004 width=8) (actual time=0.069..1604.789 rows=1726973 loops=1)
         Filter: ((source)::text = ANY ('{source1,source2}'::text[]))
         Rows Removed by Filter: 1651946
         Buffers: shared hit=327620
 Planning time: 2.737 ms
 Execution time: 4117.573 ms
(11 rows)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305937 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO