您能否帮我改变脚本字符串或其他任何改进查询的方法。
如果您可以从以下脚本提供示例脚本,我不是开发人员。表值函数在查询的实际执行计划中花费49%并运行7分钟。
提前谢谢。
LEFT OUTER JOIN
(SELECT
pp2.patient_id,
Foodallergy= STUFF((SELECT ',' + cd.description
FROM [OHCP_OHProblemList].[problemlist].[problems_v] pp
INNER JOIN [OHCP_OHCLINICAL].[CodeSet].[CodeSet] CS ON cs.identifier = pp.problem_name_code_set
INNER JOIN ohcp_ohclinical.codeset.CodeDefinition cd ON (cd.code = pp.problem_name_code AND cd.codesetid = cs.id)
WHERE patient_id = pp2.patient_id
AND pp.last_update_action NOT IN ('DELETE', 'CLOSE')
AND pp.adr_class_code IN (3,4)
AND cd.description != 'Other'
ORDER BY [description]
FOR XML PATH ('')), 1, 1, ''),
description = STUFF((SELECT N',' + ' Other:' + pp.problem_name_freetext_desc
FROM [OHCP_OHProblemList].[problemlist].[problems_v] pp
INNER JOIN [OHCP_OHCLINICAL].[CodeSet].[CodeSet] CS on cs.identifier = pp.problem_name_code_set
INNER JOIN ohcp_ohclinical.codeset.CodeDefinition cd ON (cd.code = pp.problem_name_code AND cd.codesetid = cs.id)
WHERE patient_id = pp2.patient_id
AND pp.last_update_action not in ('DELETE', 'CLOSE')
AND pp.adr_class_code in (3, 4)
FOR XML PATH('')), 1, 1, '')
FROM
[OHCP_OHProblemList].[problemlist].[problems_v] pp2
GROUP BY
pp2.patient_id) AS problem ON problem.patient_id = externalpatientid.externalpatientid