开发者

How to do pagination in simpledb

开发者 https://www.devze.com 2023-02-03 12:59 出处:网络
I have tried offset in simpledb but it\'s not working as it was working in MySQL. I want to do paging for my database API in PHP so that I send the pagenumber and pagelength to the query and i开发者_运

I have tried offset in simpledb but it's not working as it was working in MySQL. I want to do paging for my database API in PHP so that I send the pagenumber and pagelength to the query and i开发者_运维知识库t will return the data of that page only.

How can I do this in simpledb?

    select * from second 
where time_stamp is not null and gibid = '54' and gibview = 'O' 
order by time_stamp asc limit $pagelength 

Offset is not working so I can't add offset in query. I have Googled and find there is next token is returned but I am not getting nexttoken. How to check for nexttoken?


Per the Simpledb team (this question was asked in their forums) using the next token is the way to do it.

If you want to grab, say the 2500-2600th item from a list and not iterate over the first 2500, the simpledb team recommends doing a count(*) up to 2500, cause it is fast, then grabbing the next token from that result and then issuing your real query to pull the names and attributes.

Clever hack to the fact that limit doesn't take a starting index I thought, but should save you some performance. Just wanted to share that.


Done By Using NextToken in simple db

$files = $this->db->select($domain, $query, $offset)

Here $offset is nexttoken string which will pass in query. and will return next page.


$pagelenght should be :

$pagenum = 4; //current page
$numitems = 20; //items per page
$row_from = $pagenum * $numitems - $numitems;
$pagelenght = $row_from.','.$numitems; 


in the end pagelenght should look like this
$pagelenght = '0,20'; //first page
$pagelenght = '20,20'; //second page
$pagelenght = '40,20'; //third page
$pagelenght = 60,20'; //forth page

something like this, first number is from which row, and second is number how many items in one page.


I confirm that LIMIT in SimpleDB only takes one argument.

To be sure, I tried the following query (the domain Person exists): "SELECT * FROM Person LIMIT 20, 20" and I had the following response: Client error : The specified query expression syntax is not valid.

Even if limit is not specified, SimpleDB applies a default limit 100, the maximum limit is 2500 rows. I want to stress out that LIMIT works differently from other databases. LIMIT 100 means that you'll get 100 results per time, and you'll get another batch of 100 results with the next token (provided that there is enough data of course).

The way to do the pagination is use tokens. Already obtained token can be stored in the session, so it will be possible to go back to previous pages.


Amazon SimpleDB doesn't support OFFSET clauses in its query syntax like some other storage systems do. For example, if you're using pages of 10 items each in your app, here is how you might get the 4th page in MySQL:

SELECT * FROM items LIMIT 30, 10

The LIMIT 30, 10 clause means skip the first 30 records and return the 10 after them. SimpleDB doesn't have this out of the box, but you can simulate it by doing 2 queries. The first is this one, which counts up the first 30 items:

SELECT COUNT(*) FROM items LIMIT 30

Your SimpleDB client library will execute this, returning the count and also a NextToken element in the response. Now you can make this second query to get the 4th page of items:

SELECT * FROM items LIMIT 10

Make sure that you pass along the NextToken into your client library so that the query is resumed from the associated cursor. Any query issued with that next token only operates on the records that weren't counted by the first query, giving you an implicit offset.

From article https://coderwall.com/p/yr-mdg/numbered-paging-in-simpledb-queries

0

精彩评论

暂无评论...
验证码 换一张
取 消