I'm working on a Slow Query. It's faster now (It was 20sec before) but still not good.
Can you have a look and see if you can find something?
Cheers
Query:
WITH jobs AS(SELECT job.id, job.clientid, CONCAT(customer.company,' ', customer.name_first,' ', customer.name_last)AS"identity", job.gps_lat, job.gps_long FROMpublic.ja_jobs AS job JOINpublic.ja_customers AS customer ON customer.id = job.customerid WHERE job.clientid =22AND job.time_job >=1422702000AND job.time_job <=1456743540ANDNOT job.deleted ANDNOT job.templated
), items AS(SELECT job.identity,COALESCE(bill_item.unit_cost, billable.unit_cost,0)* bill_item.quantity AS cost,COALESCE(bill_item.unit_price, billable.unit_price,0)* bill_item.quantity AS revenue, bill_item.quantity AS quantity, note.n_quote_status, bill_item.for_invoicing FROM jobs AS job JOINpublic.ja_notes AS note ON note.jobid = job.id AND note.note_type IN('time','part')JOIN dm.bill_items AS bill_item ON bill_item.bill_item_id = note.bill_item_id AND bill_item.for_invoicing LEFTJOIN dm.billables AS billable ON billable.billable_id = note.billable_id JOINpublic.ja_mobiusers AS user_creator ON user_creator.id = note.mobiuserid AND( user_creator.name_first ilike 'Alan'OR user_creator.name_last ilike 'Alan'))SELECT item.identity, SUM(CASEWHEN item.for_invoicing THEN item.revenue ELSE0END)AS revenue, SUM(CASEWHEN item.for_invoicing THEN item.quantity ELSE0END)AS quantity, SUM(CASEWHEN item.for_invoicing THEN item.cost ELSE0END)AS cost
FROM items AS item
GROUPBY item.identityORDERBY revenue DESC, item.identityASC