Paginated output

This section provides recommendations for organizing paginated data output.

To organize paginated output, we recommend selecting data sorted by primary key sequentially, limiting the number of rows with the LIMIT keyword.

The query in listing 1 demonstrates the recommended way to organize paginated output.

Note

$lastCity, $lastNumber: Primary key values obtained from the previous query.

Listing 1: Query for organizing paginated output

  1. -- Table `schools`:
  2. -- ┌─────────┬─────────┬─────┐
  3. -- | Name | Type | Key |
  4. -- ├─────────┼─────────┼─────┤
  5. -- | city | Utf8? | K0 |
  6. -- | number | Uint32? | K1 |
  7. -- | address | Utf8? | |
  8. -- └─────────┴─────────┴─────┘
  9. DECLARE $limit AS Uint64;
  10. DECLARE $lastCity AS Utf8;
  11. DECLARE $lastNumber AS Uint32;
  12. $part1 = (
  13. SELECT * FROM schools
  14. WHERE city = $lastCity AND number > $lastNumber
  15. ORDER BY city, number LIMIT $limit
  16. );
  17. $part2 = (
  18. SELECT * FROM schools
  19. WHERE city > $lastCity
  20. ORDER BY city, number LIMIT $limit
  21. );
  22. $union = (
  23. SELECT * FROM $part1
  24. UNION ALL
  25. SELECT * FROM $part2
  26. );
  27. SELECT * FROM $union
  28. ORDER BY city, number LIMIT $limit;

Paginated output - 图1

NULL value in key column

In YDB, all columns, including key ones, may have a NULL value. Despite this, using NULL as key column values is highly discouraged, since the SQL standard doesn’t allow NULL to be compared. As a result, concise SQL statements with simple comparison operators won’t work correctly. Instead, you’ll have to use cumbersome statements with IS NULL/IS NOT NULL expressions.

Examples of paginated output implementation