-2

我在下面有查询,查询返回正确的结果,但是当表cslogcurve_datanode中有超过 10000 行时,我的查询需要很长时间,我想问如何优化我的查询或最好的 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
4

1 回答 1

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;
于 2012-09-16T04:40:30.173 回答