2017-05-24 127 views
-1

存在一個加速下面的sql查詢的機會?SQL優化(group by和max)

select 
    max(xtrid) as xtrid 
    , jid 
from jpltab jl 
    inner join rb_cust u 
    on jl.custid = u.custid 
where jl.tpe = 'Y' 
    and jl.jid in (51, 52, 53, 54, 55) 
    and u.org = 'INVCE' 
group by jid 
order by xtrid desc; 

感謝

+1

添加索引.... –

+0

您能否包含執行計劃? – VDK

+0

由於您的Where子句中的jl.jid具有連續的數字,請將其更改爲「介於51和55之間」,而不是「IN(51,52,53,54,55)」。這應該會讓你有更好的表現。看到這裏... https://stackoverflow.com/questions/3308280/is-there-a-performance-difference-between-between-and-in-with-mysql-or-in-sql-in –

回答

-3

你可以拿出 'order by xtrid desc' 因爲你已經選擇了最大

+0

爲每個組選擇最大值... –

+0

是...您不需要按組排序值,以便爲每個組選擇最大值。 – heyhey

1

這是您的查詢:

select jl.jid, max(xtrid) as xtrid 
from jpltab jl inner join 
    rb_cust u 
    on jl.custid = u.custid 
where jl.tpe = 'Y' and 
     jl.jid in (51, 52, 53, 54, 55) and 
     u.org = 'INVCE' 
group by jl.jid 
order by xtrid desc; 

我將開始與索引。想到的是jpltab(tpe, jid, custid)rb_cust(custid, org)