Colocated tables Beta

Colocated tables can dramatically increase the number of relations (tables, indexes, etc.) that can be supported per node while keeping the number of tablets per node low.

In workloads that do very little IOPS and have a small data set, the bottleneck shifts from CPU/disk/network to the number of tablets one can host per node. There are practical limitations to the number of tablets that YugabyteDB can handle per node, even though this number could be very high, depending on the workload pattern. Since each table by default requires at least one tablet without colocation, a database with 5000 relations (tables, indexes, etc.) will have at least 5000 tablets, which increases the CPU/disk/network overhead. If most of these relations are small in size, then it’s beneficial to use colocated tables.

Colocating various SQL tables puts all of their data into a single tablet, called the colocation tablet.Note that all the data in the colocation tablet is still replicated across 3 nodes (or whatever the replication factor is).

In this section, we’ll explore creating and using colocated tables. If you haven’t installed YugabyteDB yet, do so first by following the Quick start guide.

1. Create a universe

You can create a universe by following Create local cluster.

2. Create a colocated database

Connect to the cluster using ysqlsh.

  1. $ ./bin/ysqlsh -h 127.0.0.1

Create database with colocated = true option.

  1. yugabyte=# CREATE DATABASE northwind WITH colocated = true;

This will create a database northwind whose tables will be stored on a single tablet.

3. Create tables

Connect to northwind database and create tables using standard CREATE TABLE command.The tables will be colocated on a single tablet since the database was created with colocated = true option.

  1. yugabyte=# \c northwind
  2. yugabyte=# CREATE TABLE customers (
  3. customer_id bpchar,
  4. company_name character varying(40) NOT NULL,
  5. contact_name character varying(30),
  6. contact_title character varying(30),
  7. PRIMARY KEY(customer_id ASC)
  8. );
  9. yugabyte=# CREATE TABLE categories (
  10. category_id smallint,
  11. category_name character varying(15) NOT NULL,
  12. description text,
  13. PRIMARY KEY(category_id ASC)
  14. );
  15. yugabyte=# CREATE TABLE suppliers (
  16. supplier_id smallint,
  17. company_name character varying(40) NOT NULL,
  18. contact_name character varying(30),
  19. contact_title character varying(30),
  20. PRIMARY KEY(supplier_id ASC)
  21. );
  22. yugabyte=# CREATE TABLE products (
  23. product_id smallint,
  24. product_name character varying(40) NOT NULL,
  25. supplier_id smallint,
  26. category_id smallint,
  27. quantity_per_unit character varying(20),
  28. unit_price real,
  29. PRIMARY KEY(product_id ASC),
  30. FOREIGN KEY (category_id) REFERENCES categories,
  31. FOREIGN KEY (supplier_id) REFERENCES suppliers
  32. );

If you go to tables view in master UI, you’ll see that all tables have the same tablet.

customers table

categories table

4. Opt out table from colocation

YugabyteDB has the flexibility to opt a table out of colocation. In this case, the table will use its own set of tabletsinstead of using the same tablet as the colocated database. This is useful for scaling out tables that we know are likelyto be large. You can do this by using colocated = false option while creating table.

  1. yugabyte=# CREATE TABLE orders (
  2. order_id smallint NOT NULL PRIMARY KEY,
  3. customer_id bpchar,
  4. order_date date,
  5. ship_address character varying(60),
  6. ship_city character varying(15),
  7. ship_postal_code character varying(10),
  8. FOREIGN KEY (customer_id) REFERENCES customers
  9. ) WITH (colocated = false);

If you go to tables view in master UI, you’ll see that orders table has its own set of tablets.

orders table

5. Reading and writing data in colocated tables

You can use standard YSQL DML statements to read and write data in colocated tables. YSQL’s query planner and executorwill handle routing the data to the correct tablet.

What’s next?

For more information, see the architecture for colocated tables.