我在下面有查询,查询返回正确的结果,但当表 cslogcurve_datanode 中有超过10000行时,My Query需要花费很长时间,我想问一下如何优化或最好sintax用于我的查询,并且必须在我的查询下面返回相同的结果
SELECT DISTINCT
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-1' AND s1.baseNodeDouble = t.baseNodeDouble) AS R1,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-10' AND s1.baseNodeDouble = t.baseNodeDouble) AS R2
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-11' AND s1.baseNodeDouble = t.baseNodeDouble) AS R3,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-12' AND s1.baseNodeDouble = t.baseNodeDouble) AS R4,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-13' AND s1.baseNodeDouble = t.baseNodeDouble) AS R5,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-14' AND s1.baseNodeDouble = t.baseNodeDouble) AS R6,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-15' AND s1.baseNodeDouble = t.baseNodeDouble) AS R7,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-16' AND s1.baseNodeDouble = t.baseNodeDouble) AS R8,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-17' AND s1.baseNodeDouble = t.baseNodeDouble) AS R9,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-18' AND s1.baseNodeDouble = t.baseNodeDouble) AS R10
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-19' AND s1.baseNodeDouble = t.baseNodeDouble) AS R11,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-2' AND s1.baseNodeDouble = t.baseNodeDouble) AS R12,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-20' AND s1.baseNodeDouble = t.baseNodeDouble) AS R13,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-21' AND s1.baseNodeDouble = t.baseNodeDouble) AS R14,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-3' AND s1.baseNodeDouble = t.baseNodeDouble) AS R15,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-4' AND s1.baseNodeDouble = t.baseNodeDouble) AS R16,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-5' AND s1.baseNodeDouble = t.baseNodeDouble) AS R17,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-6' AND s1.baseNodeDouble = t.baseNodeDouble) AS R18,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-7' AND s1.baseNodeDouble = t.baseNodeDouble) AS R19,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-8' AND s1.baseNodeDouble = t.baseNodeDouble) AS R20,
(SELECT dataNode FROM cslogcurve_datanode s1 WHERE logCurveInfoUID = 'W-12B-01f34a-lci-9' AND s1.baseNodeDouble = t.baseNodeDouble) AS R21
FROM cslogcurve_datanode t ORDER BY R1 ASC
答案 0 :(得分:5)
请改用IN
子句
语法:
SELECT DISTINCT <COLUMN>
FROM <TABLE>
WHERE <COLUMN> IN (<VALUE1>,<VALUE2>,...)
ORDER BY <COLUMN>;
所以你的查询应该是这样的:
SELECT DISTINCT dataNode
FROM cslogcurve_datanode
WHERE logCurveInfoUID IN ('W-12B-01f34a-lci-1', 'W-12B-01f34a-lci-10'
,'W-12B-01f34a-lci-11','W-12B-01f34a-lci-12'
,'W-12B-01f34a-lci-13',....)
ORDER BY dataNode ASC;