奇怪的AS400数据库

select distinct userpo1_.ID as ID60_, userpo1_.CUST_ID as CUST2_60_, userpo1_.CARD_TYPE as CARD3_60_, userpo1_.USER_CODE as USER4_60_, userpo1_.LOGIN_PASS as LOGIN5_60_, userpo1_.LOGIN_NAME as LOGIN6_60_, userpo1_.USER_NAME as USER7_60_, userpo1_.GENDER as GENDER60_, userpo1_.CARD_CODE as CARD9_60_, userpo1_.IDENTITY as IDENTITY60_, userpo1_.TEL1 as TEL11_60_, userpo1_.TEL2 as TEL12_60_, userpo1_.REC_FLAG as REC13_60_, userpo1_.CHK_FLAG as CHK14_60_, userpo1_.OPT_TYPE as OPT15_60_, userpo1_.OPT_USERID as OPT16_60_, userpo1_.CHK_USERID as CHK17_60_, userpo1_.LASTUPDATE as LASTUPDATE60_, userpo1_.TEMP1 as TEMP19_60_, userpo1_.TEMP2 as TEMP20_60_ from RUN08110.T_ACCUSR accountuse0_, RUN08110.T_USRINFO userpo1_, RUN08110.T_CUSTOMER customerpo2_ where userpo1_.CUST_ID=customerpo2_.ID and accountuse0_.USER_ID=userpo1_.ID and accountuse0_.CHK_FLAG>0 and userpo1_.IDENTITY>3 and (customerpo2_.CUST_CODE like '%0000000009%') and userpo1_.IDENTITY=7 and accountuse0_.REC_FLAG=1 fetch first 10 rows only
就是这么一个由Hibernate转换出来的SQL语句 在IBM AS400数据库中执行就出现了问题,问题是由于有了fetch以后distinct失效了,本来这条语句的目的是先distinct取出不重复的记录,然后从不重复的记录中取出前面的10条记录,但是在AS400中执行以后结果却取出了重复的结果,其实就是distinct失效了。我认为这应该算这个数据库产品的一个BUG,后来我也解决了问题。解决问题的方法是改写了上面这条语句,先用户distinct取出了不重复的记录,语句变成这样的了:
select  * from run08110.T_USRINFO where id in(select distinct userpo1_.ID as ID60 from RUN08110.T_ACCUSR accountuse0_, RUN08110.T_USRINFO userpo1_, RUN08110.T_CUSTOMER customerpo2_ where userpo1_.CUST_ID=customerpo2_.ID and accountuse0_.USER_ID=userpo1_.ID and accountuse0_.CHK_FLAG>0 and userpo1_.IDENTITY>3 and (customerpo2_.CUST_CODE like '%0000000009%') and userpo1_.IDENTITY=7 and accountuse0_.REC_FLAG=1) fetch first 10 rows only
也就是先用子查询找到不重复的记录,然后再从不重复的记录中取出10条,问题得到解决了,而且查询速度得到了大幅的提升。

你可能感兴趣的:(sql,Hibernate,IBM)