CREATE SCALEOUT TABLE
Use CREATE SCALEOUT TABLE to spread the rows of one table across member tables on several member clusters of a database cluster group for Aurora.
Description and syntax
Creates a scaleout table: one table name at the routing endpoint whose rows a routing key spreads across member tables on several member clusters. The routing function maps each key to a collocation, and each member table is the only member table of the scaleout table in its collocation. The statement creates nothing on a member cluster; you create and load the member tables yourself. For the concepts, see Scaleout tables and routing functions. For a worked example with loading, see Creating and loading a scaleout table.
CREATE SCALEOUT TABLEtable_name( ROUTE BY KEY(columnPOSITION(n) [,columnPOSITION(n) ...]) { USING ROUTING FUNCTIONfunction_name| USING ROUTING FUNCTION OFparent_table} ) [table_option[[,]table_option] ...] MEMBERS ( MEMBERmember_table[, MEMBERmember_table...] );table_option::= [DEFAULT] CHARACTER SET [=]charset| [DEFAULT] COLLATE [=]collation
The table options can come in any order, with or without a comma between them.
Parameters and options
The statement takes the following parameters.
table_name-
A name unique across the group, not only in its database. Scaleout tables and router table names share one namespace, so the name must differ from every router table name (the
router_table_nameof a data mapping) and from every other scaleout table.On Aurora MySQL, you can qualify the name with a database; in queries, the routing layer accepts and ignores the qualifier. You query the table through a database that holds its member tables.
- ROUTE BY KEY(
columnPOSITION(n) [, ...]) -
The routing key columns and their positions in the member tables:
POSITION(2)is the second column. GivePOSITIONon every key column or on none. WithoutPOSITION, the routing layer refuses everyINSERT ... VALUESthat has no column list (Aurora MySQL error 1235,Routing key column '<column>' has an invalid position in metadata). AnINSERTwith a column list doesn't need it. The key has as many columns as the routing function has parameters, in parameter order. Give every scaleout table aROUTE BY KEYclause; you can't query a scaleout table that has none. - USING ROUTING FUNCTION
function_name -
The routing function that places the rows. A scaleout table uses one routing function. Every scaleout table routed over the same collocations must use the same routing function.
- USING ROUTING FUNCTION OF
parent_table -
Follows the routing function of a parent scaleout table, and switches when the parent switches. The parent has its own function, not one taken with
OF. The child table carries the routing key column itself. - [DEFAULT] CHARACTER SET
charset, [DEFAULT] COLLATEcollation -
The table default character set and collation. The routing layer stores them, and
SHOW CREATE SCALEOUT TABLEshows them. A character routing key that doesn't name its ownCOLLATEtakes these settings, and the routing layer compares its values under that collation. A character routing key needs the same collation on every member cluster. - MEMBER
member_table -
The router table name of one member table, from its table-level data mapping, fully qualified. List one for each collocation that the function targets.
On Aurora MySQL, write
db.member_table, such asdb.orders_s1.
Where it runs and required privileges
Run the statement at the routing endpoint, in a session that isn't pinned, outside a transaction. On Aurora MySQL, inside a transaction the routing layer refuses the statement with error 1179. For the details, see Where these statements run.
The router user needs dbclustergroup_metadata_admin or dbclustergroup_super_admin. For all privileges for database cluster groups for Aurora, see Privileges.
Usage notes
Keep the following in mind when you use the statement.
-
Each member table needs a table-level data mapping first, in a collocation that no other member table of the same scaleout table uses. Create the objects in the order shown in Order of creation.
-
The routing layer doesn't check the member tables, either when you create the scaleout table or add a member, or when you change a member table later. Keep every member table identical: the same columns, in the same order, with the same data types. Otherwise a statement can fail on some member clusters, or an
INSERTwithout a column list can write values into the wrong columns. See Member table definitions. -
The routing layer uses
POSITIONfor anINSERT ... VALUESwith no column list. -
To keep related rows together, give each related table the same routing key and routing function, or point it at the parent with
OF. A join on the key then runs on one member cluster. -
The routing layer refuses a statement without the routing key. Write the key as an equality or
INpredicate on a literal. For the key forms that route, and the functions that the routing layer evaluates in a key value, see Statement compatibility, drivers, ORMs, and tools. -
You can't update a routing key column. A unique constraint holds across the scaleout table only when it includes every routing key column. An auto-increment column or a sequence on a member table counts within its own member cluster, so its values can repeat across member clusters. Generate values that must be unique across the group in your application.
-
A multi-row
INSERTmust send every row to one member cluster. Group the rows by member cluster in your application, and send oneINSERTfor each. For aHASHrouting function, your application can't compute the member cluster of a row, so insert one row in each statement. -
On Aurora MySQL, text and binary-protocol prepared statements work on a scaleout table. The routing layer routes each execute by the routing key value bound for that execute. It prepares the statement on a new member cluster first.
-
In query routing mode, the routing layer refuses
INSERT ... SELECTandINSERT ... SETinto a scaleout table, also when every row goes to one member cluster. On Aurora MySQL, both return error 1235.INSERT ... SELECTreturnsINSERT with a non-VALUES source is not supported for scaleout routing, andINSERT ... SETreturnsINSERT without a VALUES clause is not supported for scaleout routing. UseINSERT ... VALUES. -
A pinned session can't reach a scaleout table. It reaches the member tables by their names on the member cluster, so keep each row you write there in the member table that its key maps to.
-
Keep your own record of the statement. You read a definition back only with SHOW CREATE SCALEOUT TABLE.
-
Aurora MySQL doesn't support
EXPLAIN CREATE SCALEOUT TABLEyet. The routing layer refuses it with error 1235,EXPLAIN for SCALEOUT TABLE DDL is not supported yet.
Examples
The following example creates orders, routed by customer_id through the routing function orders_mod_fn, with one member table in each of its two collocations.
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 );
To keep items with orders, create items on the same key with USING ROUTING FUNCTION OF orders (or USING ROUTING FUNCTION orders_mod_fn), with its member tables mapped in the same collocations as the orders member tables.
CREATE SCALEOUT TABLE items ( ROUTE BY KEY(customer_id POSITION(1)) USING ROUTING FUNCTION OF orders ) MEMBERS ( MEMBERitems_member_1, MEMBERitems_member_2);
Errors
A refused statement leaves nothing behind. For all errors from the routing layer, see Error codes for database cluster groups for Aurora.
| Aurora MySQL | Meaning |
|---|---|
64025 Data mapping '<name>' already exists (42000) |
A router table name uses the name. |
64000 Member '<member>' has no data mapping. Create a data mapping for it. (42000) |
A member table isn't mapped at table level, or the statement names its cluster name. Map it first, and name its router table name. |
64040 Member '<member>' already belongs to a scaleout table (42000) |
A member table belongs to one scaleout table only. |
64065 Members '<member>' and '<member>' are in collocation group '<collocation>'. A scaleout table can have only one member per collocation group. (42000) |
Two member tables share a collocation. |
64066 Scaleout table '<table>' has no member in collocation group '<collocation>', which its routing function targets (42000) With more than one group, the message reads collocation groups '<a>', '<b>'. |
No listed member table is in a targeted collocation. |
64020 Scaleout table '<name>' already exists (42000) |
A scaleout table with the name already exists. With IF NOT EXISTS, the statement succeeds and leaves the existing definition unchanged. |
64001 Scaleout table '<parent>' doesn't exist (42000) |
USING ROUTING FUNCTION OF names a parent that doesn't exist. The parent must have its own routing function. |
64061 Scaleout table '<name>' can't inherit its routing function from '<parent>'. '<parent>' must define its own routing function. (42000) |
The OF parent itself uses OF, or has no routing function of its own. |
| 1064 | Syntax error: a form outside the grammar. |
1179 CREATE SCALEOUT TABLE is not allowed inside a transaction (opened by BEGIN/START TRANSACTION or SET autocommit=0) (25000) |
The statement ran inside a transaction. |
1227 Access denied; you need the '<PRIVILEGE>' privilege for this operation |
The router user lacks the privilege for database cluster groups for Aurora. |
A member table or routing key column that is missing on its member cluster causes no error when you create the scaleout table. The routing layer doesn't check the member clusters, so the statement succeeds, and the member cluster returns its own error when a later statement reaches the missing table or column.
Related statements
For more information, see the following: