GithubHelp home page GithubHelp logo

cockroachdb-keyset-pagination-example's Introduction

CockroachDB keyset pagination example

NOTE: This is unofficial - no warranties. For official docs, see https://www.cockroachlabs.com/docs.

Uses CockroachDB and the employees data set.

Based on descriptions of keyset pagination (aka the "seek method") in:

https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/

https://blog.jooq.org/2013/10/26/faster-sql-paging-with-jooq-using-the-seek-method/

Dependencies

Written in Python 3. Uses the following libraries:

Install with:

$ pip3 install psycopg2

Get started

Start a local cluster with:

$ cockroach demo

Start the SQL shell:

$ cockroach sql --insecure

From the SQL shell, import the employees data set as follows:

> CREATE DATABASE employees;
> USE employees;
> IMPORT MYSQLDUMP 'https://s3-us-west-1.amazonaws.com/cockroachdb-movr/datasets/employees-db/mysqldump/employees-full.sql.gz';

Back in the UNIX shell, run the script as shown below. Make sure to pass the port the demo cluster is using to the script (in this case, it's 49459).

$ python3 pagination.py -p 49459 # or --port

You should see output like:

DEBUG:root:------------------------------------------------
DEBUG:root:paginate_aux.count = 0
DEBUG:root:paginate_aux.page_count = 8
DEBUG:root:sql:
SELECT * FROM employees
  WHERE employees.emp_no > 10000
  ORDER BY employees.emp_no ASC
  LIMIT 5;

DEBUG:root:paginate_aux(10000, 5) -->
DEBUG:root:        {'emp_no': 10001, 'birth_date': datetime.date(1953, 9, 2), 'first_name': 'Georgi', 'last_name': 'Facello', 'gender': 'M', 'hire_date': datetime.date(1986, 6, 26)}
DEBUG:root:        {'emp_no': 10002, 'birth_date': datetime.date(1964, 6, 2), 'first_name': 'Bezalel', 'last_name': 'Simmel', 'gender': 'F', 'hire_date': datetime.date(1985, 11, 21)}
DEBUG:root:        {'emp_no': 10003, 'birth_date': datetime.date(1959, 12, 3), 'first_name': 'Parto', 'last_name': 'Bamford', 'gender': 'M', 'hire_date': datetime.date(1986, 8, 28)}
DEBUG:root:        {'emp_no': 10004, 'birth_date': datetime.date(1954, 5, 1), 'first_name': 'Chirstian', 'last_name': 'Koblick', 'gender': 'M', 'hire_date': datetime.date(1986, 12, 1)}
DEBUG:root:        {'emp_no': 10005, 'birth_date': datetime.date(1955, 1, 21), 'first_name': 'Kyoichi', 'last_name': 'Maliniak', 'gender': 'M', 'hire_date': datetime.date(1989, 9, 12)}
DEBUG:root:------------------------------------------------
DEBUG:root:paginate_aux.count = 1
DEBUG:root:paginate_aux.page_count = 8
DEBUG:root:sql:
SELECT * FROM employees
  WHERE employees.emp_no > 10005
  ORDER BY employees.emp_no ASC
  LIMIT 5;

DEBUG:root:paginate_aux(10005, 5) -->
DEBUG:root:        {'emp_no': 10006, 'birth_date': datetime.date(1953, 4, 20), 'first_name': 'Anneke', 'last_name': 'Preusig', 'gender': 'F', 'hire_date': datetime.date(1989, 6, 2)}
DEBUG:root:        {'emp_no': 10007, 'birth_date': datetime.date(1957, 5, 23), 'first_name': 'Tzvetan', 'last_name': 'Zielinski', 'gender': 'F', 'hire_date': datetime.date(1989, 2, 10)}
DEBUG:root:        {'emp_no': 10008, 'birth_date': datetime.date(1958, 2, 19), 'first_name': 'Saniya', 'last_name': 'Kalloufi', 'gender': 'M', 'hire_date': datetime.date(1994, 9, 15)}
DEBUG:root:        {'emp_no': 10009, 'birth_date': datetime.date(1952, 4, 19), 'first_name': 'Sumant', 'last_name': 'Peac', 'gender': 'F', 'hire_date': datetime.date(1985, 2, 18)}
DEBUG:root:        {'emp_no': 10010, 'birth_date': datetime.date(1963, 6, 1), 'first_name': 'Duangkaew', 'last_name': 'Piveteau', 'gender': 'F', 'hire_date': datetime.date(1989, 8, 24)}

... snip ...

OK: First 40 IDs returned match expected output

cockroachdb-keyset-pagination-example's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.