Differences Between Logical Keys and Physical Indexes in MySQL
In MySQL, logical keys describe the logical structure or rules of a table (such as candidate keys, composite keys, or superkeys), while physical indexes define how MySQL stores and organizes data for fast retrieval. Logical keys are part of data modeling, and physical indexes are part of database storage and optimization.
Define the logical uniqueness rules of a table.
Describe which columns can uniquely identify a row (candidate keys).
Can include multiple columns (composite keys).
Not all logical keys must be implemented physically.
Exist at the schema design level, regardless of storage engine behavior.
Candidate key → any minimal column set that uniquely identifies a row.
Composite key → a key made of multiple columns (e.g., (order_id, product_id)).
Primary key → the chosen candidate key for the table.
Alternate keys → candidate keys not selected as the primary key.
Actual data structures that MySQL stores on disk (B+Tree indexes for InnoDB).
Used to speed up lookups, JOINs, sorting, filtering, and enforcing constraints.
Created automatically for PRIMARY KEY and UNIQUE constraints.
Can exist even when not tied to logical keys (e.g., an index just for performance).
Logical keys exist in the data model; physical indexes exist in storage.
Logical keys describe uniqueness; indexes enforce uniqueness when tied to constraints.
Composite logical keys may or may not become composite indexes.
Indexes can exist without being logical keys (e.g., an index on order_date for performance).
InnoDB physically organizes data by the PRIMARY KEY (clustered index), which is a physical index—not a logical concept.
Logical keys guide schema design and define unique identification.
Physical indexes determine query speed, access paths, and optimizer decisions.
MySQL automatically turns certain logical constraints (PRIMARY KEY, UNIQUE) into indexes.
Foreign keys require physical indexes on child and parent columns.
In summary, logical keys define how data should behave, while physical indexes determine how MySQL stores and retrieves data efficiently. Both are related but serve different layers—logical design vs. physical performance.
You need to guarantee that (first_name, last_name) is unique in a MySQL table. How would you implement that, and what’s the difference between adding a UNIQUE constraint versus creating a separate index?
If you define a composite primary key on (order_id, product_id), how does MySQL store the rows internally and use that key for lookups?
We have an 'email' column that must be unique and also queried frequently. Explain why you might declare it as a UNIQUE key instead of just adding a regular index.
Our service is slow when filtering on a composite candidate key (user_id, region). The optimizer isn’t using the index you created. Walk me through why the logical key definition might not help and what you’d check or change.
During a migration we replaced a surrogate primary key with a composite key. After deployment, insert latency doubled. Why could the physical index layout cause this slowdown, and how would you address it?
A query that filters on just one column of a composite UNIQUE key ends up doing a full table scan. Explain why the logical key isn’t sufficient and what index adjustment you’d make.
We have a high‑traffic orders table with a composite primary key (customer_id, order_date) and several secondary indexes. At 10 M rows we see lock contention and index bloat. Discuss the trade‑offs of keeping that logical key as the primary key versus switching to a surrogate key with separate indexes.
Design a sharding strategy for a multi‑tenant SaaS where each tenant’s data is identified by a logical composite key (tenant_id, entity_id). How would you map those logical keys to physical indexes to keep joins fast while minimizing index size?
Our reporting service joins on a composite foreign key that mirrors a candidate key in another table. Explain the impact on join performance and how you’d structure indexes to avoid redundant physical indexes.
Our legacy monolith uses many candidate keys as primary keys, resulting in large composite indexes. We’re moving to a microservices architecture with separate services per entity. Outline a migration roadmap to replace logical keys with surrogate primary keys and redesign physical indexes, covering data consistency, downtime, and future schema evolution.
In a multi‑region MySQL cluster we replicate tables that have composite primary keys. What challenges do physical index ordering and conflict resolution present, and how would you decide between keeping logical composite keys or switching to surrogate keys to ensure deterministic replication and low latency?