Skip to content

Instantly share code, notes, and snippets.

@Hammadk
Last active January 12, 2024 20:13
Show Gist options
  • Save Hammadk/b4843633ede12fb5647f6bf66efb9333 to your computer and use it in GitHub Desktop.
Save Hammadk/b4843633ede12fb5647f6bf66efb9333 to your computer and use it in GitHub Desktop.
Cases where Lookup Vindexes are not required to enforce global uniqueness.
# 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