简体   繁体   English

如何优化此T-sql查询?

[英]How to optimize this T-sql query?

There is a loop in this query, in the last where condition. 该查询的最后一个where条件中存在一个循环。 and this causes a severe problem to the performance of SQL. 这会严重影响SQL的性能。

I have no idea about how to modify it. 我不知道如何修改它。

select pr.tavpun
 from mta110 pr
 where pr.taisoc = mta110.taisoc
   and pr.taitar = mta110.taitar
   and pr.taydat = mta110.taydat
   and pr.tairef = mta110.tairef
   and pr.tatind = (select max(pr2.tatind) from mta110 pr2
                    where pr2.taisoc = mta110.taisoc
                      and pr2.taitar = mta110.taitar
                      and pr2.taydat = mta110.taydat
                      and pr2.tairef = mta110.tairef
                      and pr2.tatind <= mgc100.gntind)) AS SalesPrice

Your query makes little sense, because pr is not a reasonable alias for mta110 , and mta110 is not recognized in the outer query. 您的查询毫无意义,因为pr不是mta110的合理别名,并且mta110在外部查询中无法识别。

I speculate that you have two tables, pr and mta110 which are joined and you want the "most recent" row of mta110 for each matching row. 我推测您有两个表prmta110 ,并且您希望每个匹配行的mta110为“最新”行。

If this interpretation is correct, then you can use row_number() and a proper join : 如果这个解释是正确的,那么你可以使用row_number()和适当的join

select . . .
from pr join
     (select m.*,
             row_number() over (partition by taisoc, taitar, taydat, tairef order by gntind desc) as seqnum
      from mta110 m
     ) m
     on pr.? = m.?
where seqnum = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM