dimanche 28 juin 2015

How to speed up the query in PostgreSQL

I have DB in PostgreSQL with a big data(now it is somewhere about 46 GB and db will continue to grow). I create index on often used column. Config file change too - shared_buffers = 1GB, temp_buffers = 256MB, work_mem = 512MB.. But query answered very slowly. Query example:

select distinct us_category_id as cat, count(h_user_id) as res from web_hits 
inner join users on h_user_id = us_id 
where (h_datetime)::date = ('2015-06-26')::date and us_category_id != ''
group by us_category_id

Explain Analyse:

HashAggregate (cost=2870958.72..2870958.93 rows=21 width=9) (actual time=899141.683..899141.683 rows=0 loops=1)

Group Key: users.us_category_id, count(web_hits.h_user_id)

-> HashAggregate (cost=2870958.41..2870958.62 rows=21 width=9) (actual time=899141.681..899141.681 rows=0 loops=1)

Group Key: users.us_category_id

-> Hash Join (cost=5974.98..2869632.11 rows=265259 width=9) (actual time=899141.679..899141.679 rows=0 loops=1)

Hash Cond: ((web_hits.h_user_id)::text = (users.us_id)::text)

-> Seq Scan on web_hits (cost=0.00..2857563.80 rows=275260 width=7) (actual time=899141.676..899141.676 rows=0 loops=1)

-> Seq Scan on web_hits (cost=0.00..2857563.80 rows=275260 width=7) (actual time=899141.676..899141.676 rows=0 loops=1)

Filter: ((h_datetime)::date = '2015-06-26'::date)

Rows Removed by Filter: 55051918

-> Hash (cost=4292.99..4292.99 rows=134559 width=10) (never executed)

-> Seq Scan on users (cost=0.00..4292.99 rows=134559 width=10) (never executed)

Filter: ((us_category_id)::text <> ''::text)

"Planning time: 1.309 ms" "Execution time: 899141.789 ms"

Date is changed. How can I speed up the query?

Aucun commentaire:

Enregistrer un commentaire