Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
run: mvn -B package --file pom.xml

- name: Upload WAR file as artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Identity-API
path: target/identity-0.0.1.war
15 changes: 14 additions & 1 deletion src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ Long getBeneficiaryCountsByVillageIDAndLastModifyDate(@Param("villageIDs") List<
@Param("lastModDate") Timestamp lastModifiedDate);


@Query("SELECT t FROM MBeneficiarymapping t WHERE t.vanSerialNo =:vanSerialNo AND t.vanID=:vanID")
//@Query("SELECT t FROM MBeneficiarymapping t WHERE t.vanSerialNo =:vanSerialNo AND t.vanID=:vanID")
@Query(value = "select bm FROM MBeneficiarymapping bm "
+"LEFT JOIN MBeneficiaryregidmapping brm ON brm.benRegId=bm.benRegId and brm.vanID=bm.vanID "
+"LEFT JOIN MBeneficiarycontact bc ON bc.vanSerialNo = bm.benContactsId and bm.vanID=bc.vanID "
+"LEFT JOIN MBeneficiarydetail bd on bm.benDetailsId = bd.vanSerialNo and bm.vanID=bd.vanID "
+"LEFT JOIN MBeneficiaryaddress ba ON ba.vanSerialNo = bm.benAddressId and bm.vanID=ba.vanID "
+"LEFT JOIN MBeneficiaryconsent bt on bt.vanSerialNo=bm.benConsentId and bm.vanID=bt.vanID "
//+"LEFT JOIN MBensecurestack bs on bs.benSecureStackId=bm.benSecureStackId and bm.vanID=bs.vanID "
+"LEFT JOIN MBeneficiaryImage bi on bi.vanSerialNo=bm.benImageId and bm.vanID=bi.vanID "
+"LEFT JOIN MBeneficiaryAccount bac on bac.vanSerialNo=bm.benAccountID and bm.vanID=bac.vanID "
+"LEFT JOIN MBeneficiaryidentity bid on bid.vanSerialNo=bm.benMapId and bm.vanID=bid.vanID "
+"LEFT JOIN MBeneficiaryfamilymapping bfm on bfm.vanSerialNo=bm.benMapId and bm.vanID=bfm.vanID "
+"LEFT JOIN MBeneficiaryservicemapping bsm on bsm.vanSerialNo=bm.benMapId and bm.vanID=bsm.vanID "
+"where bm.vanSerialNo=:vanSerialNo and bm.vanID=:vanID")
Comment on lines +125 to +138
Copy link

@coderabbitai coderabbitai bot Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

❓ Verification inconclusive

Consider performance implications of the complex query.

The query joins 10 tables which could impact performance, especially with large datasets. Consider the following improvements:

  1. Add appropriate indexes on join columns (vanID, vanSerialNo) in all tables
  2. Implement pagination to limit result set size
  3. Consider splitting into multiple queries if not all data is always needed

Run the following script to check if indexes exist on the join columns:


🏁 Script executed:

#!/bin/bash
# Description: Check for missing indexes on join columns
# Note: This assumes MySQL/MariaDB. Adjust for other databases.

echo "Checking indexes on join columns..."
mysql -e "
SELECT DISTINCT
  CONCAT(TABLE_NAME, '.', COLUMN_NAME) as 'Table.Column',
  CASE WHEN INDEX_NAME IS NULL THEN 'Missing Index' ELSE 'Has Index' END as 'Index Status'
FROM INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN INFORMATION_SCHEMA.STATISTICS s 
  ON c.TABLE_NAME = s.TABLE_NAME 
  AND c.COLUMN_NAME = s.COLUMN_NAME
WHERE c.TABLE_SCHEMA = 'your_db_name'
  AND c.TABLE_NAME IN (
    'MBeneficiaryregidmapping',
    'MBeneficiarycontact',
    'MBeneficiarydetail',
    'MBeneficiaryaddress',
    'MBeneficiaryconsent',
    'MBeneficiaryImage',
    'MBeneficiaryAccount',
    'MBeneficiaryidentity',
    'MBeneficiaryfamilymapping',
    'MBeneficiaryservicemapping'
  )
  AND c.COLUMN_NAME IN ('vanID', 'vanSerialNo')
ORDER BY 2 DESC, 1;
"

Length of output: 1030


Attention: Performance Considerations & Index Verification Needed

This query now performs complex LEFT JOINs across 10 related tables, which could significantly affect performance on large datasets. Please address the following:

  • Index Validation: Ensure that proper indexes exist on the join columns (vanID, vanSerialNo) in all involved tables. Our automated index check did not produce output (mysql command not found), so please manually verify that these indexes are in place.
  • Pagination: Consider adding pagination to limit the result set size if all joined data isn't required at once.
  • N+1 Query Risk: Review the data fetching design for potential N+1 query issues when accessing related entities.
  • Join Conditions: Confirm that all join conditions (especially the vanID criteria) consistently maintain data integrity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravishanigarapu please confirm this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai yes we have already created indexes for the all the tables.Fetching data based on filterations.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravishanigarapu Thank you for confirming. With proper indexes in place and filtered data retrieval, the query's performance should be well-optimized.


✏️ Learnings added
Learnt from: ravishanigarapu
PR: PSMRI/Identity-API#85
File: src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java:125-138
Timestamp: 2025-02-17T07:29:04.989Z
Learning: In the Identity-API project, tables involved in beneficiary mapping queries (MBeneficiaryregidmapping, MBeneficiarycontact, MBeneficiarydetail, MBeneficiaryaddress, MBeneficiaryconsent, MBeneficiaryImage, MBeneficiaryAccount, MBeneficiaryidentity, MBeneficiaryfamilymapping, MBeneficiaryservicemapping) have indexes created on join columns (vanID, vanSerialNo) to optimize query performance.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

MBeneficiarymapping getMapping(@Param("vanSerialNo") BigInteger vanSerialNo,@Param("vanID") Integer vanID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import jakarta.persistence.NoResultException;
import jakarta.persistence.QueryTimeoutException;


@Service
public class IdentityService {
private static final Logger logger = LoggerFactory.getLogger(IdentityService.class);
Expand Down Expand Up @@ -578,7 +579,8 @@ private MBeneficiarymapping getBeneficiariesDTONew(Object[] benMapArr) {
benMapOBJ.setCreatedBy(String.valueOf(benMapArr[10]));
benMapOBJ.setCreatedDate((Timestamp) benMapArr[11]);
benMapOBJ = mappingRepo.getMapping(getBigIntegerValueFromObject(benMapArr[9]), (Integer) benMapArr[8]);




BigInteger benRegId = new BigInteger(benMapArr[5].toString());
RMNCHBeneficiaryDetailsRmnch obj = rMNCHBeneficiaryDetailsRmnchRepo
Expand Down
Loading