I am trying to uniquely read latest patient data from openmrs database but it takes a lot of time and I want to know if their is a way I can optimize my SQL code to be efficient. below is the initial query
SELECT
pid2.identifier AS HospitalNumber,
pid1.identifier AS UniqueID,
person.gender AS Gender,
MAX(encounter.encounter_datetime) AS LatestVisitDate,
CONCAT(pn.given_name, ' ', pn.family_name) AS Patient_Name,
CAST(psn_atr.value AS CHAR) AS Phone_No,
pa.address1 AS Patient_Address,
pa.city_village AS Patient_LGA,
pa.state_province AS Patient_State,
MAX(IF(obs.concept_id = 159599, obs.value_datetime, NULL)) AS ART_Start_Date
-- MAX(IF((obs.concept_id=165708 and encounter.form_id=27 AND obs.obs_datetime <= @endDate AND obs.voided =0),container.last_date,null)) as Pharmacy_LastPickupdate
FROM patient_identifier AS pid2
JOIN patient_identifier AS pid1 ON pid2.patient_id = pid1.patient_id
INNER JOIN encounter ON encounter.patient_id = pid2.patient_id
INNER JOIN person ON person.person_id = pid2.patient_id
INNER JOIN person_name AS pn ON pn.person_id = pid2.patient_id
LEFT JOIN person_attribute AS psn_atr ON psn_atr.person_id = pid2.patient_id
AND psn_atr.person_attribute_type_id = (SELECT person_attribute_type_id FROM person_attribute_type WHERE name = 'Telephone Number')
LEFT JOIN person_address AS pa ON pa.person_id = pid2.patient_id
LEFT JOIN obs ON obs.person_id = pid2.patient_id
WHERE pid2.identifier_type = 5 AND pid1.identifier_type = 4
AND encounter.voided = 0 AND pid1.voided = 0
GROUP BY pid2.identifier, pid1.identifier, person.gender, person.birthdate, Patient_Name, Phone_No, Patient_Address, Patient_LGA, Patient_State;
I have tried using a subquery for my query but I discovered that more rows were added which is not what I want below is the query I tried
SELECT
pid2.identifier AS HospitalNumber,
pid1.identifier AS UniqueID,
person.gender AS Gender,
encounter.encounter_datetime AS LatestVisitDate,
CONCAT(pn.given_name, ' ', pn.family_name) AS Patient_Name,
CAST(psn_atr.value AS CHAR) AS Phone_No,
pa.address1 AS Patient_Address,
pa.city_village AS Patient_LGA,
pa.state_province AS Patient_State,
obs_art_start_date.max_art_start_date AS ART_Start_Date
FROM patient_identifier AS pid2
JOIN patient_identifier AS pid1 ON pid2.patient_id = pid1.patient_id
INNER JOIN encounter ON encounter.patient_id = pid2.patient_id
INNER JOIN person ON person.person_id = pid2.patient_id
INNER JOIN person_name AS pn ON pn.person_id = pid2.patient_id
LEFT JOIN person_attribute AS psn_atr ON psn_atr.person_id = pid2.patient_id
AND psn_atr.person_attribute_type_id = (SELECT pa_type.person_attribute_type_id FROM person_attribute_type AS pa_type WHERE pa_type.name = 'Telephone Number')
LEFT JOIN person_address AS pa ON pa.person_id = pid2.patient_id
LEFT JOIN (
SELECT
obs.person_id,
MAX(obs.value_datetime) AS max_art_start_date
FROM obs
WHERE obs.concept_id = 159599
GROUP BY obs.person_id
) AS obs_art_start_date ON obs_art_start_date.person_id = pid2.patient_id
WHERE pid2.identifier_type = 5 AND pid1.identifier_type = 4
AND encounter.voided = 0 AND pid1.voided = 0;