2017-05-24 154 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)