I'm taking the month using to_char()
which is common in two tables namely Table1
and Table2
.
But in Table2 I have both date and month.
Also I've created a index on Table1. If I write the same query only Table1 I get the results within 1000ms.
I'm taking sum of values and combining the results using left join.
Here's the query.
SELECT "month", -- Table2 sum("value1"), -- Table2 sum("value2") -- Table1 FROM Table2 LEFT JOIN Table1 ON Table2."month" = to_char(Table1.Date, 'Mon-yy') WHERE Table2.Date BEtween '2014-01-01' AND '2014-03-01' GROUP BY "month"
EXPLAIN OF THE Query :
"GroupAggregate (cost=88133.61..3688425.12 rows=2 width=15)" " -> Merge Left Join (cost=88133.61..2707006.48 rows=130855816 width=15)" " Merge Cond: (Table2.month = (to_char((Table1.date)::timestamp with time zone, 'Mon-yy'::text)))" " -> Sort (cost=8922.32..9056.34 rows=53609 width=11)" " Sort Key: Table2.month" " -> Seq Scan on Table2(cost=0.00..3885.28 rows=53609 width=11)" " Filter: ((date >= '2014-01-01'::date) AND (date Materialize (cost=79211.29..81652.22 rows=488186 width=8)" " -> Sort (cost=79211.29..80431.75 rows=488186 width=8)" " Sort Key: (to_char((Table1.date)::timestamp with time zone, 'Mon-yy'::text))" " -> Seq Scan on Table1(cost=0.00..19735.86 rows=488186 width=8)"
Currently I have about 500k rows in Table1.
Everyday I update the table with around 3 to 4k records to Table1.
The query just keeps running.
I get no results.
Can i anyone say me where I'm going wrong?
table2.date
? How many rows fromtable2
will be selected by the conditionbetween '2014-01-01' AND '2014-03-01'
? And why do you have amonth
column intable2
if you already have adate
column that apparently stores the full date. – a_horse_with_no_name