Skip to content

Commit

Permalink
obsolete activity_current replace with column iscurrent + indexes to …
Browse files Browse the repository at this point in the history
…support faster activity queries in general - v1 only need to do v2
  • Loading branch information
micheal-w-wells committed Nov 22, 2023
1 parent b18631d commit 8615a9d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
32 changes: 16 additions & 16 deletions api/src/queries/activity-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ CurrentNegativeObservations AS (
}

sqlStatement.append(
SQL` FROM activity_incoming_data a inner join activity_current b on a.activity_incoming_data_id = b.incoming_data_id `
SQL` FROM activity_incoming_data a `
);

if (searchCriteria.search_feature || searchCriteria.search_feature_server_id) {
Expand All @@ -374,68 +374,68 @@ LEFT JOIN
switch (searchCriteria.CSVType) {
case 'terrestrial_plant_observation':
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join observation_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_plant_observation':
sqlStatement.append('join observation_aquatic_plant_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join observation_aquatic_plant_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'terrestrial_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_chemical_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_chemical_aquatic_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'terrestrial_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_mechanical_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_mechanical_aquatic_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'biocontrol_release':
sqlStatement.append('join biocontrol_release_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join biocontrol_release_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'biocontrol_collection':
sqlStatement.append('join biocontrol_collection_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join biocontrol_collection_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'biocontrol_dispersal':
sqlStatement.append(
'join biocontrol_dispersal_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join biocontrol_dispersal_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'chemical_treatment_monitoring':
sqlStatement.append(
'join chemical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join chemical_treatment_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'mechanical_treatment_monitoring':
sqlStatement.append(
'join mechanical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join mechanical_treatment_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'biocontrol_release_monitoring':
sqlStatement.append(
'join biocontrol_release_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join biocontrol_release_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;

default:
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join observation_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
}
}

sqlStatement.append(SQL` where 1 = 1`);
sqlStatement.append(SQL` where 1 = 1 and a.iscurrent = true `);

if (lean) {
sqlStatement.append(SQL` and a.activity_incoming_data_id > 36345`);
Expand Down Expand Up @@ -762,8 +762,8 @@ LEFT JOIN
export const getActivitySQL = (activityId: string): SQLStatement => {
return SQL`
SELECT a.* FROM activity_incoming_data a
join activity_current b on a.activity_incoming_data_id = b.incoming_data_id
WHERE a.activity_id = ${activityId}
and a.iscurrent = true
`;
};

Expand Down
56 changes: 56 additions & 0 deletions database/src/migrations/0079_current_activity_refactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw(
`
set search_path=invasivesbc,public;
ALTER TABLE invasivesbc.activity_incoming_data add if not exists iscurrent boolean NOT NULL DEFAULT true;
CREATE OR REPLACE FUNCTION invasivesbc.current_activity_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
update invasivesbc.activity_incoming_data
set iscurrent = false
where activity_id = new.activity_id
and activity_incoming_data_id != new.activity_incoming_data_id
and new.deleted_timestamp is null;
update invasivesbc.activity_incoming_data
set deleted_timestamp = NOW()
where activity_id = new.activity_id
and activity_incoming_data_id != new.activity_incoming_data_id
and deleted_timestamp is null;
RETURN NEW;
END;
$function$
;
create or replace trigger update_activity_current after
insert
on
invasivesbc.activity_incoming_data for each row execute function invasivesbc.current_activity_function() ;
update invasivesbc.activity_incoming_data
set iscurrent = false ,
deleted_timestamp = NOW()
where activity_incoming_data_id not in (select incoming_data_id from activity_current);
CREATE INDEX if not exists activity_incoming_data_activity_id_and_current_idx ON invasivesbc.activity_incoming_data (activity_id,iscurrent);
create index if not exists activity_incoming_data_iscurrent_idx ON invasivesbc.activity_incoming_data (iscurrent);
CREATE INDEX if not exists activity_incoming_data_form_status_idx ON invasivesbc.activity_incoming_data (form_status);
`
);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`
`);
}

0 comments on commit 8615a9d

Please sign in to comment.