简体   繁体   中英

What is the best way to choose primary key in candidate key?

I am concerned about which candidate key to select as the primary key among the candidate keys.

Assume that using mysql database(innoDB). Suppose we have a unique value, Student Number , and a unique value, ID Number(eg Social Security Number) .

Student ID number and ID number can each be a candidate key. In this case, what value should I set as the primary key even considering auto-increment new column?

My guess is that innoDB(mysql) use primary key to create the clustering index . So, is it right to use a column where I need to find a specific range, since it has the advantage of being able to find a range?

Thank you!!

First, you should be aware that the US Social Security Number is not unique .

You're correct that InnoDB always treats the primary key as the clustering index. You don't have to guess, it's documented in the manual:https://dev.mysql.com/doc/refman/8.0/en/innodb-index-types.html

You don't necessarily need to use the primary key to find a specific range. You could use a secondary index as well. You could even use a non-indexed column, but it would result in a table-scan which causes poor performance unless the table is very small.

Given the choice between searching the clustered index versus a secondary index, it's a little bit more optimized to search the clustered index.

There are exceptions to the guideline, and we can't know if your query is one of those exceptions because you haven't described any of your queries.

This brings up a broader point: you can't choose the best optimization strategy without knowing the specific queries you need to optimize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM