Instead of
select * from table_name
Use
select col_1,col_2... from table_name
Instead of
select * from table_1 a,table_2 b where a.id=b.id
Use
select col_1,col_2... from table_1 a inner join table_2 b on a.id=b.id
If few set of columns are used regularly as filter or in join columns then create index in them.
create index index_name on table_name (col_1,col2..)
Note that using indexes on production write tables might hamper you write response time.
This would help you optimise individual logical parts and also narrow down the issues quickly when debugging.
with sample_cte as ( select col_1,col2,sum(value) as sum_value from test_table group by 1,2 ) , sample_cte_2 as ( select col_1,col2,sum(value) as sum_value from test_table group by 1,2 ) select * from sample_cte a inner join sample_cte_2 b on a.col_1=b.col_2
7. Make the very primary table without any duplicates, so that you never have to use select distinct ... from ....
or select col_1,col_2 ... from .... group by 1,2
type of statements.
9. If you joining based on datetime or date columns, store another column as the unix_epoch
of the datetime/date column.
select date_part('epoch',current_date) ;
1680134400