Using database-level and table-level mappings
A data mapping maps a whole object to one member cluster of a database cluster group for Aurora. A database-level mapping maps a whole database and every object in it, and a table-level mapping maps one table of the database. For how mappings work, see Data mappings and collocations. For the statement grammar, see Data mapping statements.
Starting with database-level mappings
Database-level mappings are the most common starting point. If your application already keeps each tenant or domain in its own database, map each one to the member cluster that already holds it.
Aurora MySQL supports database-level and table-level mappings, as the following table shows.
| Level | What it places |
|---|---|
| Database | An entire database on one member cluster |
| Table | One table on one member cluster |
Choosing the mapping level
A database-level mapping gives you the following:
-
No routing key to choose or carry. The routing layer directs every query on a mapped name to the member cluster that stores it. For queries on mapped names, the change to the application is the connection string.
-
The wider SQL surface. A routed session on mapped databases supports the SQL that applications send, including joins, subqueries, common table expressions, transactions, savepoints, and prepared statements. It rejects DDL, which runs through a pinned session or a direct connection. On Aurora MySQL,
CALLand stored function calls run on the member cluster that the database's data mapping names. On Aurora MySQL, each member cluster generatesAUTO_INCREMENTvalues for its own tables. -
Whole-database moves. A database in its own collocation moves in one operation.
-
Child tables follow their parents. With a database for each tenant or domain, every table of that database is on one member cluster, so joins between those tables resolve to that member cluster.
Use a table-level mapping when tables of one database must sit on different member clusters or move on their own. Table-level mappings also place the member tables of a scaleout table.
To choose a placement for each object, work through these questions in order:
-
Does the whole object fit on one member cluster? Map it with a data mapping. This covers a database for each tenant and any table that you keep whole.
-
Must the rows of one table span member clusters? Use a scaleout table, the only construct that spreads the rows of one table. See Choosing a routing key and routing function.
-
Must this object move on its own, or stay with related objects? Give an object that moves alone its own collocation, and give related objects one shared collocation. See Planning collocations and headroom.
-
Which level? Map at the coarsest level that expresses your intent. A database-level mapping collocates everything in that database.
If you plan to move an object by itself, for example one database for each tenant, give it its own collocation when you create its mapping. To move one mapping to its own collocation later, set router_collocation_group and member_cluster_alias together in one UPDATE. Use the current alias to keep the data where it is. Don't delete and insert the mapping again, because the object doesn't route between the two statements. If the mapping is a member table of a scaleout table, the UPDATE fails with error 64066 when it would leave a collocation that the routing function targets without a member.
Rules and limits of each level
Each level has the following rules and limits:
-
One database, one level. A database-level mapping and a table-level mapping can't coexist for one database. While the database-level mapping exists, the routing layer rejects a table-level mapping in that database with error 64060 (SQLSTATE
42000) on Aurora MySQL. -
Map every table or none. In a database with table-level mappings, a table that you leave unmapped isn't reachable through the routing endpoint.
-
One database must fit one member cluster. To grow the database, scale that member cluster up. A database moves whole, so you can't spread one large tenant database.
-
Cross-database work is local only on one member cluster. The routing layer refuses joins and transactions across databases on different member clusters. Put databases that your application joins or writes together in one collocation.
-
A database that holds a scaleout table can't also be database-level. In a group that mixes both, keep the scaleout table in its own database.
-
A table can become a scaleout table later. Convert a table when it outgrows its member cluster. Data movement moves whole tables, so you load the member tables of the new scaleout table yourself.
-
Names match in exact letter case. On Aurora MySQL,
lower_case_table_namesis fixed at 0, so the routing layer matches database and table names in exact letter case, quoted or not.
Changing from database-level to table-level mappings
Changing a database from database-level to table-level placement is a replacement, not an edit. You delete one database-level row and insert one table-level row for each table, in that order, which is the only order that the routing layer accepts. A mapping change reaches the routing layer within a short time. For what an open session or transaction sees in that time, see Applying data mapping changes.
The database is unreachable during the change
From the delete until the table-level mappings reach the routing layer, the tables of that database aren't reachable through the routing endpoint. The following procedure shows the error that a client gets. Stop application traffic to that database before you start, and run the insert directly after the delete.
Before you change the mappings, export them, as Exporting the mappings describes. Run every statement in the following procedure at the routing endpoint, in query routing mode, as a router user that has dbclustergroup_metadata_admin. Run each mapping statement outside a transaction. One multi-row INSERT applies all of its rows or none, so put every table of the database in one statement. The routing layer rejects ORDER BY on dbclustergroup_sys.data_mappings, so sort the rows that you read on the client.
One statement can add, change, or remove at most 1,000 data mappings. For more information about the limit, see Routing metadata and naming limits. A database with more tables needs several statements, and its tables stay partly mapped until the last statement runs.
During the change, a connection to the database succeeds, but a statement on one of its tables fails with error 1146 (SQLSTATE 42S02): Table '<database>.<table>' doesn't exist.
To change from database-level to table-level mappings
-
In your application, stop traffic to the database,
app_dbin this example. -
At the routing endpoint, read the database-level row, and note its
member_cluster_aliasandrouter_collocation_group. The delete in the next step removes the only record of both values. A database-level row has an empty string, not NULL, in its table column, so an emptyrouter_table_nameselects only that row.SELECT * FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('app_db', ''); -
At the routing endpoint, delete the database-level mapping.
DELETE FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('app_db', ''); -
At the routing endpoint, insert the table-level mappings for every table of that database in one multi-row
INSERT. In every row, use themember_cluster_aliasand therouter_collocation_groupthat you noted. A row that names no collocation gets the default collocation. The following example maps the two tablesordersandcustomersofapp_db.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 ('app_db', 'orders', 'app_db', 'orders', 'MEMBER_CLUSTER_ALIAS', 'COLLOCATION'), ('app_db', 'customers', 'app_db', 'customers', 'MEMBER_CLUSTER_ALIAS', 'COLLOCATION'); -
At the routing endpoint, read the mappings back, and compare them with your export.
SELECT * FROM dbclustergroup_sys.data_mappings WHERE router_db_name = 'app_db'; -
At the routing endpoint, run one routed read on each table. Then, in your application, restart traffic to the database.
After the change, the database's tables can move to other member clusters on their own, as their collocations allow. To add, repoint, or remove mappings after you load data, see Changing data mappings.
Example: placing a table on one member cluster
The following example places one whole table on one member cluster with a table-level mapping. A new group has no data mappings, so no object is reachable through the routing endpoint until you add a mapping.
Your router user needs a member cluster user of the same name that has privileges on the table. Without one, the router user can't reach that member cluster or pin to it. For more information, see Router users and routing layer privileges.
Create the table on the member cluster over a direct connection or a pinned session. A pinned session passes the DDL through to the member cluster unchanged, and it names objects by their cluster names. The routing layer doesn't check a mapping against the member cluster, so create the table before you map it. Then add the mapping at the routing endpoint, in a session that isn't pinned. A pinned session sends a mapping statement to the member cluster, not to the routing layer. For pinning, see Sessions, transactions, and session pinning.
Before you start, create the database retail_db on the member cluster where you place the table.
To create the table on the member cluster
-
At the routing endpoint, in query routing mode, copy the alias of the member cluster where you place the table.
SELECT member_cluster_alias, db_cluster_identifier FROM dbclustergroup_sys.member_cluster_mappings; -
Connect to that member cluster directly through its cluster endpoint, or pin a session to it at the routing endpoint with
SET SESSION router_cluster_pin = '.MEMBER_CLUSTER_ALIAS'; -
On the member cluster (direct or pinned session), create the table.
CREATE TABLE retail_db.customer (customer_id INT NOT NULL PRIMARY KEY, name VARCHAR(100)); -
If you used a pinned session, at the routing endpoint, return it to query routing mode with
SET SESSION router_cluster_pin = DEFAULT;.
To map and query the table
-
Connect to the routing endpoint as a router user that has
dbclustergroup_metadata_admin. You can keep the session from the previous procedure if its user has that privilege. -
At the routing endpoint, insert the mapping outside a transaction. If your driver opens a transaction by default, turn on autocommit for this session. This row places the table
retail_db.customerunder the router table namecustomer_s1, in a collocation of its own, so that the table can move by itself later.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 ('retail_db', 'customer', 'retail_db', 'customer_s1', 'MEMBER_CLUSTER_ALIAS', 'cg_customer'); -
At the routing endpoint, read the row back to check the member cluster alias and the cluster names.
SELECT * FROM dbclustergroup_sys.data_mappings WHERE router_db_name = 'retail_db'; -
At the routing endpoint, query the router name. After the mapping reaches the routing layer, the routing layer sends the statement to the member cluster that the mapping names.
SELECT * FROM retail_db.customer_s1;
Packaged applications
A packaged or third-party application is one that you install and run but don't change. Its schema and SQL come from its vendor. You add its clusters to a group as they are and, if you route it, use database-level mappings. The group then scales the number of instances that you run, not one instance beyond a member cluster.
Scaleout tables don't apply. They need a routing key on every owned table and nearly every statement. You can't add a column to a schema that you don't own or change the queries that its persistence layer generates. With a database-level mapping, every table of an instance's database lives on one member cluster, so the application's joins, transactions, sequence tables, and lookups by ID work as they do on one cluster.
Plan the placement as follows:
-
One instance, one collocation. If an instance uses several databases, map them into the same collocation, because the routing layer refuses statements across databases on different member clusters.
-
Rebalance by moving instances. To move load off a busy member cluster, move whole instances off it by moving their collocations with data movement. The application keeps the same endpoint and database names. For more information, see Moving data between member clusters.
-
Put new instances where there is headroom. Create the new instance's database on its target member cluster, then map it.
-
Scale one instance vertically. Raise its member cluster's capacity. When the member cluster can't grow further, move the other instances off it.
In the compatibility pass, exercise every feature that you rely on, including the installer, upgrades, background jobs, and reports. Run the application's own schema creation and upgrades on the member cluster, directly or through a pinned session. If the installer can't set a session pin, point it at the member cluster's endpoint. Check the driver that the product ships. If a check fails and the product has no setting for it, keep that instance on its cluster endpoint. For the compatibility pass, see Testing an existing application before you route it.
Then map each instance's databases where they are, and verify each instance with a routed read for each mapping and a smoke test of the product's main screens and jobs. Point instances at the routing endpoint one at a time. To go back, point the instance at its cluster endpoint again.
Related pages
The following pages cover related tasks and concepts.