2012/05/13

讓 SimpleDB 支援 paging (offset)

在 implement page 的時候通常會用到 SQL 語法的"offset"
取第一頁:
SELECT * FORM `article` OFFSET LIMIT 10
取第二頁:
SELECT * FORM `article` OFFSET LIMIT 10 OFFSET 10
取第三頁:
SELECT * FORM `article` OFFSET LIMIT 10 OFFSET 20

但 AWS SimpleDB 並不支援 offset 這個與法,必須要用 next_token 方式來取
以下用python boto library做個示例:
sdb_conn = SDBConnection(...)

# Skip first 20 items
query = "SELECT COUNT(*) FORM `article` OFFSET LIMIT 20"
rs = sdb_conn.select(sdb_domain, query=query)

# Get target page
query = "SELECT * FORM `article` OFFSET LIMIT 10"
rs2 = sdb_conn.select(sdb_domain, query=query, next_token=rs.next_token)
for item in rs2:
    ...

"SELECT COUNT(*)" 是最有效率的略過方法,如果用"SELECT * "則會有 return 2500 rows 的限制!


 參考資料


1 則留言:


  1. Many businesses are increasingly turning to online resources to expand their reach. One useful tool is the website that offers valuable insights and services. By leveraging such platforms, companies can improve their digital presence and connect with a broader audience. Utilizing a reliable website is essential for success in today's competitive market.

    回覆刪除