什么datatype使用的MySQL唯一键/索引!

时间:2021-08-10 19:11:12

Is it ok to use INT as the datatype for the index in MySQL. I'm not sure if this is the best way to go as I fully expect this table to get a lot of entries that will eventually 'overflow' the INT

在MySQL中使用INT作为索引的数据类型可以吗?我不确定这是不是最好的方法,因为我完全希望这个表能得到很多最终会“溢出”INT的条目。

I doubt something like Facebook uses INT for their index on something such as the wallpost table or something else that would be high traffic / get lots of entries.

我怀疑Facebook在诸如wallpost表之类的东西上使用INT,或者其他高流量/获得大量条目的东西。

Any thoughts?

任何想法吗?

3 个解决方案

#1


2  

use INT( 11 ) UNSIGNED ZEROFILL NOT NULL

使用INT(11)未签名的零填充不为空。

#2


1  

An INT can go up to over 2 billion (twice that many if unsigned). If you expect your table to have more rows than that, you can use a BIGINT (which unsigned goes up to 18,446,744,073,709,551,615—probably more than even Facebook needs).

一个INT数可以超过20亿(如果没有签名的话,会增加两倍)。如果您希望您的表有更多的行,那么可以使用BIGINT(未签名的值最多为18,446,744,073,709,551,615,可能比Facebook需要的还要多)。

#3


1  

Generally speaking, you have two choices:

一般来说,你有两种选择:

A 'natural key' that reflects the reality of the data you are working with. For example, a telephone number might be a valid primary key for a calling list, but it would not be appropriate at all for, say, a list of health care beneficiaries because many people might share the same phone number in a home. Note that a natural key might be made up of more than one field. For example, the combination of Make, Model, and Year might be a key for a list of automobile models.

一个“自然键”反映了你正在处理的数据的真实性。例如,电话号码可能是呼叫列表的有效主键,但它对所有人来说都是不合适的,比方说,一份医疗保险受益人名单,因为许多人可能在家里共享相同的电话号码。请注意,自然键可以由多个字段组成。例如,Make、Model和Year的组合可能是汽车模型列表的关键。

A 'surrogate key' on the other hand, is just an arbitrary value that you assign to a row. If you go that route, I'd recommend using a GUID (UUID in MySql). The best way that I know to represent those in MySQL is with a char(36) column. GUIDs are effectively unique forever and can be used infinitely.

另一方面,一个“代理键”,只是您分配给一行的任意值。如果您走这条路线,我建议您使用GUID(在MySql中使用UUID)。我知道在MySQL中表示这些的最好方法是使用char(36)列。GUIDs实际上是独一无二的,可以无限使用。

If you insist on using a plain old number, then INT is probably fine, or you can use BIGINT to be really sure.

如果您坚持使用一个普通的旧数字,那么INT可能很好,或者您可以使用BIGINT来确定。

BIGINT UNSIGNED ZEROFILL

#1


2  

use INT( 11 ) UNSIGNED ZEROFILL NOT NULL

使用INT(11)未签名的零填充不为空。

#2


1  

An INT can go up to over 2 billion (twice that many if unsigned). If you expect your table to have more rows than that, you can use a BIGINT (which unsigned goes up to 18,446,744,073,709,551,615—probably more than even Facebook needs).

一个INT数可以超过20亿(如果没有签名的话,会增加两倍)。如果您希望您的表有更多的行,那么可以使用BIGINT(未签名的值最多为18,446,744,073,709,551,615,可能比Facebook需要的还要多)。

#3


1  

Generally speaking, you have two choices:

一般来说,你有两种选择:

A 'natural key' that reflects the reality of the data you are working with. For example, a telephone number might be a valid primary key for a calling list, but it would not be appropriate at all for, say, a list of health care beneficiaries because many people might share the same phone number in a home. Note that a natural key might be made up of more than one field. For example, the combination of Make, Model, and Year might be a key for a list of automobile models.

一个“自然键”反映了你正在处理的数据的真实性。例如,电话号码可能是呼叫列表的有效主键,但它对所有人来说都是不合适的,比方说,一份医疗保险受益人名单,因为许多人可能在家里共享相同的电话号码。请注意,自然键可以由多个字段组成。例如,Make、Model和Year的组合可能是汽车模型列表的关键。

A 'surrogate key' on the other hand, is just an arbitrary value that you assign to a row. If you go that route, I'd recommend using a GUID (UUID in MySql). The best way that I know to represent those in MySQL is with a char(36) column. GUIDs are effectively unique forever and can be used infinitely.

另一方面,一个“代理键”,只是您分配给一行的任意值。如果您走这条路线,我建议您使用GUID(在MySql中使用UUID)。我知道在MySQL中表示这些的最好方法是使用char(36)列。GUIDs实际上是独一无二的,可以无限使用。

If you insist on using a plain old number, then INT is probably fine, or you can use BIGINT to be really sure.

如果您坚持使用一个普通的旧数字,那么INT可能很好,或者您可以使用BIGINT来确定。

BIGINT UNSIGNED ZEROFILL