Skip to content

Commit f0e8e9c

Browse files
committed
Query optimisation kilkari subscriber with registration date.
1 parent 9b14482 commit f0e8e9c

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

NMSReportingSuite/src/main/java/com/beehyv/nmsreporting/dao/impl/BeneficiaryWithRegistrationDateBlockDaoImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer dis
5656
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
5757
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
5858
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
59-
"FROM Beneficiary b INNER JOIN ( SELECT beneficiary_id, MAX(subscription_id) as max_id " +
60-
"FROM subscriptions s GROUP BY beneficiary_id ) max_ids ON b.id = max_ids.beneficiary_id " +
61-
"INNER JOIN subscriptions s ON max_ids.max_id = s.subscription_id " +
59+
"FROM Beneficiary b " +
60+
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
6261
"WHERE registrationDate >= :fromDate AND registrationDate < :toDate " +
6362
"AND b.district_id = :districtId GROUP BY b.healthBlock_id "+
6463

@@ -140,16 +139,19 @@ public List<KilkariSubscriberRegistrationDateRejectedCountDto> duplicateRejected
140139
" COUNT(DISTINCT CASE WHEN date_diff + 90 >= 169 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Child, " +
141140
" COUNT(DISTINCT CASE WHEN date_diff + 90 < -90 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Ineligible " +
142141
"FROM ( " +
143-
" SELECT mir.*, " +
142+
" SELECT mir.health_block_id, " +
143+
" mir.registration_no, " +
144144
" DATEDIFF(mir.registration_date, STR_TO_DATE(SUBSTRING_INDEX(mir.lmp_date, '.', 1), '%Y-%m-%dT%H:%i:%s')) AS date_diff " +
145145
" FROM mother_import_rejection mir " +
146146
" INNER JOIN ( " +
147-
" SELECT MAX(id) AS id " +
147+
" SELECT registration_no, MAX(id) AS max_id " +
148148
" FROM mother_import_rejection " +
149+
" WHERE registration_date >= :fromDate " +
150+
" AND registration_date < :toDate " +
151+
" AND district_id = :districtId " +
149152
" GROUP BY registration_no " +
150-
" ) mir_latest ON mir.id = mir_latest.id " +
151-
" WHERE mir.district_id = :districtId " +
152-
" AND (mir.registration_date >= :fromDate AND mir.registration_date < :toDate) " +
153+
" ) latest ON mir.registration_no = latest.registration_no AND mir.id = latest.max_id " +
154+
" INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
153155
") mir " +
154156
"INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
155157
"GROUP BY mir.health_block_id " +

NMSReportingSuite/src/main/java/com/beehyv/nmsreporting/dao/impl/BeneficiaryWithRegistrationDateDistrictDaoImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer sta
5858
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
5959
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
6060
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
61-
"FROM Beneficiary b INNER JOIN ( SELECT beneficiary_id, MAX(subscription_id) as max_id " +
62-
"FROM subscriptions s group by beneficiary_id ) max_ids ON b.id = max_ids.beneficiary_id " +
63-
"INNER JOIN subscriptions s ON max_ids.max_id = s.subscription_id " +
61+
"FROM Beneficiary b " +
62+
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
6463
"WHERE registrationDate >= :fromDate AND registrationDate < :toDate " +
6564
"AND b.state_id = :stateId GROUP BY b.district_id "+
6665

@@ -142,16 +141,19 @@ public List<KilkariSubscriberRegistrationDateRejectedCountDto> duplicateRejected
142141
" COUNT(DISTINCT CASE WHEN date_diff + 90 >= 169 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Child, " +
143142
" COUNT(DISTINCT CASE WHEN date_diff + 90 < -90 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Ineligible " +
144143
"FROM ( " +
145-
" SELECT mir.*, " +
144+
" SELECT mir.district_id, " +
145+
" mir.registration_no, " +
146146
" DATEDIFF(mir.registration_date, STR_TO_DATE(SUBSTRING_INDEX(mir.lmp_date, '.', 1), '%Y-%m-%dT%H:%i:%s')) AS date_diff " +
147147
" FROM mother_import_rejection mir " +
148148
" INNER JOIN ( " +
149-
" SELECT MAX(id) AS id " +
149+
" SELECT registration_no, MAX(id) AS max_id " +
150150
" FROM mother_import_rejection " +
151+
" WHERE registration_date >= :fromDate " +
152+
" AND registration_date < :toDate " +
153+
" AND state_id = :stateId " +
151154
" GROUP BY registration_no " +
152-
" ) mir_latest ON mir.id = mir_latest.id " +
153-
" WHERE mir.state_id = :stateId " +
154-
" AND (mir.registration_date >= :fromDate AND mir.registration_date < :toDate) " +
155+
" ) latest ON mir.registration_no = latest.registration_no AND mir.id = latest.max_id " +
156+
" INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
155157
") mir " +
156158
"INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
157159
"GROUP BY mir.district_id " +

NMSReportingSuite/src/main/java/com/beehyv/nmsreporting/dao/impl/BeneficiaryWithRegistrationDateStateDaoImpl.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Date fromDa
5656
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
5757
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child, "+
5858
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
59-
"FROM Beneficiary b INNER JOIN ( SELECT beneficiary_id, MAX(subscription_id) as max_id " +
60-
"FROM subscriptions s group by beneficiary_id ) max_ids ON b.id = max_ids.beneficiary_id " +
61-
"INNER JOIN subscriptions s ON max_ids.max_id = s.subscription_id " +
59+
"FROM Beneficiary b " +
60+
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
6261
"WHERE registrationDate >= :fromDate AND registrationDate < :toDate " +
6362
" AND b.state_id NOT IN (43, 42) " + //removing Kerala and TamilNadu
6463
"GROUP BY b.state_id "+
@@ -142,15 +141,19 @@ public List<KilkariSubscriberRegistrationDateRejectedCountDto> duplicateRejected
142141
" COUNT(DISTINCT CASE WHEN date_diff + 90 >= 169 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Child, " +
143142
" COUNT(DISTINCT CASE WHEN date_diff + 90 < -90 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Ineligible " +
144143
"FROM ( " +
145-
" SELECT mir.*, " +
144+
" SELECT mir.state_id, " +
145+
" mir.registration_no, " +
146146
" DATEDIFF(mir.registration_date, STR_TO_DATE(SUBSTRING_INDEX(mir.lmp_date, '.', 1), '%Y-%m-%dT%H:%i:%s')) AS date_diff " +
147147
" FROM mother_import_rejection mir " +
148148
" INNER JOIN ( " +
149-
" SELECT MAX(id) AS id " +
149+
" SELECT registration_no, MAX(id) AS max_id " +
150150
" FROM mother_import_rejection " +
151+
" WHERE registration_date >= :fromDate " +
152+
" AND registration_date < :toDate " +
153+
" AND state_id NOT IN (43, 42) " +
151154
" GROUP BY registration_no " +
152-
" ) mir_latest ON mir.id = mir_latest.id " +
153-
" WHERE (mir.registration_date >= :fromDate AND mir.registration_date < :toDate) AND mir.state_id NOT IN (43, 42) " +
155+
" ) latest ON mir.registration_no = latest.registration_no AND mir.id = latest.max_id " +
156+
" INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
154157
") mir " +
155158
"INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
156159
"GROUP BY mir.state_id " +

NMSReportingSuite/src/main/java/com/beehyv/nmsreporting/dao/impl/BeneficiaryWithRegistrationDateSubCentreDaoImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer blo
5858
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
5959
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
6060
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
61-
"FROM Beneficiary b INNER JOIN ( SELECT beneficiary_id, MAX(subscription_id) as max_id " +
62-
"FROM subscriptions s group by beneficiary_id ) max_ids ON b.id = max_ids.beneficiary_id " +
63-
"INNER JOIN subscriptions s ON max_ids.max_id = s.subscription_id "+
61+
"FROM Beneficiary b " +
62+
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) "+
6463
"WHERE registrationDate >= :fromDate AND registrationDate < :toDate " +
6564
"AND b.healthBlock_id = :blockId GROUP BY b.healthSubFacility_id " +
6665

@@ -142,16 +141,19 @@ public List<KilkariSubscriberRegistrationDateRejectedCountDto> duplicateRejected
142141
" COUNT(DISTINCT CASE WHEN date_diff + 90 >= 169 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Child, " +
143142
" COUNT(DISTINCT CASE WHEN date_diff + 90 < -90 THEN mir.registration_no ELSE NULL END) AS duplicate_subscribers_Ineligible " +
144143
"FROM ( " +
145-
" SELECT mir.*, " +
144+
" SELECT mir.subcentre_id, " +
145+
" mir.registration_no, " +
146146
" DATEDIFF(mir.registration_date, STR_TO_DATE(SUBSTRING_INDEX(mir.lmp_date, '.', 1), '%Y-%m-%dT%H:%i:%s')) AS date_diff " +
147147
" FROM mother_import_rejection mir " +
148148
" INNER JOIN ( " +
149-
" SELECT MAX(id) AS id " +
149+
" SELECT registration_no, MAX(id) AS max_id " +
150150
" FROM mother_import_rejection " +
151+
" WHERE registration_date >= :fromDate " +
152+
" AND registration_date < :toDate " +
153+
" AND health_block_id = :blockId " +
151154
" GROUP BY registration_no " +
152-
" ) mir_latest ON mir.id = mir_latest.id " +
153-
" WHERE mir.health_block_id = :blockId " +
154-
" AND (mir.registration_date >= :fromDate AND mir.registration_date < :toDate) " +
155+
" ) latest ON mir.registration_no = latest.registration_no AND mir.id = latest.max_id " +
156+
" INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
155157
") mir " +
156158
"INNER JOIN Beneficiary b ON b.rch_id = mir.registration_no " +
157159
"GROUP BY mir.subcentre_id " +

0 commit comments

Comments
 (0)