Oracle查询花了很长时间

时间:2014-06-27 20:44:27

标签: sql oracle runtime

我的查询的精简版本在下面(因为有多少变量),当我自己运行子查询时它会在2秒内运行,但当我环绕它时,它需要超过半小时。我检查了所有列名 - 两次。为什么会这样?我该怎么做才能解决它?

SELECT Amount, Comments, FirstName, LastName, TermName, AdjustmentType, Void_Indicator FROM ( Select c.amount amount, c.comments comments, p.firstname firstname, p.lastname lastname, e.termname termname, c.adjustmenttype adjustmenttype, b.voidindicator void_indicator From ChargesDTL c, ChargesHDR b, PeopleHDL p, TermMaster e Where ( b.studentnumber=p.studentnumber and c.termid = e.term_id and b.adjustmentnumber=c.adjustmentnumber and p.personID=b.personID and b.locationcode='12' ) Union Select c.amount amount, c.comments comments, p1.firstname firstname, p1.lastname lastname, e.termname termname, c.adjustmenttype adjustmenttype, b.voidindicator void_indicator From ChargesDTL c, ChargesHDR b, PeopleHDL p1, TermMaster e Where ( b.studentnumber=p.studentnumber and c.termid = e.term_id and b.adjustmentnumber=c.adjustmentnumber and p1.personId = b.personId and b.locationcode='13' ) order by adjustmenttype )

2 个答案:

答案 0 :(得分:1)

正如我在你对你的问题的评论中提到的那样。您的查询可以使用一组连接语句轻松地缩减为一个查询。 Union和Derived表是完全没必要的。

SELECT
    c.amount,
    c.comments,
    p.firstname,
    p.lastname,
    e.termname,
    c.adjustmenttype,
    b.voidindicator
FROM ChargesDTL c
INNER JOIN TermMaster e ON e.termid = c.term_id 
INNER JOIN ChargesHDR b ON b.adjustmentnumber = c.adjustmentnumber
INNER JOIN PeopleHDL p ON p.personID = b.personID AND p.studentnumber = b.studentnumber
WHERE
    (b.locationcode = '12' OR b.locationcode = '13')
ORDER BY c.adjustmenttype

答案 1 :(得分:0)

这样做你想要的吗?

Select  c.amount amount,
    c.comments comments,
    p1.firstname firstname,
    p1.lastname lastname,
    e.termname termname,
    c.adjustmenttype adjustmenttype,
    b.voidindicator void_indicator
From ChargesDTL c,
     ChargesHDR b
     on b.adjustmentnumber = c.adjustmentnumber join
    PeopleHDL p
    on b.studentnumber = p.studentnumber and
       p.personId = b.personId join
    TermMaster e
    on c.termid = e.term_id
Whereb.locationcode in ('12', '13')
order by adjustmenttype;

您应该学会使用正确的,明确的join语法。几十年来,逗号语法已经过时(但完全支持)。