View a markdown version of this page

Creating and loading a scaleout table - Amazon Aurora

Creating and loading a scaleout table

In a database cluster group for Aurora, you build a scaleout table from parts that you create in order: one member table for each collocation, one data mapping for each member table, a routing function, and the scaleout table itself. You then verify that every collocation routes and load the member tables yourself. These steps change the group's data mappings.

Before you begin, check the following:

  • You have chosen the routing key, routing function, and number of collocations, as Choosing a routing key and routing function and Planning collocations and headroom describe.

  • Your router user has dbclustergroup_metadata_admin, and the same-named member cluster user can create databases and tables on each member cluster. For more information, see Router users and routing layer privileges.

  • The scaleout table's name is unused in the group. A scaleout table name shares one namespace with the router table names in the data mappings, so it must differ from every router table name and every other scaleout table name. The routing layer rejects a collision.

  • You have exported the mappings, as Exporting the mappings describes.

Example: a scaleout table across two member clusters

The following example distributes an orders table across two member clusters, ALIAS_A and ALIAS_B, by customer_id. It uses two collocations and a MOD routing function.

Important

Plan the number of collocations before you create the routing function

A scaleout table spreads across no more member clusters than its routing function has collocations, and a routing function has no ALTER statement. This example uses two. Don't copy this routing function for production data. Create more collocations than you have member clusters. A later change of routing function changes only the metadata and moves no rows. If the new function sends a row to another collocation, you can't read that row by its routing key. For the steps, see Changing a routing function.

The routing layer doesn't run table DDL in query routing mode. Create each member table on its member cluster, through a direct connection or a pinned session. Then create the data mappings, the routing function, and the scaleout table at the routing endpoint, in a session that isn't pinned. Every member table needs a data mapping before you create the scaleout table, because CREATE SCALEOUT TABLE checks that those mappings exist. Run each mapping statement outside a transaction, and put the mappings of every member table in one multi-row INSERT.

One statement can add, change, or remove at most 1,000 data mappings. For a scaleout table with more than 1,000 member tables, split the mappings across several INSERT statements, and create the scaleout table after the last one. For more information about the limit, see Routing metadata and naming limits.

The routing function statement is the following:

CREATE SCALEOUT ROUTING FUNCTION orders_mod_fn(customer_id INT) AS PARTITION BY mod(customer_id, 2) IN TARGET COLLOCATION ( coll_0 VALUES IN (0), coll_1 VALUES IN (1) );

It can name only collocations that data mappings already created. Under MOD, each collocation takes the remainders that it lists, such as coll_0 VALUES IN (0). The following statements complete the example.

To list your group's member cluster aliases, run the following query at the routing endpoint.

SELECT member_cluster_alias, db_cluster_identifier FROM dbclustergroup_sys.member_cluster_mappings;
To create a scaleout table
  1. On the member cluster of ALIAS_A, through a pinned session, create the database db and the member table db.orders_phys. To use a direct connection instead, connect to the member cluster's endpoint and omit the two SET SESSION lines.

    SET SESSION router_cluster_pin = 'ALIAS_A'; CREATE DATABASE db; CREATE TABLE db.orders_phys (customer_id INT NOT NULL, order_id INT NOT NULL, PRIMARY KEY (customer_id, order_id)); SET SESSION router_cluster_pin = DEFAULT;
  2. On the member cluster of ALIAS_B, run the same statements, pinned to 'ALIAS_B'. Check that the definitions match, as Auditing access and member cluster configuration describes.

  3. At the routing endpoint, with autocommit on, create one data mapping for each member table in one multi-row INSERT. Give each member table its own router table name and collocation. The data mapping creates the collocation.

    INSERT INTO dbclustergroup_sys.data_mappings (cluster_db_name, cluster_table_name, router_db_name, router_table_name, member_cluster_alias, router_collocation_group) VALUES ('db', 'orders_phys', 'db', 'orders_s1', 'ALIAS_A', 'coll_0'), ('db', 'orders_phys', 'db', 'orders_s2', 'ALIAS_B', 'coll_1');
  4. At the routing endpoint, create the routing function orders_mod_fn with the preceding statement.

  5. At the routing endpoint, create the scaleout table. It groups the member tables under one name and declares the routing key. MEMBERS names router table names, not cluster names.

    CREATE SCALEOUT TABLE orders ( ROUTE BY KEY(customer_id POSITION(1)) USING ROUTING FUNCTION orders_mod_fn ) MEMBERS ( MEMBER db.orders_s1, MEMBER db.orders_s2 );
  6. At the routing endpoint, verify that every collocation routes: insert and read back one test row for each collocation, with keys that no real customer uses. On each member cluster (direct), confirm that the row landed in the expected member table. Then delete it at the routing endpoint.

    INSERT INTO db.orders (customer_id, order_id) VALUES (TEST_KEY, 1); SELECT customer_id, order_id FROM db.orders WHERE customer_id = TEST_KEY AND order_id = 1; DELETE FROM db.orders WHERE customer_id = TEST_KEY AND order_id = 1;
  7. At the routing endpoint, save the output of SHOW CREATE SCALEOUT TABLE orders; with your DDL, and export the mappings again. The new export differs from the previous one only in the new member tables' rows.

The following table shows where two example queries route after you select the database with USE db;.

Statement Result
SELECT * FROM orders WHERE customer_id = 42; mod(42, 2) = 0, so the routing layer selects coll_0 and sends the query to the member cluster of ALIAS_A.
SELECT * FROM orders WHERE customer_id = 7; Routes to coll_1, on the member cluster of ALIAS_B.

To read the same rows on the member cluster itself, name the member table by its cluster name. In a session pinned to 'ALIAS_A', or over a direct connection to that member cluster, SELECT * FROM db.orders_phys WHERE customer_id = 42; returns the rows that the routed query returns. Only the routed query reaches them by the scaleout table name.

The routing layer rejects a routing function that names a collocation that no data mapping carries. It also rejects a scaleout table that has no member table in one of its routing function's collocations. CREATE SCALEOUT TABLE needs at least one member. A table can lose its last member through ALTER SCALEOUT TABLE ... DROP MEMBER, and the routing layer applies the check again once the table has members. A member table receives rows only when its router_collocation_group is a collocation of the routing function.

To read back a routing function, use SHOW CREATE SCALEOUT ROUTING FUNCTION.

Keeping a dependent table with its parent

Scaleout tables that route on the same key through the same routing function keep rows with the same key on the same member cluster. To keep a child table with its parent, create its member tables in the same collocations as the parent's member tables, and inherit the function from the root table with USING ROUTING FUNCTION OF.

The dependent table must carry the routing key column of its parent itself. Name the root table with OF, never a table that itself inherits its function. Inheriting a routing function governs routing only: it isn't a foreign key, and it doesn't check that a child's key value exists in the parent. CREATE SCALEOUT TABLE rejects a dependent table when one of its member tables has no data mapping. It doesn't check that the tables exist on the member clusters, so a missing table shows on the first routed statement. Before you create it, check the following:

  • The parent scaleout table exists and routes.

  • Every member table of the dependent table has a mapping row, in the same collocation as the parent's member table on that member cluster.

  • Each mapping row names a table that exists on that member cluster.

To keep items with orders from the preceding example, create the member table db.items_phys on each member cluster. Map the copies as items_s1 in coll_0 and items_s2 in coll_1. Then create the scaleout table at the routing endpoint.

CREATE SCALEOUT TABLE items ( ROUTE BY KEY(customer_id POSITION(1)) USING ROUTING FUNCTION OF orders ) MEMBERS ( MEMBER db.items_s1, MEMBER db.items_s2 );

Declaring the key columns

Declare the key columns in the order of the routing function's parameters. The routing layer rejects a scaleout table whose routing key has a different number of columns than its routing function has parameters. For the full grammar, see Scaleout table statements.

In ROUTE BY KEY, POSITION(n) gives the ordinal position of the column in the column list of the member table. The routing layer uses it for a statement where the column order is implicit, such as INSERT ... VALUES with no column list. For a two-column key, write both columns, for example ROUTE BY KEY(id POSITION(1), geo POSITION(2)).

On Aurora MySQL, a scaleout table can store a default character set and collation, which SHOW CREATE SCALEOUT TABLE shows. Routing doesn't read them, and they don't change the member tables.

The routing layer checks only that every key column has a POSITION or none does, that no POSITION is 0, and that no two key columns share one. It doesn't check POSITION(n) against the member tables. Make sure each routing key column is at that position in every member table. See Member table definitions.

Loading a scaleout table

Bulk load into a scaleout table isn't supported through the routing endpoint. Instead, load each member table directly with the rows that the routing function sends to its collocation, or, for a HASH scaleout table, load through routed INSERT statements. For the full procedure and the load tools, see Loading data into member clusters.

To load a scaleout table
  1. Connect directly to one member cluster, or pin a session to it at the routing endpoint. Name each member table by its cluster name, such as db.orders_phys, because a pinned session uses the cluster names of the member tables.

  2. On the member cluster (direct or pinned session), load each member table with only the rows whose key maps to its collocation. A pinned or direct load bypasses the routing function, and a routed read never finds a row that you load into the wrong member table. Under MOD, filter with the routing function's expression, as shown after this procedure. Under COLUMN, filter by the key values or key ranges that the collocation lists. Under PREFIX, compare the prefix of each key with the collocation's values or range bounds, not the key itself. For example, filter on SUBSTRING_INDEX(KEY_COLUMN, 'DELIMITER', DEPTH). If a key starts with the delimiter, use DEPTH + 1. Leave out keys with fewer than DEPTH segments, because no routed statement can reach them.

  3. Repeat the load for each remaining member cluster.

  4. Under HASH, no statement returns the hash or the collocation of a key value, so you can't compute a row's member table. Instead, load at the routing endpoint with INSERT statements that carry the routing key, one row in each statement, and the routing layer places each row.

In query routing mode, the routing layer rejects LOAD DATA into a scaleout table, because the statement carries no routing key value. For step 2, under MOD, filter the rows for each member table with the following condition. MOD routing keeps the sign of a negative key, as the MOD() function does on the engine, so list the negative remainders in a collocation if your keys can be negative.

WHERE MOD(KEY_COLUMN, DIVISOR) IN (BUCKETS_OF_THIS_COLLOCATION)

Under COLUMN with ranges, filter with the bounds of the collocation, both included. For a range that ends at MAXVALUE, use KEY_COLUMN >= LOW.

WHERE KEY_COLUMN BETWEEN LOW AND HIGH

A pinned session forwards every statement unchanged, so INSERT and LOAD DATA LOCAL INFILE both load a member table there. LOAD DATA LOCAL INFILE needs local_infile turned on in the member cluster and in the client. On a direct connection, you can also use LOAD DATA FROM S3, as Loading data into an Amazon Aurora MySQL DB cluster from text files in an Amazon S3 bucket describes.

On Aurora MySQL, the routing layer also refuses INSERT ... SELECT into a scaleout table, with error 1235.

To verify a pinned or direct load, check each member table on its member cluster, and then check a sample of keys at the routing endpoint:

  • On each member cluster (direct or pinned session), count the rows that don't belong to the member table's collocation. Use the filter that you loaded the table with, negated. Under MOD, for example, run the following statement. A count other than 0 means that the table holds rows that routed statements can't reach. A row whose key is NULL isn't reachable by any routed statement either, and the negated filter doesn't count it, so the example also counts rows where the key is NULL.

    SELECT COUNT(*) FROM CLUSTER_DB.MEMBER_TABLE WHERE MOD(KEY_COLUMN, DIVISOR) NOT IN (BUCKETS_OF_THIS_COLLOCATION) OR KEY_COLUMN IS NULL;
  • At the routing endpoint, for a few loaded keys from each member table, run EXPLAIN SELECT * FROM TABLE WHERE KEY_COLUMN = KEY_VALUE;. The Extra column of its first row, whose select_type is ROUTER, names the member cluster that the key reaches, after Target Member Clusters:. When two collocations share a member cluster, the alias doesn't tell which member table the key reaches; the table column of the next row, or Pushdown Query: in Extra, names the member table. This row appears only in the default (traditional) EXPLAIN format. Then run the same SELECT without EXPLAIN, and check that it returns the row.

To fix a row that is in the wrong member table, insert it at the routing endpoint, which places it in the member table of its collocation. Then delete it from the wrong member table over a direct connection or in a pinned session.

To check a workload and send it through the routing layer, see Sending traffic through the routing layer.

If something goes wrong

Check the following before you load data:

  • The routing layer refuses a test key, or the key lands in an unexpected member table. Don't load data. Check the key column positions and the routing function's coverage of every value.

  • A routed statement reports that a table or database doesn't exist (error 1146 or 1049 on Aurora MySQL). The table or database has no data mapping, or a mapping names a table that doesn't exist on its member cluster.

  • The routing layer refuses a CREATE statement. Check the grammar in Routing function statements or Scaleout table statements, fix the cause, and run the statement again.

  • A routed statement on a dependent table fails. At the routing endpoint, read dbclustergroup_sys.member_cluster_mappings first and dbclustergroup_sys.data_mappings second. Look for an empty list of member cluster aliases, a mapping whose collocation the routing function doesn't cover, and a mapping row whose table doesn't exist on its member cluster.

For each error code, see Error codes for database cluster groups for Aurora.

Removing a scaleout table that you created

DROP SCALEOUT TABLE removes only the scaleout table from the routing layer's metadata. The data mappings, the collocations, the routing function, and the member tables on the member clusters stay, with their data. One statement drops one scaleout table, and a drop doesn't cascade to the scaleout tables that inherit its routing function. The routing layer refuses to drop a scaleout table that another scaleout table names with USING ROUTING FUNCTION OF.

To remove a scaleout table and its parts
  1. At the routing endpoint, drop each dependent scaleout table first, or point it at a routing function of its own with ALTER SCALEOUT TABLE.

  2. At the routing endpoint, run DROP SCALEOUT TABLE.

  3. If no other scaleout table uses the routing function, remove it at the routing endpoint with DROP SCALEOUT ROUTING FUNCTION.

  4. If you no longer need the member tables, at the routing endpoint delete their data mappings, and then drop each member table on its member cluster (direct or pinned session) with DROP TABLE.

For the statements, see DROP SCALEOUT TABLE and DROP SCALEOUT ROUTING FUNCTION.

For related tasks and concepts, see the following pages: