avniproject / avni-server

Backend APIs for Avni
https://avniproject.org
GNU Affero General Public License v3.0
7 stars 25 forks source link

Optimise this report query #775

Closed mahalakshme closed 2 months ago

mahalakshme commented 3 months ago

Issue:

This query, going to be used on superset reports for APF org, is very slow - takes 37 secs when load on prodread db is low

Technical analysis:

Done using explain analyse

  1. Replacing c.base_date::DATE + ((cicv.item_detail_status::JSON ->> 0)::JSON ->> 'end')::INT AS due_date, with c.base_date::DATE + cicv.item_detail_end/(86400) AS due_date, saves 15 secs since we are avoiding JSON parsing.
  2. Adding index on ETL address tables on id column - might help since nested loop join is done between ETL address and public.individual table
  3. To check if the slowness caused by join with checklist_item_checklist_view can be minimised, dumped the data of view into apf_temp_from_view table in prerelease, created an index on it and used this table instead of the view. But found it to be slow. Further analysis here can be made.

AC:

Out of scope:

Correcting the logic of the query which is incorrect

himeshr commented 2 months ago

Query Plan is available here for reference https://explain.dalibo.com/plan/d705hf8faae4893f

himeshr commented 2 months ago

Sample org Address Table schema before and after ETL run to create index for Address "ID" columns

Screenshot 2024-08-27 at 3 25 23 PM
himeshr commented 2 months ago

If needed, implementation team can manually create index for their target organisation address.id column using command

set role <db_schema>;
create unique index concurrently address_id_idx on address (id);
AchalaBelokar commented 2 months ago

As per discussion with @himeshr no need to QA this card.