I have this query to aggregate some data from t2
into t1
. This is done to optimize the application I am working on so that there are less queries to the database. I chose the below approach to make sure that I don't have to update t1
twice.
The big questions is, what indexes am I likely missing here and can the query be optimized further?
update t1
set
col1 = t2.col1_count,
col2 = t2.col2_sum,
col3 = t2.col3_sum
from (
select
b.user_id, b.t1_id,
coalesce(count(b.id), 0) as col1_count,
sum(case when b.col5 = true then b.col2 else 0 end) as col2_sum,
sum(case when b.col5 = false then b.col3 else 0 end) as col3_sum
from t1 a
left join t2 b on b.t1_id = a.id
where
b.user_id = 1
group by b.user_id, b.t1_id
) as t2
where
t2.t1_id = t1.id;
EDIT Adding requested information
These are my current indexes:
create index ix_t1_user_id on t1(user_id);
create unique index ux_t2_t1_id_t3_id on t2(t1_id, t3_id);
create index ix_t2_user_id on t2(user_id);
create index ix_t2_t1_id on t2(t1_id);
explain analyze gives me the following result:
Update on t1 (cost=2725.40..2737.42 rows=1 width=138) (actual time=1.428..1.428 rows=0 loops=1)
-> Nested Loop (cost=2725.40..2737.42 rows=1 width=138) (actual time=0.646..1.148 rows=166 loops=1)
-> Subquery Scan on t2 (cost=2725.40..2725.42 rows=1 width=84) (actual time=0.642..0.729 rows=166 loops=1)
-> HashAggregate (cost=2725.40..2725.41 rows=1 width=17) (actual time=0.639..0.685 rows=166 loops=1)
-> Nested Loop (cost=5.81..2725.39 rows=1 width=17) (actual time=0.034..0.536 rows=197 loops=1)
-> Bitmap Heap Scan on t2 b (cost=5.81..414.29 rows=193 width=13) (actual time=0.024..0.050 rows=197 loops=1)
Recheck Cond: (user_id = 1)
-> Bitmap Index Scan on ix_t2_user_id (cost=0.00..5.76 rows=193 width=0) (actual time=0.017..0.017 rows=197 loops=1)
Index Cond: (user_id = 1)
-> Index Scan using t1_pkey on t1 a (cost=0.00..11.96 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=197)
Index Cond: (id = b.t1_id)
Filter: (user_id = 1)
-> Index Scan using t1_pkey on t1 (cost=0.00..11.98 rows=1 width=58) (actual time=0.002..0.002 rows=1 loops=166)
Index Cond: (id = t2.t1_id)
Total runtime: 1.490 ms
ANALYZE
d your tables recently? The estimated and real row counts differ significantly. And an other observation: the topmost row indicates that there are no rows affected (actual rows=1
). How does the plan look like if it really does something? What would be the desirable execution time? – dezso Jan 27 at 18:09