Last active
January 12, 2024 20:13
-
-
Save Hammadk/b4843633ede12fb5647f6bf66efb9333 to your computer and use it in GitHub Desktop.
Cases where Lookup Vindexes are not required to enforce global uniqueness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# All tables have a user_id column, and the keyspace is sharded by this column. | |
CREATE TABLE `products` ( | |
`id` BIGINT NOT NULL, | |
`user_id` BIGINT NOT NULL, | |
PRIMARY KEY (`id`) | |
) | |
CREATE TABLE `orders` ( | |
`id` BIGINT NOT NULL, | |
`user_id` BIGINT NOT NULL, | |
`product_id` BIGINT NOT NULL, | |
`line_item_id` BIGINT NOT NULL, | |
PRIMARY KEY (`id`), | |
# Not unique across shards. We would require a Lookup Vindex. | |
UNIQUE KEY `index_on_line_item_id` (`line_item_id`), | |
# Unique across shards, because each 'user_id' is only in one shard. | |
UNIQUE KEY `index_orders_on_user_id_and_line_item_id` (`user_id`,`line_item_id`), | |
# Unique across shards, because the 'products' table is sharded by 'user_id'. Uniqueness is guaranteed as a 'product_id' will only exist in a single shard. | |
UNIQUE KEY `index_orders_on_product_id_and_line_item_id` (`product_id`,`line_item_id`), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment