sql - Wrong results in query with ORDER BY and ROWNUM -
i have problem query generated orm framework limit , order results in oracle database 11.2.0.1.0 64bit production. generated select looks this:
select * (select this_.* plate this_ this_.id in (select distinct this_.id y0_ plate this_ ) order this_.name asc) rownum <= 10;
trying understand problem created following sandbox:
create table plate ( id integer primary key, name varchar2(30), description varchar2(255) ); insert plate values ( 1, 'aaa-1234', 'test1' ); insert plate values ( 2, 'bbb-1234', 'test2' ); insert plate values ( 3, 'ccc-1234', 'test3' ); insert plate values ( 4, 'ddd-1234', 'test4' ); commit;
executing select in example returns:
id name description 1 ddd-1234 (null) 2 ddd-1234 (null) 3 ddd-1234 (null) 4 ddd-1234 (null)
in understanding should return:
id name description 1 aaa-1234 test1 2 bbb-1234 test2 3 ccc-1234 test3 4 ddd-1234 test4
what wrong? why not returning expected to?
edited: removing order clause returns expected result. why?
edited 2: execution plan following:
--------------------------------------------------------------------------------- | id | operation | name | rows | bytes | cost (%cpu)| time | --------------------------------------------------------------------------------- | 0 | select statement | | | | 4 (100)| | |* 1 | count stopkey | | | | | | | 2 | view | | 4 | 636 | 4 (25)| 00:00:01 | |* 3 | sort order stopkey| | 4 | 636 | 4 (25)| 00:00:01 | | 4 | table access full | plate | 4 | 636 | 3 (0)| 00:00:01 | --------------------------------------------------------------------------------- predicate information (identified operation id): --------------------------------------------------- 1 - filter(rownum<=10) 3 - filter(rownum<=10) note ----- - dynamic sampling used statement (level=2)
in subquery, there seems strange me:
select this_.* plate **this_** this_.id in (select distinct this_.id y0_ plate **this_** ) order this_.name asc
the this_ alias used twice 2 different selects. maybe causes wrong results. not sure this_ pointing in order clause example. cannot guarantee reason issue seems odd me.
Comments
Post a Comment