• Examples: Real-world use cases of composite primary keys. • Considerations: When and why to use them in your application. Fundamentals of Composite Primary Keys
for a composite primary key, e ff ectively representing many-to-many relationships. • Not all concepts are suitable for composite primary keys in a relational database. • Use of composite primary keys should be driven by database schema design, not just Rails' support. Recap
on Performance: How altering the primary key of a table to a composite primary key can improve performance of certain operations. • Trade-o ff s involved in using composite primary keys. Composite Primary Keys in Multi-Tenant Applications
slow queries Reduction in the overall time taken by slow queries 500X 10X Improvement for some queries with low volume Improvement for remaining slow queries
it from the use of composite primary keys by including the tenant key in the primary key • Impact on Performance: Altering the primary key of a table to a composite primary key can enhance the speed of bulk reads and updates. However, this comes at the cost of a slower insertion rate. • Consideration for Insertion Speed: For applications where the speed of data insertion is essential, using composite primary keys might not be the best option. Recap
tenant-sharded models both typically include multiple columns in queries. • Implementing composite primary keys can be a foundational, but not necessary, step towards a full sharding strategy. Recap
Rails models. • How to work with a composite primary key model. • How to de f ine a virtual primary key. • Limitations: What not to do when de f ining composite primary keys. Active Record Support for Composite Primary Keys
"description" = 'from A to B’ WHERE "travel_routes"."origin" = 'A' AND "travel_routes"."destination" = 'B' route.delete # => DELETE FROM "travel_routes" WHERE "travel_routes"."origin" = 'A' AND “travel_routes"."destination" = ‘B'
route, body: "This is my favorite route!” ) review.travel_route # => SELECT "travel_routes".* FROM "travel_routes" WHERE "travel_routes"."origin" = ‘A' AND "travel_routes"."destination" = 'B' LIMIT 1
support for composite primary keys. • Usage of 'id' Column: Preferably, the column named id should be used only for single-column primary keys. • Virtual Primary Key: The query_constraints option can be used as a "virtual primary key" to mimic primary key behavior without altering the database schema. This can be particularly useful in certain tenant-based sharding implementations. Recap
unnecessary and excessive. Example: Join tables • Multi tenant applications with a lot of bulk reads scoped per single tenant. When to consider using composite primary keys