Skip to content

Commit 1f36157

Browse files
committed
first rows
1 parent c081658 commit 1f36157

File tree

9 files changed

+787
-0
lines changed

9 files changed

+787
-0
lines changed

optimizer/first_rows/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
This demonstrates patch for Bug# 22174392. Oracle Database 12.2.0.1 was used in the demonstration.
2+
3+
It improves the costing of FIRST ROWS type queries so that the optimizer makes better use of indexes to sort returned rows.
4+
5+
This is important for applications that return rows (in sorted order) to users that paginate through the result set.
6+
7+
See scripts before.sql and after.sql - spooled 'lst' output files are provided.
8+
9+
Compare the cost in the before and after versions, and also compare the differences in the resulting plans. The costs in the after example (for the FETCH FIRST) are must lower and compare favorably with the ROWNUM queries.
10+
11+
DISCLAIMER:
12+
<br/>-- These scripts are provided for educational purposes only.
13+
<br/>-- They are NOT supported by Oracle World Wide Technical Support.
14+
<br/>-- The scripts have been tested and they appear to work as intended.
15+
<br/>-- You should always run scripts on a test instance.
16+
<br/>
17+
WARNING:
18+
<br/>-- The scripts will drop a table called "T" - use on test database only
19+

optimizer/first_rows/after.lst

Lines changed: 338 additions & 0 deletions
Large diffs are not rendered by default.

optimizer/first_rows/after.sql

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@@fixon
2+
3+
set pagesize 1000 linesize 350 trims on
4+
column owner format a10
5+
column edition_name format a10
6+
column object_name format a20
7+
column subobject_name format a20
8+
set timing on
9+
select * from
10+
( select *
11+
from t
12+
order by object_id desc )
13+
where rownum <= 10;
14+
15+
@plan
16+
17+
select * from
18+
( select *
19+
from t
20+
order by object_id desc )
21+
where rownum <= 10;
22+
23+
@plan
24+
25+
select *
26+
from t
27+
order by object_id asc
28+
fetch first 10 rows only;
29+
30+
@plan
31+
32+
select *
33+
from t
34+
order by object_id asc
35+
fetch first 10 rows only;
36+
37+
@plan
38+
39+
select *
40+
from t
41+
order by object_id asc
42+
offset 10 rows fetch first 10 rows only;
43+
44+
@plan

optimizer/first_rows/before.lst

Lines changed: 332 additions & 0 deletions
Large diffs are not rendered by default.

optimizer/first_rows/before.sql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
set pagesize 1000 linesize 350 trims on
2+
column owner format a10
3+
column edition_name format a10
4+
column object_name format a20
5+
column subobject_name format a20
6+
set timing on
7+
select * from
8+
( select *
9+
from t
10+
order by object_id desc )
11+
where rownum <= 10;
12+
13+
@plan
14+
15+
select * from
16+
( select *
17+
from t
18+
order by object_id desc )
19+
where rownum <= 10;
20+
21+
@plan
22+
23+
select *
24+
from t
25+
order by object_id asc
26+
fetch first 10 rows only;
27+
28+
@plan
29+
30+
select *
31+
from t
32+
order by object_id asc
33+
fetch first 10 rows only;
34+
35+
@plan
36+
37+
select *
38+
from t
39+
order by object_id asc
40+
offset 10 rows fetch first 10 rows only;
41+
42+
@plan

optimizer/first_rows/con

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sqlplus adhoc/adhoc
2+

optimizer/first_rows/fixoff.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
alter session set "_fix_control"='22174392:OFF';
2+

optimizer/first_rows/fixon.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
alter session set "_fix_control"='22174392:ON';
2+

optimizer/first_rows/plan.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
column plan_table_output format a100
2+
3+
SELECT *
4+
FROM table(DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'TYPICAL -PREDICATE'));
5+
6+

0 commit comments

Comments
 (0)