sql - How to optimize this query for my school project -
it's assignment kindly me optimize below 2 queries.
optimize assignment 1:
select n.node_id, min(least(n.date,ec.date)) date n, ec (n.node_id = ec.node_id_from or n.node_id = ec.node_id_to) , n.date - ec.date > 0 group n.node_id;
optimize assignment 2:
select to_char(convert_timezone ('utc','america/los_angeles', tablea."date"), 'yyyy-mm') "date_month", count(distinct case when (tableb."date" not null) tableb._id else null end) "tableb.countb", count(distinct case when (tablec."date" not null) tablec._id else null end) "tablec.countc" tablea tablea left join tableb tableb on (date (convert_timezone ('utc', 'america/los_angeles',tableb."date"))) = (date (convert_timezone ('utc', 'america/los_angeles',tablea."date"))) left join tablec tablec on (date (convert_timezone ('utc', 'america/los_angeles',tablec."date"))) = (date (convert_timezone ('utc', 'america/los_angeles',tablea."date"))) tablea."date" >= convert_timezone ('america/los_angeles', 'utc', dateadd (month, -17, date_trunc('month', date_trunc('day', convert_timezone ('utc', 'america/los_angeles',getdate ())))) group 1 order 1 desc limit 500;
use short alias makes sql query shorter , cleaner. here optimized version of second query
select datepart(month, a.date-8/24) date_month, sum(case when b.date not null 1 else 0 end) countb, sum(case when c.date not null 1 else 0 end) countc, tablea left join tableb b on b.date = a.date -- timezone offsets not necessary, left join tablec c on c.date = a.date -- both in same timezone a.date >= dateadd(hour, 8, dateadd (month,-17,date_trunc('month', getdate () )) group 1 order 1 desc limit 500;
Comments
Post a Comment