如何优化我的SQL查询?

时间:2012-09-04 10:38:26

标签: sql select query-optimization

SELECT ORG.DESCRIPTION, ORG.EXTERNALKEY,
   (SELECT P.VALUE FROM PROPERTY P, ORGANIZATION ORG WHERE P.IDX=24 AND ORG.ID=P.ID AND ORG.EXTERNALKEY='200004') AS COR_ACCOUNT,
   (SELECT ORG.DESCRIPTION FROM ORGANIZATION ORG WHERE ORG.NAME = P.VALUE) AS CPC,
   (SELECT P.VALUE FROM PROPERTY P, ORGANIZATION ORG WHERE P.IDX = 4 AND P.ID = ORG.ID AND ORG.EXTERNALKEY = '200004') AS VOEN
FROM PROPERTY P, ORGANIZATION ORG
WHERE P.IDX = 22 AND ORG.EXTERNALKEY = '200004' AND P.ID = ORG.ID;

您好。如何优化这个sql或者这是正常的吗?

2 个答案:

答案 0 :(得分:0)

我不确定你的具体要求,但鉴于SQL,我认为下面应该有效。

SELECT ORG.DESCRIPTION, ORG.EXTERNALKEY,
CASE WHEN P.IDX = 24 then P.Value END AS COR_ACCOUNT,
(SELECT ORG.DESCRIPTION FROM ORGANIZATION ORG WHERE ORG.NAME = P.VALUE) AS CPC,
  CASE WHEN p.idx = 4 then P.VALUE END AS VOEN
FROM PROPERTY P, ORGANIZATION ORG
WHERE P.IDX in(22,24,4) AND ORG.EXTERNALKEY = '200004' AND P.ID = ORG.ID;

答案 1 :(得分:0)

优化您的查询

SELECT ORG.DESCRIPTION,ORG.EXTERNALKEY,
CASE WHEN ORG.EXTERNALKEY='200004' AND P.IDX=24 THEN P.VALUE END AS COR_ACCOUNT,
(SELECT ORG.DESCRIPTION FROM ORGANIZATION ORG WHERE ORG.NAME = P.VALUE) AS CPC,
CASE WHEN ORG.EXTERNALKEY='200004' AND P.IDX=4 THEN P.VALUE END AS VOEN,
FROM PROPERTY P, ORGANIZATION ORG
WHERE P.IDX = 22 AND ORG.EXTERNALKEY = '200004' AND P.ID = ORG.ID;