polardb/polardbx-sql/polardbx-pagination
- community
Checking that the SKILL.md file is still in the repository.
Efficient pagination and large table traversal for PolarDB-X 2.0 Enterprise Edition. Recommends Keyset pagination (cursor pagination) over LIMIT M,N for deep pagination scenarios, provides per-shard traversal for extreme cases, includes index requirements and complete Java code examples. Use when implementing pagination queries, traversing large tables, exporting data in batches, or optimizing slow LIMIT M,N queries on PolarDB-X. Triggers: "分页查询", "deep pagination", "Keyset pagination", "大表遍历", "数据导出", "LIMIT优化", "分页性能", "cursor pagination", "游标分页", "翻页慢", "深翻页", "pagination query", "batch traversal", "LIMIT M N slow", "efficient paging"
Install
npx vskill@latest install polardb/polardbx-sql/polardbx-paginationSKILL.md
PolarDB-X Efficient Pagination — Keyset Pagination & Large Table Traversal
Implement efficient pagination for PolarDB-X 2.0 Enterprise Edition (AUTO mode) that maintains constant performance regardless of page depth. Covers Keyset pagination strategies, per-shard traversal, index requirements, and production-ready Java code.
Scope: PolarDB-X 2.0 Enterprise Edition + AUTO mode database only.
Why LIMIT M, N Fails for Deep Pagination
- Standalone DB cost: O(M+N) — must scan M rows before returning N rows.
- Distributed DB cost: O(M+N) × number of shards — each shard returns M+N rows to CN for merge-sort.
- Result: Performance degrades linearly as page number increases; unacceptable for large tables.
For small data volumes with shallow pagination (< 1000 offset),LIMIT M, Nis acceptable.
Core Workflow
- Determine the pagination scenario:
- Does the table use New Sequence (AUTO mode default)? → Scenario A
- Is the sort column potentially non-unique (time column, group sequence ID)? → Scenario B
- Are there extreme stability requirements with many shards? → Per-shard traversal
- Select the pagination strategy (see Quick Reference below).
- Verify index exists for the sort columns (critical for performance).
- Generate code with proper JDBC settings for production use.
Pagination Strategies Quick Reference
Scenario A: New Sequence ID (AUTO mode default)
ID is globally ordered → represents write time order:
Cost: O(N) constant, regardless of page depth.
Scenario B: Sort Column May Have Duplicates (time columns, etc.)
Use (sort_column, id) tuple comparison as cursor:
PolarDB-X supports tuple comparison (col1, col2) > (?, ?) and can leverage composite indexes.
Per-Shard Traversal (Advanced)
For extreme scenarios (many shards, relaxed ordering):
Index Requirements
| Sort Method | Required Index | |
|---|---|---|
ORDER BY id | Primary key (usually exists) | |
ORDER BY gmt_create, id | (gmt_create, id) composite index | |
ORDER BY c1, gmt_create, id (with WHERE c1 = ?) | (c1, gmt_create, id) composite index |
Method Comparison
| Method | Performance | Applicable Scenarios | Notes | |
|---|---|---|---|---|
LIMIT M, N | O(M+N), degrading | Shallow pagination, small data | Even worse in distributed systems | |
| Keyset (id) | O(N), constant | AUTO mode, traverse in write order | Requires globally ordered id | |
| Keyset (sort_col, id) | O(N), constant | Sort columns with possible duplicates | Requires composite index | |
| Per-shard traversal | O(N), constant | Many shards, relaxed ordering | Requires SHOW TOPOLOGY + HINT | |
| Batch Tool | Internally optimized | Data export | Dedicated tool |
Java JDBC Settings
| Parameter | Value | Reason | |
|---|---|---|---|
netTimeoutForStreamingResults | 0 | Avoid streaming read timeouts | |
socketTimeout | As needed (ms) | Avoid long queries being disconnected | |
setFetchSize | Integer.MIN_VALUE | Enable streaming reads (avoid OOM) | |
autocommit | true | Avoid creating long transactions |
Best Practices
- Never use LIMIT M,N for deep pagination on large tables — cost grows linearly.
- Always create composite indexes matching your ORDER BY columns.
- Use tuple comparison
(col, id) > (?, ?)for non-unique sort columns — PolarDB-X supports this natively. - Keep autocommit=true for traversal — pagination is long-running; don't create long transactions.
- Use streaming reads (
fetchSize = Integer.MIN_VALUE) to avoid loading entire result sets into memory. - Consider Batch Tool for pure data export scenarios — it has built-in PolarDB-X optimizations.
Reference
| Reference | Description | |
|---|---|---|
| references/pagination-best-practice.md | Complete pagination guide: all scenarios, per-shard traversal details, full Java code example, Batch Tool, FAQ |
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingpolardbx-executor/src/main/resources/ai/skills/polardbx-pagination/SKILL.md
Popularity
- Repository stars
- 1,687
- Repository forks
- 338
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/polardb/polardbx-sql/polardbx-pagination)