Skip to main content

polardbx-pagination

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"

설치로 이동

소스 정보

저장소
polardb/polardbx-sql
최근 소스 활동
2026년 9월 20일 04:37
감지된 SKILL.md 언어
영어
스타
1,686
포크
337

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

파일 탐색기
3 개 파일

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
polardbx-pagination
description
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"
metadata
{"version":"0.1.0"}
# 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, N` is acceptable. ## Core Workflow 1. **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 2. **Select the pagination strategy** (see Quick Reference below). 3. **Verify index exists** for the sort columns (critical for performance). 4. **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: ```sql -- First batch SELECT * FROM t1 ORDER BY id LIMIT 1000; -- Subsequent batches (record last_id from previous batch) SELECT * FROM t1 WHERE id > :last_id ORDER BY id LIMIT 1000; ``` 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: ```sql -- First batch SELECT * FROM t1 ORDER BY gmt_create, id LIMIT 1000; -- Subsequent batches (record last_gmt_create and last_id) SELECT * FROM t1 WHERE (gmt_create, id) > (:last_gmt_create, :last_id) ORDER BY gmt_create, id LIMIT 1000; ``` PolarDB-X supports tuple comparison `(col1, col2) > (?, ?)` and can leverage composite indexes. ### Per-Shard Traversal (Advanced) For extreme scenarios (many shards, relaxed ordering): ```sql -- 1. Get topology SHOW TOPOLOGY FROM t1; -- 2. Paginate within each shard using HINT /*+TDDL:NODE('partition_name')*/ SELECT * FROM t1 WHERE (gmt_create, id) > (?, ?) ORDER BY gmt_create, id LIMIT 1000; ``` ## 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 | ```sql -- Create composite index for pagination ALTER TABLE t1 ADD INDEX idx_page (gmt_create, id); ``` ## 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 1. **Never use LIMIT M,N for deep pagination** on large tables — cost grows linearly. 2. **Always create composite indexes** matching your ORDER BY columns. 3. **Use tuple comparison** `(col, id) > (?, ?)` for non-unique sort columns — PolarDB-X supports this natively. 4. **Keep autocommit=true** for traversal — pagination is long-running; don't create long transactions. 5. **Use streaming reads** (`fetchSize = Integer.MIN_VALUE`) to avoid loading entire result sets into memory. 6. **Consider Batch Tool** for pure data export scenarios — it has built-in PolarDB-X optimizations. ## Reference | Reference | Description | |-----------|-------------| | [references/pagination-best-practice.md](references/pagination-best-practice.md) | Complete pagination guide: all scenarios, per-shard traversal details, full Java code example, Batch Tool, FAQ |
GitHub에서 보기