0

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

9
  • 1
    This question is similar to: Select query with offset limit is much too slow. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 25 at 7:07
  • Thank you. I saw this - but it is for postgresql. And I am asking specifically for HSQLDB because the doc says that indexes would be used by hsqldb. I edited my question with the doc excerpt. Commented Jul 25 at 7:24
  • It says it is using the index; see the ORDER BY section. It still needs to scan forward 80,000 rows to find the starting point. What in the execution plan do you think is wrong? Commented Jul 25 at 14:15
  • Why does it not use the index to get the 80,000 st record and then fetch with the limit only 25 records? I thought with an index it is not necessary to scan all records before the offset ? Commented Jul 25 at 18:40
  • The HSQLDB doc says: " 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,". My interpretation is that the query should not scan forward through 80,000 records ?! Commented Jul 25 at 18:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.