Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if there is more data after a query with LIMIT?

I am using LIMIT and OFFSET in InfluxQL queries to handle pagination.

For example, in a measurement with 3 rows.

> SELECT *::field FROM i2QYtZBVSnuXjLhQhuAV6w;
name: i2QYtZBVSnuXjLhQhuAV6w
time                 hello
----                 -----
2018-02-23T18:00:00Z 1000
2018-02-23T18:30:00Z 1200
2018-02-23T19:00:00Z 990

Supposing I read the rows one by one using LIMIT and OFFSET:

> SELECT *::field FROM i2QYtZBVSnuXjLhQhuAV6w LIMIT 1;
name: i2QYtZBVSnuXjLhQhuAV6w
time                 hello
----                 -----
2018-02-23T18:00:00Z 1000
> SELECT *::field FROM i2QYtZBVSnuXjLhQhuAV6w LIMIT 1 OFFSET 1;
name: i2QYtZBVSnuXjLhQhuAV6w
time                 hello
----                 -----
2018-02-23T18:30:00Z 1200
> SELECT *::field FROM i2QYtZBVSnuXjLhQhuAV6w LIMIT 1 OFFSET 2;
name: i2QYtZBVSnuXjLhQhuAV6w
time                 hello
----                 -----
2018-02-23T19:00:00Z 990

Is there a way to know that there is no more data after that without performing an additional query?

EDIT: My use case is generating a "next page token" for a user-facing REST API. I'd like to avoid giving the user a token that will just return an empty row set.

like image 434
LodeRunner Avatar asked Feb 23 '18 22:02

LodeRunner


1 Answers

To avoid the additional query, you can always make the DB query using limit as (limit + 1).

Ex : Set limit as 6, if the actual limit is 5 and give the next page token only if the result set has 6 rows. Also, return the data only for 5 rows to the client.

like image 123
Deva Gerald Avatar answered Oct 15 '22 01:10

Deva Gerald