Skip to content

Commit b2c6d9d

Browse files
committed
Give pastable examples of older paging syntax
1 parent 1f6596c commit b2c6d9d

File tree

1 file changed

+76
-73
lines changed

1 file changed

+76
-73
lines changed

doc/api.md

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11807,103 +11807,106 @@ See [Oracle Database Objects and Collections](#objects).
1180711807

1180811808
#### <a name="pagingdata"></a> 17.1.8 Limiting Rows and Creating Paged Datasets
1180911809

11810-
Query data is commonly broken into one or more sets:
11810+
Query data is commonly fetched in one or more batches of rows:
1181111811

11812-
- To give an upper bound on the number of rows that a query has to process,
11813-
which can help improve database scalability.
11812+
- For fetching all data in small sets to process when the number of records is
11813+
too large for Node.js to handle at the same time. This can be handled by
11814+
[ResultSets](#resultsethandling) or [`queryStream()`](#querystream) with one
11815+
execution of the SQL query.
1181411816

1181511817
- To perform 'Web pagination' that allows moving from one set of rows to a next,
1181611818
or previous, set on demand.
1181711819

11818-
- For fetching of all data in consecutive small sets for batch processing. This
11819-
happens because the number of records is too large for Node.js to handle at the
11820-
same time.
11821-
11822-
The latter can be handled by [ResultSets](#resultsethandling) or
11823-
[`queryStream()`](#querystream) with one execution of the SQL query as
11824-
discussed in those links.
11820+
- To give an upper bound on the number of rows that a query has to process,
11821+
which can help improve database scalability.
1182511822

1182611823
'Web pagination' and limiting the maximum number of rows are discussed in this
1182711824
section. For each 'page' of results, a SQL query is executed to get the
1182811825
appropriate set of rows from a table. Since the query will be executed more
11829-
than once, make sure to use [bind variables](#bind) for row numbers and row
11830-
limits.
11826+
than once, make sure to use [bind variables](#bind) for the starting row and
11827+
the number of rows.
1183111828

11832-
Oracle Database 12c SQL introduced an `OFFSET` / `FETCH` clause which
11833-
is similar to the `LIMIT` keyword of MySQL. See [Row Limiting:
11834-
Examples][5] in the Oracle documentation. A node-oracledb example is:
11829+
Techniques include:
1183511830

11836-
```javascript
11837-
const myoffset = 0; // do not skip any rows (start at row 1)
11838-
const mymaxnumrows = 20; // get 20 rows
11831+
- For Oracle Database 12c, use the `OFFSET` / `FETCH` syntax. This is similar
11832+
to the `LIMIT` keyword of MySQL. See [Row Limiting: Examples][5] in the
11833+
Oracle documentation. A node-oracledb example is:
1183911834

11840-
const result = await connection.execute(
11841-
`SELECT last_name
11842-
FROM employees
11843-
ORDER BY last_name
11844-
OFFSET :offset ROWS FETCH NEXT :maxnumrows ROWS ONLY`,
11845-
{ offset: myoffset, maxnumrows: mymaxnumrows },
11846-
{ prefetchRows: mymaxnumrows + 1, fetchArraySize: mymaxnumrows }
11847-
);
11848-
```
11835+
```javascript
11836+
const myoffset = 0; // do not skip any rows (start at row 1)
11837+
const mymaxnumrows = 20; // get 20 rows
1184911838

11850-
A runnable example is in [rowlimit.js][84].
11839+
const sql = `SELECT last_name
11840+
FROM employees
11841+
ORDER BY last_name
11842+
OFFSET :offset ROWS FETCH NEXT :maxnumrows ROWS ONLY`;
1185111843

11852-
You can use a basic [`execute()`](#execute) or a
11853-
[ResultSet](#resultsetclass), or [`queryStream()`](#querystream) with
11854-
your query. For basic `execute()` fetches, make sure that
11855-
`oracledb.maxRows` is greater than the value bound to `:maxnumrows`,
11856-
or set to 0 (meaning unlimited).
11844+
const result = await connection.execute(
11845+
sql,
11846+
{ offset: myoffset, maxnumrows: mymaxnumrows },
11847+
{ prefetchRows: mymaxnumrows + 1, fetchArraySize: mymaxnumrows }
11848+
);
11849+
```
1185711850

11858-
In applications where the SQL query is not known in advance, this
11859-
method sometimes involves appending the `OFFSET` clause to the 'real'
11860-
user query. Be very careful to avoid SQL injection security issues.
11851+
A runnable example is in [rowlimit.js][84].
1186111852

11862-
As an anti-example, another way to limit the number of rows returned
11863-
involves setting [`maxRows`](#propdbmaxrows). However it is more
11864-
efficient to let Oracle Database do the row selection in the SQL query
11865-
and only return the exact number of rows required to node-oracledb.
11853+
You can use a basic [`execute()`](#execute) or a
11854+
[ResultSet](#resultsetclass), or [`queryStream()`](#querystream) with
11855+
your query. For basic `execute()` fetches, make sure that
11856+
`oracledb.maxRows` is greater than the value bound to `:maxnumrows`,
11857+
or set to 0 (meaning unlimited).
1186611858

11867-
For Oracle Database 11g and earlier there are several alternative ways
11868-
to limit the number of rows returned. The old, canonical paging query
11869-
is:
11859+
In applications where the SQL query is not known in advance, this
11860+
method sometimes involves appending the `OFFSET` clause to the 'real'
11861+
user query. Be very careful to avoid SQL injection security issues.
1187011862

11871-
```SQL
11872-
SELECT *
11873-
FROM (SELECT a.*, ROWNUM AS rnum
11874-
FROM (YOUR_QUERY_GOES_HERE -- including the order by) a
11875-
WHERE ROWNUM <= MAX_ROW)
11876-
WHERE rnum >= MIN_ROW
11877-
```
11863+
- For Oracle Database 11g and earlier there are several alternative ways
11864+
to limit the number of rows returned. The old, canonical paging query
11865+
is:
1187811866

11879-
Here, `MIN_ROW` is the row number of first row and `MAX_ROW` is the
11880-
row number of the last row to return. For example:
11867+
```SQL
11868+
SELECT *
11869+
FROM (SELECT a.*, ROWNUM AS rnum
11870+
FROM (YOUR_QUERY_GOES_HERE -- including the order by) a
11871+
WHERE ROWNUM <= MAX_ROW)
11872+
WHERE rnum >= MIN_ROW
11873+
```
1188111874

11882-
```SQL
11883-
SELECT *
11884-
FROM (SELECT a.*, ROWNUM AS rnum
11885-
FROM (SELECT last_name FROM employees ORDER BY last_name) a
11886-
WHERE ROWNUM <= 20)
11887-
WHERE rnum >= 1
11888-
```
11875+
Here, `MIN_ROW` is the row number of first row and `MAX_ROW` is the row number
11876+
of the last row to return. Using the same bind values definitions as
11877+
previously, an example is:
1188911878

11890-
This always has an 'extra' column, here called RNUM.
11879+
```javascript
11880+
const sql = `SELECT *
11881+
FROM (SELECT a.*, ROWNUM AS rnum
11882+
FROM (SELECT last_name FROM employees ORDER BY last_name) a
11883+
WHERE ROWNUM <= :maxnumrows + :offset)
11884+
WHERE rnum >= :offset + 1`;
11885+
```
1189111886

11892-
An alternative and preferred query syntax for Oracle Database 11g uses
11893-
the analytic `ROW_NUMBER()` function. For example to get the 1st to
11894-
20th names the query is:
11887+
This always has an 'extra' column, here called RNUM.
1189511888

11896-
```SQL
11897-
SELECT last_name FROM
11898-
(SELECT last_name,
11899-
ROW_NUMBER() OVER (ORDER BY last_name) AS myr
11900-
FROM employees)
11901-
WHERE myr BETWEEN 1 and 20
11902-
```
11889+
- An alternative, preferred query syntax for Oracle Database 11g uses the
11890+
analytic `ROW_NUMBER()` function. For example:
11891+
11892+
```javascript
11893+
const sql = `SELECT last_name
11894+
FROM (SELECT last_name,
11895+
ROW_NUMBER() OVER (ORDER BY last_name) AS myr
11896+
FROM employees)
11897+
WHERE myr BETWEEN :offset + 1 and :maxnumrows + :offset`;
11898+
```
11899+
11900+
Refer to [On Top-n and Pagination Queries][85] in Oracle Magazine for
11901+
details.
11902+
11903+
As an anti-example, another way to limit the number of rows returned
11904+
involves setting [`maxRows`](#propdbmaxrows). However it is more
11905+
efficient to let Oracle Database do the row selection in the SQL query
11906+
and only fetch the exact number of rows required from the database.
1190311907

11904-
Refer to [On Top-n and Pagination Queries][85] in Oracle Magazine for
11905-
details. Also review the videos [SQL for pagination queries - memory and
11906-
performance][166] and [SQL for pagination queries - advanced options][167].
11908+
The videos [SQL for pagination queries - memory and performance][166] and [SQL
11909+
for pagination queries - advanced options][167] are worth reviewing.
1190711910

1190811911
#### <a name="autoincrement"></a> 17.1.9 Auto-Increment Columns
1190911912

0 commit comments

Comments
 (0)