Hsqldb 2.7.4
If I use OFFSET in an index query, the index should be used for offset calculation, as far as I understand the documentation!?
That does not work for me. Any ideas?
SELECT id FROM news ORDER BY id DESC LIMIT 25 offset 25;
--> 17ms
SELECT id FROM news ORDER BY id DESC LIMIT 25 offset 80000;
-->1.31 seconds.
ID is the primary index.
CREATE TABLE NEWS
(
ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
.... other fields );
ALTER TABLE NEWS
ADD PRIMARY KEY (ID);
Explain the plan:
isDistinctSelect=[false]
isGrouped=[false]
isAggregated=[false]
columns=[ COLUMN: PUBLIC.NEWS.ID not nullable
]
[range variable 1
join type=INNER
table=NEWS
cardinality=81400
access=FULL SCAN
join condition = [index=SYS_IDX_SYS_PK_10815_10822
]
]]
order by=[
COLUMN: PUBLIC.NEWS.ID
DESC
uses index]
offset=[
VALUE = 80000, TYPE = INTEGER]
limit=[
VALUE = 25, TYPE = INTEGER]
PARAMETERS=[]
SUBQUERIES[]
Thank you all for any help!
The documentation says: Indexes and ORDER BY, OFFSET and LIMIT HyperSQL can use an index on an ORDER BY clause if all the columns in ORDER BY are in a single-column or multi-column index (in the exact order). This is important if there is a LIMIT n (or FETCH n ROWS ONLY) clause. In this situation, the use of an index allows the query processor to access only the number of rows specified in the LIMIT clause, instead of building the whole result set, which can be huge.
UPDATE: If I do not use offset but a where clause with ID, that also doesn't help:
SELECT id FROM news where id>80000 ORDER BY id DESC limit 25 --> 12ms
SELECT id FROM news where id>80 ORDER BY id DESC limit 25 --> 1.5 seconds