Skip to content

Commit

Permalink
Performances (#111)
Browse files Browse the repository at this point in the history
* fix: n+1 on get survey-units

* fix: minor type fix

* fix: add indexes to state/surveyunit table (perf)

* fix(perf): delete useless subselect for date in contact outcomes

* fix: missing query param names

* bump version

* update liquibase for tests

---------

Co-authored-by: David Darras <[email protected]>
  • Loading branch information
SimonDmz and davdarras authored Nov 8, 2023
1 parent ac2ddaf commit 29abcea
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.insee.pearljam</groupId>
<artifactId>pearljam-back-office</artifactId>
<version>4.0.9</version>
<version>4.1.0</version>

<packaging>war</packaging>
<name>Pearl-Jam-Back-Office</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ public interface ContactOutcomeRepository extends JpaRepository<ContactOutcome,
+ "AND organization_unit_id IN (:ouIds) "
// Last state is TBR or FIN or CLO
+ "AND EXISTS ("
+ "SELECT 1 FROM state "
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id "
+ "AND date=("
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id"
+ ") "
+ "AND type IN ('TBR', 'FIN', 'CLO')"
+ ") "
+ "AND interviewer_id=:interviewerId "
Expand Down Expand Up @@ -93,12 +89,8 @@ Map<String, BigInteger> findContactOutcomeTypeByInterviewerAndCampaign(@Param("c
+ "AND organization_unit_id IN (:ouIds) "
// Last state is TBR or FIN or CLO
+ "AND EXISTS ("
+ "SELECT 1 FROM state "
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id "
+ "AND date=("
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id"
+ ") "
+ "AND type IN ('TBR', 'FIN', 'CLO')"
+ ") "
+ "AND interviewer_id IS NULL "
Expand Down Expand Up @@ -130,12 +122,8 @@ Map<String, BigInteger> findContactOutcomeTypeNotAttributed(@Param("campaignId")
+ "WHERE campaign_id=?1 "
// Last state is TBR or FIN or CLO
+ "AND EXISTS ("
+ "SELECT 1 FROM state "
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id "
+ "AND date=("
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id"
+ ") "
+ "AND type IN ('TBR', 'FIN', 'CLO')"
+ ") "
+ ") "
Expand Down Expand Up @@ -164,12 +152,8 @@ Map<String, BigInteger> findContactOutcomeTypeNotAttributed(@Param("campaignId")
+ "WHERE campaign_id=?1 "
// Last state is TBR or FIN or CLO
+ "AND EXISTS ("
+ "SELECT 1 FROM state "
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id "
+ "AND date=("
+ "SELECT MAX(date) FROM state "
+ "WHERE survey_unit_id = su.id"
+ ") "
+ "AND type IN ('TBR', 'FIN', 'CLO')"
+ ") "
+ "AND organization_unit_id=?2 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,19 @@ public interface SurveyUnitRepository extends JpaRepository<SurveyUnit, String>
)
List<SurveyUnit> findClosableIascoSurveyUnitId(@Param("ids") List<String> ids);

Set<SurveyUnit> findByCampaignIdAndOrganizationUnitIdIn(String id, List<String> lstOuId);


@Query(value="SELECT su FROM SurveyUnit su "
+" LEFT JOIN fetch su.comments"
+" LEFT JOIN fetch su.states"
+" LEFT JOIN fetch su.address"
+" LEFT JOIN fetch su.sampleIdentifier "
+" LEFT JOIN fetch su.interviewer "
+" LEFT JOIN fetch su.contactOucome "
+" LEFT JOIN fetch su.closingCause "
+" LEFT JOIN fetch su.identification "
+ "WHERE su.campaign.id=:id AND su.organizationUnit.id IN (:lstOuId)")
Set<SurveyUnit> findByCampaignIdAndOrganizationUnitIdIn(@Param("id")String id, @Param("lstOuId")List<String> lstOuId);

List<SurveyUnit> findByInterviewerIdIgnoreCase(String id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fr.insee.pearljam.api.repository;

import fr.insee.pearljam.api.domain.SurveyUnitTempZone;

import java.util.UUID;

import org.springframework.data.jpa.repository.JpaRepository;

/**
Expand All @@ -10,7 +13,7 @@
* @author Laurent Caouissin
*
*/
public interface SurveyUnitTempZoneRepository extends JpaRepository<SurveyUnitTempZone, String> {
public interface SurveyUnitTempZoneRepository extends JpaRepository<SurveyUnitTempZone, UUID> {

public void deleteBySurveyUnitId(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="davdarras" id="510-1">
<createIndex indexName="idx_survey_unit_on_state" tableName="state">
<column name="survey_unit_id"/>
</createIndex>
</changeSet>
<changeSet author="davdarras" id="510-2">
<createIndex indexName="idx_campaign_on_survey_unit" tableName="survey_unit">
<column name="campaign_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>
3 changes: 2 additions & 1 deletion src/main/resources/db/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<!-- Data model v4 : comment size extension -->
<include file="changelog/502_data_model_v4_changes.xml" relativeToChangelogFile="true" />


<!-- add indexes for perf -->
<include file="changelog/510_indexes_for_state_and_surveyunit.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="davdarras" id="510-1">
<createIndex indexName="idx_survey_unit_on_state" tableName="state">
<column name="survey_unit_id"/>
</createIndex>
</changeSet>
<changeSet author="davdarras" id="510-2">
<createIndex indexName="idx_campaign_on_survey_unit" tableName="survey_unit">
<column name="campaign_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>
8 changes: 6 additions & 2 deletions src/test/resources/db/master.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet author="bclaudel" id="1" context="not prod">
<sql dbms="postgresql" endDelimiter=";">
CREATE SCHEMA IF NOT EXISTS public
</sql>
</sql>
</changeSet>
<include file="changelog/000_init.xml" relativeToChangelogFile="true" />

Expand All @@ -28,4 +29,7 @@
<include file="changelog/501_data_model_v4_changes.xml" relativeToChangelogFile="true" />
<!-- Data model v4 : comment size extension -->
<include file="changelog/502_data_model_v4_changes.xml" relativeToChangelogFile="true" />

<!-- add indexes for perf -->
<include file="changelog/510_indexes_for_state_and_surveyunit.xml" relativeToChangelogFile="true" />
</databaseChangeLog>

0 comments on commit 29abcea

Please sign in to comment.