simpledb中如何进行分页
我在 simpledb 中尝试过 offset,但它不像在 MySQL 中那样工作。我想在 PHP 中为我的数据库 API 进行分页,以便将页码和页长发送到查询,它将仅返回该页面的数据。
我怎样才能在 simpledb 中做到这一点?
select * from second
where time_stamp is not null and gibid = '54' and gibview = 'O'
order by time_stamp asc limit $pagelength
偏移量不起作用,因此我无法在查询中添加偏移量。我用 Google 搜索并发现返回了下一个令牌,但我没有得到下一个令牌。如何检查下一个令牌?
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 it 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 Simpledb 团队的说法(这个问题是在他们的论坛中提出的),使用下一个令牌是执行此操作的方法。
如果您想抓取列表中的第 2500-2600 个项目,而不是迭代前 2500 个项目,simpledb 团队建议执行 count(*) 直至 2500,因为它很快,然后从该结果中抓取下一个标记然后发出真正的查询来提取名称和属性。
巧妙地破解了这样一个事实:限制不需要我认为的起始索引,但应该可以节省一些性能。只是想分享这一点。
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.
通过在简单数据库中使用 NextToken 完成
这里 $offset 是将在查询中传递的 nexttoken 字符串。并将返回下一页。
Done By Using NextToken in simple db
Here $offset is nexttoken string which will pass in query. and will return next page.
$pagelenght 应该是:
像这样,第一个数字是来自哪一行,第二个数字是一页中有多少个项目。
$pagelenght should be :
something like this, first number is from which row, and second is number how many items in one page.
我确认 SimpleDB 中的 LIMIT 仅采用一个参数。
可以肯定的是,我尝试了以下查询(域 Person 存在):
“SELECT * FROM Person LIMIT 20, 20”,我得到以下响应:
客户端错误:指定的查询表达式语法无效。
即使未指定限制,SimpleDB 也会应用默认限制 100,最大限制为 2500 行。我想强调 LIMIT 的工作方式与其他数据库不同。 LIMIT 100 意味着您每次将获得 100 个结果,并且使用下一个令牌您将获得另一批 100 个结果(当然前提是有足够的数据)。
进行分页的方法是使用标记。已经获得的令牌可以存储在会话中,因此可以返回到之前的页面。
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 不像其他存储系统那样在其查询语法中支持 OFFSET 子句。例如,如果您在应用程序中使用每页 10 个项目,则在 MySQL 中获取第 4 页的方法如下:
LIMIT 30, 10 子句意味着跳过前 30 条记录并返回其后的 10 条记录。 SimpleDB 没有开箱即用的功能,但您可以通过执行 2 个查询来模拟它。第一个是这个,它计算前 30 个项目:
您的 SimpleDB 客户端库将执行此操作,返回计数以及响应中的 NextToken 元素。现在您可以进行第二个查询来获取第 4 页的项目:
确保将 NextToken 传递到客户端库中,以便从关联的游标恢复查询。使用下一个标记发出的任何查询仅对第一个查询未计数的记录进行操作,从而为您提供隐式偏移量。
来自文章 https://coderwall.com/p/yr- mdg/simpledb-queries 中的编号分页
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:
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:
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:
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