Global Routing

Global Table Routing

Vitess has an implicit feature of routing the queries to the appropriate keyspace based on the table specified in the from list. This differs from the standard mysql, in mysql unqualified tables will fail if the correct database is not set on the connection.

This feature works only for unique table names provided in the VSchema, and only when no default keyspace is set on the connection. One exception to the uniqueness rule is Reference Tables that explicitly specify a source table.

Example:

  1. mysql> show keyspaces;
  2. +----------+
  3. | Database |
  4. +----------+
  5. | ks |
  6. | customer |
  7. | commerce |
  8. +----------+
  9. 3 rows in set (0.00 sec)

ks and customer are sharded keyspaces and commerce is an unsharded keyspace.

Tables present in each of the keyspace.

  1. mysql> show tables from ks;
  2. +--------------+
  3. | Tables_in_ks |
  4. +--------------+
  5. | customer |
  6. | matches |
  7. | player |
  8. +--------------+
  9. 3 rows in set (0.00 sec)
  10. mysql> show tables from customer;
  11. +--------------------+
  12. | Tables_in_customer |
  13. +--------------------+
  14. | corder |
  15. | customer |
  16. +--------------------+
  17. 2 rows in set (0.00 sec)
  18. mysql> show tables from commerce;
  19. +--------------------+
  20. | Tables_in_commerce |
  21. +--------------------+
  22. | customer_seq |
  23. | order_seq |
  24. | product |
  25. +--------------------+
  26. 3 rows in set (0.00 sec)

Without a default keyspace we can route to unique tables like corder, product and player but cannot route to customer

  1. mysql> show columns from corder;
  2. +-------------+----------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------------+----------------+------+-----+---------+-------+
  5. | order_id | bigint | NO | PRI | NULL | |
  6. | customer_id | bigint | YES | | NULL | |
  7. | sku | varbinary(128) | YES | | NULL | |
  8. | price | bigint | YES | | NULL | |
  9. +-------------+----------------+------+-----+---------+-------+
  10. 4 rows in set (0.01 sec)
  11. mysql> show columns from product;
  12. +-------------+----------------+------+-----+---------+-------+
  13. | Field | Type | Null | Key | Default | Extra |
  14. +-------------+----------------+------+-----+---------+-------+
  15. | sku | varbinary(128) | NO | PRI | NULL | |
  16. | description | varbinary(128) | YES | | NULL | |
  17. | price | bigint | YES | | NULL | |
  18. +-------------+----------------+------+-----+---------+-------+
  19. 3 rows in set (0.00 sec)
  20. mysql> show columns from player;
  21. +-----------+-------------+------+-----+---------+-------+
  22. | Field | Type | Null | Key | Default | Extra |
  23. +-----------+-------------+------+-----+---------+-------+
  24. | player_id | bigint | NO | PRI | NULL | |
  25. | name | varchar(50) | NO | | NULL | |
  26. +-----------+-------------+------+-----+---------+-------+
  27. 2 rows in set (0.00 sec)
  28. mysql> show columns from customer;
  29. ERROR 1105 (HY000): ambiguous table reference: customer

With the default keyspace set to customer we can only query tables in commerce i.e customer and corder.

  1. mysql> use customer
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> show columns from customer;
  6. +-------------+----------------+------+-----+---------+-------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +-------------+----------------+------+-----+---------+-------+
  9. | customer_id | bigint | NO | PRI | NULL | |
  10. | email | varbinary(128) | YES | | NULL | |
  11. +-------------+----------------+------+-----+---------+-------+
  12. 2 rows in set (0.01 sec)
  13. mysql> show columns from corder;
  14. +-------------+----------------+------+-----+---------+-------+
  15. | Field | Type | Null | Key | Default | Extra |
  16. +-------------+----------------+------+-----+---------+-------+
  17. | order_id | bigint | NO | PRI | NULL | |
  18. | customer_id | bigint | YES | | NULL | |
  19. | sku | varbinary(128) | YES | | NULL | |
  20. | price | bigint | YES | | NULL | |
  21. +-------------+----------------+------+-----+---------+-------+
  22. 4 rows in set (0.00 sec)
  23. mysql> show columns from product;
  24. ERROR 1105 (HY000): table product not found

With a default keyspace set, the queries can be routed to other keyspaces by specifying the table qualifier.

  1. mysql> use customer
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> show columns from ks.player;
  6. +-----------+-------------+------+-----+---------+-------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +-----------+-------------+------+-----+---------+-------+
  9. | player_id | bigint | NO | PRI | NULL | |
  10. | name | varchar(50) | NO | | NULL | |
  11. +-----------+-------------+------+-----+---------+-------+
  12. 2 rows in set (0.00 sec)
  13. mysql> show columns from commerce.product;
  14. +-------------+----------------+------+-----+---------+-------+
  15. | Field | Type | Null | Key | Default | Extra |
  16. +-------------+----------------+------+-----+---------+-------+
  17. | sku | varbinary(128) | NO | PRI | NULL | |
  18. | description | varbinary(128) | YES | | NULL | |
  19. | price | bigint | YES | | NULL | |
  20. +-------------+----------------+------+-----+---------+-------+
  21. 3 rows in set (0.00 sec)