为什么要获取前10行并选择所有行相同?

时间:2014-09-26 04:00:52

标签: sql db2

我在db2下面查询并希望从结果中获取前10行,但是我注意到执行时间和解释成本相同,仅获取10行并获取所有行,为什么会发生这种情况?

select this_.RIGHTSID as y0_
     , this_.RIGHTSNUM as y1_
     , rightstype1_.NAME as y2_
     , rightsstat3_.NAME as y3_
     , this_.ISSUEDATE as y4_
     , this_.EXPIRYDATE as y5_
     , licensee4_.NAME as y6_
     , agency2_.NAME as y7_
     , agency2_.AGENCYID as y8_
     , this_.EFFECTIVEDATE as y9_
     , rightstype1_.RIGHTSTYPEID as y10_ 
from RIGHTS_TB this_ 
inner join RIGHTS_TYPE_TB rightstype1_ on this_.RIGHTSTYPEID=rightstype1_.RIGHTSTYPEID
inner join AGENCY_TB agency2_ on rightstype1_.AGENCYID=agency2_.AGENCYID 
inner join RIGHTS_STATUS_TB rightsstat3_ on this_.RIGHTSSTATUSID=rightsstat3_.RIGHTSSTATUSID 
inner join LICENSEE_TB licensee4_ on this_.LICENSEEID=licensee4_.LICENSEEID 
where this_.ACTIVE_IND='Y'
order by this_.ISSUEDATE desc 
    fetch first 10 rows only

1 个答案:

答案 0 :(得分:3)

简单来说,所有这些都是执行的:

select this_.RIGHTSID as y0_
     , this_.RIGHTSNUM as y1_
     , rightstype1_.NAME as y2_
     , rightsstat3_.NAME as y3_
     , this_.ISSUEDATE as y4_
     , this_.EXPIRYDATE as y5_
     , licensee4_.NAME as y6_
     , agency2_.NAME as y7_
     , agency2_.AGENCYID as y8_
     , this_.EFFECTIVEDATE as y9_
     , rightstype1_.RIGHTSTYPEID as y10_ 
FROM RIGHTS_TB this_ 
    inner join RIGHTS_TYPE_TB rightstype1_ on this_.RIGHTSTYPEID=rightstype1_.RIGHTSTYPEID
    inner join AGENCY_TB agency2_ on rightstype1_.AGENCYID=agency2_.AGENCYID 
    inner join RIGHTS_STATUS_TB rightsstat3_ on this_.RIGHTSSTATUSID=rightsstat3_.RIGHTSSTATUSID 
    inner join LICENSEE_TB licensee4_ on this_.LICENSEEID=licensee4_.LICENSEEID 
WHERE this_.ACTIVE_IND='Y' 
ORDER BY this_.ISSUEDATE desc 

之前可以执行此操作:

fetch first 10 rows only

在完成完整的FROM和WHERE子句之前,不可能知道允许哪些记录,然后在ORDER完成之前你不知道哪个记录适合FIRST n

因此,执行时间相似并不令人惊讶。