我的数据库中有四个表
检查员:身份证,姓名,类型,状态,电子邮件
状态:id,name,parent
患者:身份证,姓名,年龄,国家代码
Patient_Log:id,patient_id,inspector_id,state_id,呼吸,body_movement,face_movement,gag,cough,cornea,pupil,doll_eye,镇静,状态,更新
我现在使用此查询:
SELECT patient.id, patient.name, patient.age, patient.national_code,
pLog.id AS pLog_id, pLog.inspector_id, pLog.state_id, pLog.breathing, pLog.sedation,
inspecor.name AS ins_name, inspecor.type AS ins_type, inspecor.status AS ins_status
state.name AS state_name
FROM patient
JOIN patient_log AS pLog ON pLog.patient_id = patient.id AND pLog.id =
(SELECT MAX(patient_log.id) FROM patient_log WHERE patient_log.patient_id = patient.id)
JOIN inspector ON inspector.id = pLog.inspector_id
JOIN state ON state.id = pLog.state_id
WHERE pLog.status = 1
ORDER BY pLog.update DESC
LIMIT 30
这查询它的工作!我的结果是真的! 但现在我的患者表有1500条记录,patient_log表有8000条记录,这个查询我得到的结果是16秒!!!!!!
如果我从查询中删除此部分,我会得到0.02秒的结果,但我的结果不正确,并从patient_log返回第一条记录:
AND pLog.id = (SELECT MAX(patient_log.id) FROM patient_log WHERE
patient_log.patient_id = patient.id)
如何优化查询以获得正确的结果?
答案 0 :(得分:0)
尝试在patient_log.patient_id
和patient_log.id